1 package org.sapia.ubik.rmi.server.transport.http; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 import java.io.OutputStream ; 6 import java.rmi.RemoteException ; 7 8 import org.sapia.ubik.net.ServerAddress; 9 import org.sapia.ubik.rmi.server.VmId; 10 import org.sapia.ubik.rmi.server.transport.MarshalInputStream; 11 import org.sapia.ubik.rmi.server.transport.MarshalOutputStream; 12 import org.sapia.ubik.rmi.server.transport.RmiConnection; 13 14 import simple.http.Request; 15 import simple.http.Response; 16 17 18 34 class HttpRmiServerConnection implements RmiConnection { 35 static final int DEFAULT_BUFSZ = 1024; 36 private int _bufsz = DEFAULT_BUFSZ; 37 private Request _req; 38 private Response _res; 39 private HttpAddress _address; 40 41 45 HttpRmiServerConnection(HttpAddress address, Request req, Response res) { 46 _req = req; 47 _res = res; 48 _address = address; 49 } 50 51 54 public void send(Object o, VmId associated, String transportType) 55 throws IOException , RemoteException { 56 try { 57 ByteArrayOutputStream bos = new ByteArrayOutputStream (_bufsz); 58 MarshalOutputStream mos = new MarshalOutputStream(bos); 59 60 if ((associated != null) && (transportType != null)) { 61 mos.setUp(associated, transportType); 62 } 63 64 mos.writeObject(o); 65 mos.flush(); 66 mos.close(); 67 68 byte[] data = bos.toByteArray(); 69 70 if (data.length > _bufsz) { 71 _bufsz = data.length; 72 } 73 74 _res.setContentLength(data.length); 75 76 OutputStream os = _res.getOutputStream(data.length); 77 os.write(data); 78 os.flush(); 79 os.close(); 80 } catch (java.net.SocketException e) { 81 throw new RemoteException ("communication with server interrupted; server probably disappeared", 82 e); 83 } 84 } 85 86 89 public void close() { 90 try { 91 _res.commit(); 92 } catch (IOException e) { 93 } 95 } 96 97 100 public ServerAddress getServerAddress() { 101 return _address; 102 } 103 104 107 public Object receive() 108 throws IOException , ClassNotFoundException , RemoteException { 109 MarshalInputStream is = new MarshalInputStream(_req.getInputStream()); 110 111 return is.readObject(); 112 } 113 114 117 public void send(Object o) throws IOException , RemoteException { 118 try { 119 MarshalOutputStream os = new MarshalOutputStream(_res.getOutputStream()); 120 os.writeObject(o); 121 os.flush(); 122 } catch (java.net.SocketException e) { 123 throw new RemoteException ("communication with server interrupted; server probably disappeared", 124 e); 125 } 126 } 127 } 128 | Popular Tags |