UnicastConnection.java [plain text]
package gnu.java.rmi.server;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.rmi.RemoteException;
public class UnicastConnection
implements Runnable, ProtocolConstants {
UnicastConnectionManager manager;
Socket sock;
DataInputStream din;
DataOutputStream dout;
ObjectInputStream oin;
ObjectOutputStream oout;
long reviveTime = 0;
long expireTime = Long.MAX_VALUE;
UnicastConnection(UnicastConnectionManager man, Socket sock) {
this.manager = man;
this.sock = sock;
}
void acceptConnection() throws IOException {
din = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
int sig = din.readInt();
if (sig != PROTOCOL_HEADER) {
throw new IOException("bad protocol header");
}
short ver = din.readShort();
if (ver != PROTOCOL_VERSION) {
throw new IOException("bad protocol version");
}
int protocol = din.readUnsignedByte();
if (protocol != SINGLE_OP_PROTOCOL) {
dout.writeByte(PROTOCOL_ACK);
dout.writeUTF(manager.serverName);
dout.writeInt(manager.serverPort);
dout.flush();
String rhost = din.readUTF();
int rport = din.readInt();
}
}
void makeConnection(int protocol) throws IOException {
din = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
dout = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
dout.writeInt(PROTOCOL_HEADER);
dout.writeShort(PROTOCOL_VERSION);
dout.writeByte(protocol);
dout.flush();
if (protocol != SINGLE_OP_PROTOCOL) {
int ack = din.readUnsignedByte();
if (ack != PROTOCOL_ACK) {
throw new RemoteException("Unsupported protocol");
}
String dicard_rhost = din.readUTF();
int discard_rport = din.readInt();
dout.writeUTF(manager.serverName);
dout.writeInt(manager.serverPort);
dout.flush();
}
}
DataInputStream getDataInputStream() throws IOException {
return (din);
}
DataOutputStream getDataOutputStream() throws IOException {
return (dout);
}
ObjectInputStream getObjectInputStream() throws IOException {
if (oin == null) {
throw new IOException("no ObjectInputtream for reading more objects");
}
return (oin);
}
ObjectInputStream startObjectInputStream() throws IOException {
return (oin = new RMIObjectInputStream(din));
}
ObjectOutputStream getObjectOutputStream() throws IOException {
if (oout == null) {
throw new IOException("no ObjectOutputStream for sending more objects");
}
return (oout);
}
ObjectOutputStream startObjectOutputStream() throws IOException {
return (oout = new RMIObjectOutputStream(dout));
}
void disconnect() {
try {
if(oout != null)
oout.close();
sock.close();
}
catch (IOException _) {
}
oin = null;
oout = null;
din = null;
dout = null;
sock = null;
}
public static final long CONNECTION_TIMEOUT = 10000L;
static boolean isExpired(UnicastConnection conn, long l){
if (l <= conn.expireTime )
return false;
return true;
}
static void resetTime(UnicastConnection conn){
long l = System.currentTimeMillis();
conn.reviveTime = l;
conn.expireTime = l + CONNECTION_TIMEOUT;
}
public void run() {
do{
try {
UnicastServer.dispatch(this);
} catch (Exception e ){
manager.discardConnection(this);
break;
}
}while(true);
}
}