KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server.transport.nio.tcp;
2
3 import java.io.IOException JavaDoc;
4 import java.rmi.RemoteException JavaDoc;
5
6 import org.sapia.ubik.net.ServerAddress;
7 import org.sapia.ubik.rmi.server.VmId;
8 import org.sapia.ubik.rmi.server.transport.MarshalInputStream;
9 import org.sapia.ubik.rmi.server.transport.MarshalOutputStream;
10 import org.sapia.ubik.rmi.server.transport.RmiConnection;
11 import org.sapia.ubik.util.ByteVector;
12 import org.sapia.ubik.util.ByteVectorInputStream;
13 import org.sapia.ubik.util.ByteVectorOutputStream;
14
15 /**
16  * @author Yanick Duchesne
17  *
18  * <dl>
19  * <dt><b>Copyright: </b>
20  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
21  * Source Software </a>. All Rights Reserved.</dd>
22  * </dt>
23  * <dt><b>License: </b>
24  * <dd>Read the license.txt file of the jar or visit the <a
25  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
26  * OSS web site</dd>
27  * </dt>
28  * </dl>
29  */

30 public class NioTcpRmiServerConnection implements RmiConnection {
31
32   private RmiChannelHandler _handler;
33   private ServerAddress _addr;
34   private MarshalInputStream _mis;
35   private MarshalOutputStream _mos;
36   private long _lastReset = System.currentTimeMillis();
37   static final long RESET_INTERVAL = 2000;
38
39   NioTcpRmiServerConnection(ServerAddress addr, RmiChannelHandler owner) {
40     _addr = addr;
41     _handler = owner;
42   }
43
44   /**
45    * @see org.sapia.ubik.net.Connection#close()
46    */

47   public void close() {
48   }
49
50   /**
51    * @see org.sapia.ubik.net.Connection#getServerAddress()
52    */

53   public ServerAddress getServerAddress() {
54     return _addr;
55   }
56
57   /**
58    * @see org.sapia.ubik.net.Connection#receive()
59    */

60   public Object JavaDoc receive() throws IOException JavaDoc, ClassNotFoundException JavaDoc,
61       RemoteException JavaDoc {
62     ByteVector data = _handler.getContent();
63     data.reset();
64     if(_mis == null){
65       _mis = new MarshalInputStream(new ByteVectorInputStream(data));
66     }
67     return _mis.readObject();
68   }
69
70   /**
71    * @see org.sapia.ubik.net.Connection#send(java.lang.Object)
72    */

73   public void send(Object JavaDoc o) throws IOException JavaDoc, RemoteException JavaDoc {
74     send(o, null, null);
75   }
76
77   /**
78    * @see org.sapia.ubik.rmi.server.transport.RmiConnection#send(java.lang.Object,
79    * org.sapia.ubik.rmi.server.VmId, java.lang.String)
80    */

81   public void send(Object JavaDoc o, VmId associated, String JavaDoc transportType)
82       throws IOException JavaDoc, RemoteException JavaDoc {
83     if((System.currentTimeMillis() - _lastReset) >= RESET_INTERVAL && _mos != null) {
84       _mos.reset();
85       _lastReset = System.currentTimeMillis();
86     }
87
88     if(_mos == null){
89       ByteVectorOutputStream out = new ByteVectorOutputStream(_handler.getByteVector());
90       _handler.getByteVector().clear(false);
91       _mos = new MarshalOutputStream(out);
92     }
93     else{
94       _handler.getByteVector().clear(false);
95     }
96     
97     if(associated != null && transportType != null) {
98       _mos.setUp(associated, transportType);
99     }
100     
101     _mos.writeObject(o);
102     _mos.flush();
103   }
104 }
105
Popular Tags