1 /* UnicastConnection.java --
2 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
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)
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.
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
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
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. */
40 package gnu
.java
.rmi
.server
;
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
;
52 public class UnicastConnection
53 implements Runnable
, ProtocolConstants
{
55 UnicastConnectionManager manager
;
58 DataOutputStream dout
;
59 ObjectInputStream oin
;
60 ObjectOutputStream oout
;
62 // reviveTime and expireTime make UnicastConnection pool-able
64 long expireTime
= Long
.MAX_VALUE
;
66 UnicastConnection(UnicastConnectionManager man
, Socket sock
) {
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()));
77 int sig
= din
.readInt();
78 if (sig
!= PROTOCOL_HEADER
) {
79 throw new IOException("bad protocol header");
81 short ver
= din
.readShort();
82 if (ver
!= PROTOCOL_VERSION
) {
83 throw new IOException("bad protocol version");
85 int protocol
= din
.readUnsignedByte();
86 if (protocol
!= SINGLE_OP_PROTOCOL
) {
88 dout
.writeByte(PROTOCOL_ACK
);
90 // Send my hostname and port
91 dout
.writeUTF(manager
.serverName
);
92 dout
.writeInt(manager
.serverPort
);
95 // Read their hostname and port
96 String rhost
= din
.readUTF();
97 int rport
= din
.readInt();
99 // Okay, ready to roll ...
102 void makeConnection(int protocol
) throws IOException
{
103 //Use BufferedXXXStream would be more efficient
104 din
= new DataInputStream(new BufferedInputStream(sock
.getInputStream()));
106 dout
= new DataOutputStream(new BufferedOutputStream(sock
.getOutputStream()));
109 dout
.writeInt(PROTOCOL_HEADER
);
110 dout
.writeShort(PROTOCOL_VERSION
);
111 dout
.writeByte(protocol
);
114 if (protocol
!= SINGLE_OP_PROTOCOL
) {
116 int ack
= din
.readUnsignedByte();
117 if (ack
!= PROTOCOL_ACK
) {
118 throw new RemoteException("Unsupported protocol");
121 // Read in host and port
122 String dicard_rhost
= din
.readUTF();
123 int discard_rport
= din
.readInt();
125 // Send them my endpoint
126 dout
.writeUTF(manager
.serverName
);
127 dout
.writeInt(manager
.serverPort
);
130 // Okay, ready to roll ...
133 DataInputStream
getDataInputStream() throws IOException
{
137 DataOutputStream
getDataOutputStream() throws IOException
{
143 * get ObjectInputStream for reading more objects
146 ObjectInputStream
getObjectInputStream() throws IOException
{
148 throw new IOException("no ObjectInputtream for reading more objects");
155 * starts ObjectInputStream.
158 ObjectInputStream
startObjectInputStream() throws IOException
{
159 return (oin
= new RMIObjectInputStream(din
));
164 * get ObjectOutputStream for sending more objects
167 ObjectOutputStream
getObjectOutputStream() throws IOException
{
169 throw new IOException("no ObjectOutputStream for sending more objects");
176 * starts ObjectOutputStream.
179 ObjectOutputStream
startObjectOutputStream() throws IOException
{
180 return (oout
= new RMIObjectOutputStream(dout
));
189 catch (IOException _
) {
199 public static final long CONNECTION_TIMEOUT
= 10000L;
201 static boolean isExpired(UnicastConnection conn
, long l
){
202 if (l
<= conn
.expireTime
)
207 static void resetTime(UnicastConnection conn
){
208 long l
= System
.currentTimeMillis();
210 conn
.expireTime
= l
+ CONNECTION_TIMEOUT
;
214 * We run connects on the server. Dispatch it then discard it.
219 UnicastServer
.dispatch(this);
220 //don't discardConnection explicitly, only when
221 // exception happens or the connection's expireTime
223 } catch (Exception e
){
224 manager
.discardConnection(this);