1 package org.sapia.ubik.rmi.server.transport.nio.tcp; 2 3 import java.io.IOException ; 4 import java.rmi.RemoteException ; 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 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 47 public void close() { 48 } 49 50 53 public ServerAddress getServerAddress() { 54 return _addr; 55 } 56 57 60 public Object receive() throws IOException , ClassNotFoundException , 61 RemoteException { 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 73 public void send(Object o) throws IOException , RemoteException { 74 send(o, null, null); 75 } 76 77 81 public void send(Object o, VmId associated, String transportType) 82 throws IOException , RemoteException { 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 |