import java.net.Socket; //导入方法依赖的package包/类
/**
* HandShake Constructor used by server side connection
*/
public HandShake(Socket sock, int timeout, DistributedSystem sys, Version clientVersion,
byte communicationMode) throws IOException, AuthenticationRequiredException {
this.clientVersion = clientVersion;
this.system = sys;
// SocketChannel sc = sock.getChannel();
/*
* if (sc != null) { } else
*/ {
int soTimeout = -1;
try {
soTimeout = sock.getSoTimeout();
sock.setSoTimeout(timeout);
InputStream is = sock.getInputStream();
int valRead = is.read();
// this.code = (byte)is.read();
if (valRead == -1) {
throw new EOFException(
LocalizedStrings.HandShake_HANDSHAKE_EOF_REACHED_BEFORE_CLIENT_CODE_COULD_BE_READ
.toLocalizedString());
}
this.code = (byte) valRead;
if (this.code != REPLY_OK) {
throw new IOException(
LocalizedStrings.HandShake_HANDSHAKE_REPLY_CODE_IS_NOT_OK.toLocalizedString());
}
try {
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
this.clientReadTimeout = dis.readInt();
if (clientVersion.compareTo(Version.CURRENT) < 0) {
// versioned streams allow object serialization code to deal with older clients
dis = new VersionedDataInputStream(dis, clientVersion);
dos = new VersionedDataOutputStream(dos, clientVersion);
}
this.id = ClientProxyMembershipID.readCanonicalized(dis);
// Note: credentials should always be the last piece in handshake for
// Diffie-Hellman key exchange to work
if (clientVersion.compareTo(Version.GFE_603) >= 0) {
setOverrides(new byte[] {dis.readByte()});
} else {
setClientConflation(dis.readByte());
}
// Hitesh
if (this.clientVersion.compareTo(Version.GFE_65) < 0
|| communicationMode == Acceptor.GATEWAY_TO_GATEWAY) {
this.credentials = readCredentials(dis, dos, sys);
} else {
this.credentials = this.readCredential(dis, dos, sys);
}
} catch (IOException ioe) {
this.code = -2;
throw ioe;
} catch (ClassNotFoundException cnfe) {
this.code = -3;
throw new IOException(
LocalizedStrings.HandShake_CLIENTPROXYMEMBERSHIPID_CLASS_COULD_NOT_BE_FOUND_WHILE_DESERIALIZING_THE_OBJECT
.toLocalizedString());
}
} finally {
if (soTimeout != -1) {
try {
sock.setSoTimeout(soTimeout);
} catch (IOException ignore) {
}
}
}
}
}