| 1 package org.sapia.ubik.net; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedOutputStream ; 5 import java.io.EOFException ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.io.ObjectInputStream ; 9 import java.io.ObjectOutputStream ; 10 import java.io.OutputStream ; 11 12 import java.net.Socket ; 13 import java.net.SocketException ; 14 15 import java.rmi.RemoteException ; 16 17 18 28 public class SocketConnection implements Connection { 29 static final long DEFAULT_RESET_INTERVAL = 0; 30 protected Socket _sock; 31 protected TCPAddress _address; 32 protected ClassLoader _loader; 33 protected ObjectInputStream _is; 34 protected ObjectOutputStream _os; 35 protected long _lastReset = System.currentTimeMillis(); 36 protected long _resetInterval = DEFAULT_RESET_INTERVAL; 37 38 public SocketConnection(Socket sock, ClassLoader loader) { 39 this(sock); 40 _loader = loader; 41 } 42 43 public SocketConnection(Socket sock) { 44 _sock = sock; 45 _address = new TCPAddress(sock.getInetAddress().getHostAddress(), 46 sock.getPort()); 47 } 48 49 60 public void setResetInterval(long interval){ 61 _resetInterval = interval; 62 } 63 64 67 public void send(Object o) throws IOException , RemoteException { 68 try { 69 if (_os == null) { 70 _os = newOutputStream(new BufferedOutputStream ( 71 _sock.getOutputStream(), 1000), _loader); 72 } 73 74 _os.writeObject(o); 75 76 77 if ((System.currentTimeMillis() - _lastReset) >= DEFAULT_RESET_INTERVAL) { 78 _os.reset(); 79 _lastReset = System.currentTimeMillis(); 80 } 81 82 _os.flush(); 83 84 } catch (java.net.SocketException e) { 85 throw new RemoteException ("Communication with server interrupted; server probably disappeared", 86 e); 87 } catch (EOFException e) { 88 throw new RemoteException ("Communication with server interrupted; server probably disappeared", 89 e); 90 } 91 } 92 93 96 public Object receive() 97 throws IOException , ClassNotFoundException , RemoteException { 98 try { 99 if (_is == null) { 100 _is = newInputStream(new BufferedInputStream (_sock.getInputStream(), 101 1000), _loader); 102 } 103 104 return _is.readObject(); 105 } catch (EOFException e) { 106 throw new RemoteException ("Communication with server interrupted; server probably disappeared", 107 e); 108 } catch (SocketException e) { 109 throw new RemoteException ("Connection could not be opened; server is probably down", 110 e); 111 } 112 } 113 114 117 public void close() { 118 try { 119 if (_os != null) { 120 _os.reset(); 121 _os.close(); 122 _os = null; 123 } 124 125 if (_is != null) { 126 _is.close(); 127 _is = null; 128 } 129 130 _sock.close(); 131 } catch (Throwable t) { 132 } 134 } 135 136 139 public ServerAddress getServerAddress() { 140 return _address; 141 } 142 143 148 public InputStream getInputStream() throws IOException { 149 return _sock.getInputStream(); 150 } 151 152 157 public OutputStream getOuputStream() throws IOException { 158 return _sock.getOutputStream(); 159 } 160 161 170 protected ObjectOutputStream newOutputStream(OutputStream os, 171 ClassLoader loader) throws IOException { 172 return new ObjectOutputStream (os); 173 } 174 175 189 protected ObjectInputStream newInputStream(InputStream is, ClassLoader loader) 190 throws IOException { 191 return new ObjectInputStream (is); 192 } 193 } 194 | Popular Tags |