KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > nio > tcp > NioTcpRmiClientConnection


1 package org.sapia.ubik.rmi.server.transport.nio.tcp;
2
3 import java.io.DataInputStream JavaDoc;
4 import java.io.DataOutputStream JavaDoc;
5 import java.io.EOFException JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.net.Socket JavaDoc;
9 import java.net.SocketException JavaDoc;
10 import java.rmi.RemoteException JavaDoc;
11
12 import org.sapia.ubik.net.ServerAddress;
13 import org.sapia.ubik.rmi.server.VmId;
14 import org.sapia.ubik.rmi.server.transport.MarshalInputStream;
15 import org.sapia.ubik.rmi.server.transport.MarshalOutputStream;
16 import org.sapia.ubik.rmi.server.transport.RmiConnection;
17 import org.sapia.ubik.util.ByteVector;
18 import org.sapia.ubik.util.ByteVectorOutputStream;
19
20 /**
21  * A connection over a <code>Socket</code>- the connection uses the
22  * <code>MarshalOutputStream</code> class to serialize outgoing objects.
23  *
24  * @author Yanick Duchesne
25  * <dl>
26  * <dt><b>Copyright: </b>
27  * <dd>Copyright &#169; 2002-2003 <a
28  * HREF="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
29  * Rights Reserved.</dd>
30  * </dt>
31  * <dt><b>License: </b>
32  * <dd>Read the license.txt file of the jar or visit the <a
33  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the
34  * Sapia OSS web site</dd>
35  * </dt>
36  * </dl>
37  */

38 public class NioTcpRmiClientConnection implements RmiConnection {
39   static final long RESET_INTERVAL = 2000;
40   static final int BUFSZ = 1000;
41   private Socket JavaDoc _sock;
42   private ByteVector _bytes;
43   private MarshalOutputStream _mos;
44   private MarshalInputStream _mis;
45   private DataInputStream JavaDoc _is;
46   private byte[] _readBuf, _headers;
47   private long _lastReset = System.currentTimeMillis();
48   private ServerAddress _address;
49
50   /**
51    * Constructor for RMIConnection.
52    *
53    * @param sock
54    */

55   public NioTcpRmiClientConnection(Socket JavaDoc sock, int bufsize) throws IOException JavaDoc {
56     _sock = sock;
57     _address = new NioAddress(sock.getInetAddress().getHostAddress(), sock
58         .getPort());
59     _bytes = new ByteVector(bufsize, 10);
60     _readBuf = new byte[bufsize];
61   }
62
63   /**
64    * @see org.sapia.ubik.net.Connection#getServerAddress()
65    */

66   public ServerAddress getServerAddress() {
67     return _address;
68   }
69
70   /**
71    * @see org.sapia.ubik.rmi.server.transport.RmiConnection#send(Object, VmId,
72    * String)
73    */

74   public void send(Object JavaDoc o, VmId vmId, String JavaDoc tranportType)
75       throws IOException JavaDoc, RemoteException JavaDoc {
76     try {
77       _mos.setUp(vmId, tranportType);
78       send(o);
79     } catch(java.net.SocketException JavaDoc e) {
80       throw new RemoteException JavaDoc(
81           "communication with server interrupted; server probably disappeared",
82           e);
83     }
84   }
85
86   /**
87    * @see org.sapia.ubik.net.Connection#send(java.lang.Object)
88    */

89   public void send(Object JavaDoc o) throws IOException JavaDoc, RemoteException JavaDoc {
90     _bytes.clear(false);
91     if((System.currentTimeMillis() - _lastReset) >= RESET_INTERVAL && _mos != null) {
92       _mos.reset();
93       _lastReset = System.currentTimeMillis();
94     }
95     if(_mos == null){
96       _mos = new MarshalOutputStream(new ByteVectorOutputStream(_bytes));
97     }
98     _mos.writeObject(o);
99     _mos.flush();
100     _bytes.reset();
101     
102     OutputStream JavaDoc sos = _sock.getOutputStream();
103     DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(sos);
104     dos.writeInt(_bytes.length());
105     _bytes.read(dos);
106     dos.flush();
107   }
108
109   /**
110    * @see org.sapia.ubik.net.Connection#receive()
111    */

112   public Object JavaDoc receive() throws IOException JavaDoc, ClassNotFoundException JavaDoc,
113       RemoteException JavaDoc {
114     try {
115       _bytes.clear(false);
116       if(_is == null){
117         _is = new DataInputStream JavaDoc(_sock.getInputStream());
118         _is.readInt();
119         _mis = new MarshalInputStream(_is);
120       }
121       else{
122         _is.readInt();
123       }
124       return _mis.readObject();
125     } catch(EOFException JavaDoc e) {
126       throw new RemoteException JavaDoc(
127           "Communication with server interrupted; server probably disappeared",
128           e);
129     } catch(SocketException JavaDoc e) {
130       throw new RemoteException JavaDoc(
131           "Connection could not be opened; server is probably down", e);
132     }
133   }
134
135   /**
136    * @see org.sapia.ubik.net.Connection#close()
137    */

138   public void close() {
139     try {
140       _sock.close();
141     } catch(Throwable JavaDoc t) {
142       //noop
143
}
144   }
145 }
146
Popular Tags