Merge from mainline.
[official-gcc.git] / libjava / classpath / gnu / java / rmi / server / UnicastConnection.java
blob216e453e72fe12aa15cc1580fb2b8359ba8ab7e4
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)
10 any later version.
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
20 02110-1301 USA.
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.
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;
56 Socket sock;
57 DataInputStream din;
58 DataOutputStream dout;
59 ObjectInputStream oin;
60 ObjectOutputStream oout;
62 // reviveTime and expireTime make UnicastConnection pool-able
63 long reviveTime = 0;
64 long expireTime = Long.MAX_VALUE;
66 UnicastConnection(UnicastConnectionManager man, Socket sock) {
67 this.manager = man;
68 this.sock = 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) {
87 // Send an ACK
88 dout.writeByte(PROTOCOL_ACK);
90 // Send my hostname and port
91 dout.writeUTF(manager.serverName);
92 dout.writeInt(manager.serverPort);
93 dout.flush();
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()));
108 // Send header
109 dout.writeInt(PROTOCOL_HEADER);
110 dout.writeShort(PROTOCOL_VERSION);
111 dout.writeByte(protocol);
112 dout.flush();
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");
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);
128 dout.flush();
130 // Okay, ready to roll ...
133 DataInputStream getDataInputStream() throws IOException {
134 return (din);
137 DataOutputStream getDataOutputStream() throws IOException {
138 return (dout);
143 * get ObjectInputStream for reading more objects
146 ObjectInputStream getObjectInputStream() throws IOException {
147 if (oin == null) {
148 throw new IOException("no ObjectInputtream for reading more objects");
150 return (oin);
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 {
168 if (oout == null) {
169 throw new IOException("no ObjectOutputStream for sending more objects");
171 return (oout);
176 * starts ObjectOutputStream.
179 ObjectOutputStream startObjectOutputStream() throws IOException {
180 return (oout = new RMIObjectOutputStream(dout));
183 void disconnect() {
184 try {
185 if(oout != null)
186 oout.close();
187 sock.close();
189 catch (IOException _) {
192 oin = null;
193 oout = null;
194 din = null;
195 dout = null;
196 sock = null;
199 public static final long CONNECTION_TIMEOUT = 10000L;
201 static boolean isExpired(UnicastConnection conn, long l){
202 if (l <= conn.expireTime )
203 return false;
204 return true;
207 static void resetTime(UnicastConnection conn){
208 long l = System.currentTimeMillis();
209 conn.reviveTime = l;
210 conn.expireTime = l + CONNECTION_TIMEOUT;
214 * We run connects on the server. Dispatch it then discard it.
216 public void run() {
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;
227 }while(true);