001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.cs;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.foundation.network.*;
026: import com.db4o.internal.*;
027: import com.db4o.internal.cs.messages.*;
028:
029: class ClientMessageDispatcherImpl extends Thread implements
030: ClientMessageDispatcher {
031:
032: private ClientObjectContainer i_stream;
033: private Socket4 i_socket;
034: private final BlockingQueue _messageQueue;
035: private boolean _isClosed;
036:
037: ClientMessageDispatcherImpl(ClientObjectContainer client,
038: Socket4 a_socket, BlockingQueue messageQueue_) {
039: i_stream = client;
040: _messageQueue = messageQueue_;
041: i_socket = a_socket;
042: }
043:
044: public synchronized boolean isMessageDispatcherAlive() {
045: return !_isClosed;
046: }
047:
048: public synchronized boolean close() {
049: if (_isClosed) {
050: return true;
051: }
052: _isClosed = true;
053: if (i_socket != null) {
054: try {
055: i_socket.close();
056: } catch (Db4oIOException e) {
057:
058: }
059: }
060: _messageQueue.stop();
061: return true;
062: }
063:
064: public void run() {
065: messageLoop();
066: close();
067: }
068:
069: public void messageLoop() {
070: while (isMessageDispatcherAlive()) {
071: Msg message = null;
072: try {
073: message = Msg
074: .readMessage(this , transaction(), i_socket);
075: } catch (Db4oIOException exc) {
076: return;
077: }
078: if (message == null) {
079: continue;
080: }
081:
082: // TODO are there possibly messages that have to be processed *and* passed on?
083: if (isClientSideMessage(message)) {
084: if (((ClientSideMessage) message).processAtClient()) {
085: continue;
086: }
087: }
088: _messageQueue.add(message);
089:
090: }
091: }
092:
093: private boolean isClientSideMessage(Msg message) {
094: return message instanceof ClientSideMessage;
095: }
096:
097: public boolean write(Msg msg) {
098: i_stream.write(msg);
099: return true;
100: }
101:
102: public void setDispatcherName(String name) {
103: setName("db4o client side message dispather for " + name);
104: }
105:
106: public void startDispatcher() {
107: start();
108: }
109:
110: private Transaction transaction() {
111: return i_stream.transaction();
112: }
113:
114: }
|