KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > network > YapSocketFake


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.foundation.network;
22
23 import java.io.*;
24
25 /**
26  * Fakes a socket connection for an embedded client.
27  */

28 public class YapSocketFake implements YapSocket {
29     
30     private final YapSocketFakeServer _server;
31
32     private YapSocketFake _affiliate;
33     private ByteBuffer4 _uploadBuffer;
34     private ByteBuffer4 _downloadBuffer;
35
36     public YapSocketFake(YapSocketFakeServer a_server, int timeout) {
37         _server = a_server;
38         _uploadBuffer = new ByteBuffer4(timeout);
39         _downloadBuffer = new ByteBuffer4(timeout);
40     }
41
42     public YapSocketFake(YapSocketFakeServer a_server, int timeout, YapSocketFake affiliate) {
43         this(a_server, timeout);
44         _affiliate = affiliate;
45         affiliate._affiliate = this;
46         _downloadBuffer = affiliate._uploadBuffer;
47         _uploadBuffer = affiliate._downloadBuffer;
48     }
49
50     public void close() throws IOException {
51         if (_affiliate != null) {
52             YapSocketFake temp = _affiliate;
53             _affiliate = null;
54             temp.close();
55         }
56         _affiliate = null;
57     }
58
59     public void flush() {
60         // do nothing
61
}
62
63     public boolean isConnected() {
64         return _affiliate != null;
65     }
66
67     public int read() throws IOException {
68         return _downloadBuffer.read();
69     }
70
71     public int read(byte[] a_bytes, int a_offset, int a_length) throws IOException {
72         return _downloadBuffer.read(a_bytes, a_offset, a_length);
73     }
74
75     public void setSoTimeout(int a_timeout) {
76         _uploadBuffer.setTimeout(a_timeout);
77         _downloadBuffer.setTimeout(a_timeout);
78     }
79
80     public void write(byte[] bytes) throws IOException {
81         _uploadBuffer.write(bytes);
82     }
83     
84     public void write(byte[] bytes,int off,int len) throws IOException {
85         _uploadBuffer.write(bytes, off, len);
86     }
87
88     public void write(int i) throws IOException {
89         _uploadBuffer.write(i);
90     }
91     
92     public YapSocket openParalellSocket() throws IOException {
93         return _server.openClientSocket();
94     }
95 }
96
Popular Tags