Source for gnu.java.rmi.server.UnicastConnection

   1: /* UnicastConnection.java --
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004
   3:    Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11: 
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: 
  40: package gnu.java.rmi.server;
  41: 
  42: import java.io.BufferedInputStream;
  43: import java.io.BufferedOutputStream;
  44: import java.io.DataInputStream;
  45: import java.io.DataOutputStream;
  46: import java.io.IOException;
  47: import java.io.ObjectInputStream;
  48: import java.io.ObjectOutputStream;
  49: import java.net.Socket;
  50: import java.rmi.RemoteException;
  51: 
  52: public class UnicastConnection
  53:         implements Runnable, ProtocolConstants {
  54: 
  55: UnicastConnectionManager manager;
  56: Socket sock;
  57: DataInputStream din;
  58: DataOutputStream dout;
  59: ObjectInputStream oin;
  60: ObjectOutputStream oout;
  61: 
  62: // reviveTime and expireTime make UnicastConnection pool-able
  63: long reviveTime = 0;
  64: long expireTime = Long.MAX_VALUE;
  65: 
  66: UnicastConnection(UnicastConnectionManager man, Socket sock) {
  67:         this.manager = man;
  68:         this.sock = sock;
  69: }
  70: 
  71: void acceptConnection() throws IOException {
  72: //System.out.println("Accepting connection on " + sock);
  73:     //Use BufferedXXXStream would be more efficient
  74:         din = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
  75:         dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
  76: 
  77:         int sig = din.readInt();
  78:         if (sig != PROTOCOL_HEADER) {
  79:                 throw new IOException("bad protocol header");
  80:         }
  81:         short ver = din.readShort();
  82:         if (ver != PROTOCOL_VERSION) {
  83:                 throw new IOException("bad protocol version");
  84:         }
  85:         int protocol = din.readUnsignedByte();
  86:         if (protocol != SINGLE_OP_PROTOCOL) {
  87:                 // Send an ACK
  88:                 dout.writeByte(PROTOCOL_ACK);
  89: 
  90:                 // Send my hostname and port
  91:                 dout.writeUTF(manager.serverName);
  92:                 dout.writeInt(manager.serverPort);
  93:                 dout.flush();
  94: 
  95:                 // Read their hostname and port
  96:                 String rhost = din.readUTF();
  97:                 int rport = din.readInt();
  98:         }
  99:         // Okay, ready to roll ...
 100: }
 101: 
 102: void makeConnection(int protocol) throws IOException {
 103:     //Use BufferedXXXStream would be more efficient
 104:         din = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
 105: 
 106:         dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
 107: 
 108:         // Send header
 109:         dout.writeInt(PROTOCOL_HEADER);
 110:         dout.writeShort(PROTOCOL_VERSION);
 111:         dout.writeByte(protocol);
 112:     dout.flush();
 113: 
 114:         if (protocol != SINGLE_OP_PROTOCOL) {
 115:                 // Get back ack.
 116:                 int ack = din.readUnsignedByte();
 117:                 if (ack != PROTOCOL_ACK) {
 118:                         throw new RemoteException("Unsupported protocol");
 119:                 }
 120: 
 121:                 // Read in host and port
 122:                 String dicard_rhost = din.readUTF();
 123:                 int discard_rport = din.readInt();
 124: 
 125:                 // Send them my endpoint
 126:                 dout.writeUTF(manager.serverName);
 127:                 dout.writeInt(manager.serverPort);
 128:                 dout.flush();
 129:         }
 130:         // Okay, ready to roll ...
 131: }
 132: 
 133: DataInputStream getDataInputStream() throws IOException {
 134:         return (din);
 135: }
 136: 
 137: DataOutputStream getDataOutputStream() throws IOException {
 138:         return (dout);
 139: }
 140: 
 141: /*
 142: *
 143: * get ObjectInputStream for reading more objects
 144: *
 145: */
 146: ObjectInputStream getObjectInputStream() throws IOException {
 147:         if (oin == null) {
 148:                 throw new IOException("no ObjectInputtream for reading more objects");
 149:         }
 150:         return (oin);
 151: }
 152: 
 153: /**
 154: *
 155: * starts ObjectInputStream.
 156: *
 157: */
 158: ObjectInputStream startObjectInputStream() throws IOException {
 159:         return (oin = new RMIObjectInputStream(din));
 160: }
 161: 
 162: /**
 163: *
 164: * get ObjectOutputStream for sending more objects
 165: *
 166: */
 167: ObjectOutputStream getObjectOutputStream() throws IOException {
 168:         if (oout == null) {
 169:                 throw new IOException("no ObjectOutputStream for sending more objects");
 170:         }
 171:         return (oout);
 172: }
 173: 
 174: /**
 175: *
 176: * starts ObjectOutputStream.
 177: *
 178: */
 179: ObjectOutputStream startObjectOutputStream() throws IOException {
 180:         return (oout = new RMIObjectOutputStream(dout));
 181: }
 182: 
 183: void disconnect() {
 184:         try {
 185:             if(oout != null)
 186:                 oout.close();
 187:         sock.close();
 188:         }
 189:         catch (IOException _) {
 190:     }
 191: 
 192:         oin = null;
 193:     oout = null;
 194:         din = null;
 195:         dout = null;
 196:         sock = null;
 197: }
 198: 
 199: public static final long CONNECTION_TIMEOUT = 10000L;
 200: 
 201: static boolean isExpired(UnicastConnection conn, long l){
 202:     if (l <= conn.expireTime )
 203:         return false;
 204:     return true;
 205: }
 206: 
 207: static void resetTime(UnicastConnection conn){
 208:     long l = System.currentTimeMillis();
 209:     conn.reviveTime = l;
 210:     conn.expireTime = l + CONNECTION_TIMEOUT;
 211: }
 212: 
 213: /**
 214:  * We run connects on the server. Dispatch it then discard it.
 215:  */
 216: public void run() {
 217:     do{
 218:         try {
 219:                 UnicastServer.dispatch(this);
 220:             //don't discardConnection explicitly, only when
 221:             //  exception happens or the connection's expireTime
 222:             //  comes
 223:         } catch (Exception e ){
 224:                 manager.discardConnection(this);
 225:             break;
 226:         }
 227:     }while(true);
 228: }
 229: 
 230: 
 231: }