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.foundation.network;
022:
023: import com.db4o.*;
024:
025: /**
026: * Fakes a socket connection for an embedded client.
027: */
028: public class LoopbackSocket implements Socket4 {
029:
030: private final LoopbackSocketServer _server;
031:
032: private LoopbackSocket _affiliate;
033: private BlockingByteChannel _uploadBuffer;
034: private BlockingByteChannel _downloadBuffer;
035:
036: public LoopbackSocket(LoopbackSocketServer a_server, int timeout) {
037: _server = a_server;
038: _uploadBuffer = new BlockingByteChannel(timeout);
039: _downloadBuffer = new BlockingByteChannel(timeout);
040: }
041:
042: public LoopbackSocket(LoopbackSocketServer a_server, int timeout,
043: LoopbackSocket affiliate) {
044: this (a_server, timeout);
045: _affiliate = affiliate;
046: affiliate._affiliate = this ;
047: _downloadBuffer = affiliate._uploadBuffer;
048: _uploadBuffer = affiliate._downloadBuffer;
049: }
050:
051: public void close() throws Db4oIOException {
052: closeAffiliate();
053: closeSocket();
054: }
055:
056: private void closeAffiliate() throws Db4oIOException {
057: if (_affiliate != null) {
058: LoopbackSocket temp = _affiliate;
059: _affiliate = null;
060: temp.close();
061: }
062: }
063:
064: private void closeSocket() {
065: _downloadBuffer.close();
066: _uploadBuffer.close();
067: }
068:
069: public void flush() {
070: // do nothing
071: }
072:
073: public boolean isConnected() {
074: return _affiliate != null;
075: }
076:
077: public int read() throws Db4oIOException {
078: return _downloadBuffer.read();
079: }
080:
081: public int read(byte[] a_bytes, int a_offset, int a_length)
082: throws Db4oIOException {
083: return _downloadBuffer.read(a_bytes, a_offset, a_length);
084: }
085:
086: public void setSoTimeout(int a_timeout) {
087: _uploadBuffer.setTimeout(a_timeout);
088: _downloadBuffer.setTimeout(a_timeout);
089: }
090:
091: public void write(byte[] bytes) throws Db4oIOException {
092: _uploadBuffer.write(bytes);
093: }
094:
095: public void write(byte[] bytes, int off, int len)
096: throws Db4oIOException {
097: _uploadBuffer.write(bytes, off, len);
098: }
099:
100: public void write(int i) throws Db4oIOException {
101: _uploadBuffer.write(i);
102: }
103:
104: public Socket4 openParalellSocket() throws Db4oIOException {
105: return _server.openClientSocket();
106: }
107: }
|