socket.setSoTimeout(params.SoTimeout);
if (params.SoLinger >= 0)
socket.setSoLinger(true, params.SoLinger);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
// Writes the Joram magic number
baos.write(MetaData.joramMagic);
// Writes the current date
StreamUtil.writeTo(System.currentTimeMillis(), baos);
// Writes the user identity
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> write identity = " + identity);
Identity.write(identity, baos);
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> write key = " + key);
StreamUtil.writeTo(key, baos);
if (key == -1) {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> connection opened, initializes new connection");
StreamUtil.writeTo(reconnectTimeout, baos);
baos.writeTo(os);
os.flush();
int len = StreamUtil.readIntFrom(is);
long dt = StreamUtil.readLongFrom(is);
if (dt > clockSynchroThreshold)
logger.log(BasicLevel.WARN, " -> bad clock synchronization between client and server: " + dt);
int res = StreamUtil.readIntFrom(is);
if (res > 0) {
String info = StreamUtil.readStringFrom(is);
throwSecurityError(info);
}
key = StreamUtil.readIntFrom(is);
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> key = " + identity.getUserName() + ',' + key);
connection = new ReliableTcpConnection(timer);
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> init reliable connection");
} else {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> reinitializes connection " + identity + ',' + key);
baos.writeTo(os);
os.flush();
int len = StreamUtil.readIntFrom(is);
int res = StreamUtil.readIntFrom(is);
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> read res = " + res);