// ignore
}
return;
}
MessageBuffer msg = new MessageBuffer(buffer);
byte command = msg.getByte();
if (command == SimpleSgsProtocol.LOGIN_REQUEST) {
System.out.println("SimpleServer: Received LOGIN_REQUEST");
byte version = msg.getByte();
if (version != SimpleSgsProtocol.VERSION) {
System.out.println(
"SimpleServer: Version number mismatch: " + version
+ " " + SimpleSgsProtocol.VERSION);
return;
}
String username = msg.getString();
String password = msg.getString();
System.out.println("UserName: " + username + " Password: "
+ password);
MessageBuffer reply;
if (password.equals("guest") || redirect) {
byte[] reconnectKey = new byte[] {
0x1a, 0x1b, 0x1c, 0x1d, 0x30, 0x31, 0x32, 0x33
};
reply = new MessageBuffer(1 + reconnectKey.length);
reply.putByte(SimpleSgsProtocol.LOGIN_SUCCESS).
putBytes(reconnectKey);
} else if (password.equals("redirect")) {
redirect = true;
reply = new MessageBuffer(1 + MessageBuffer.getSize(host) + 4);
reply.putByte(SimpleSgsProtocol.LOGIN_REDIRECT).
putString(host).
putInt(port);
} else {
String reason = "Bad password";
reply =
new MessageBuffer(1 + MessageBuffer.getSize(reason));
reply.putByte(SimpleSgsProtocol.LOGIN_FAILURE).
putString(reason);
}
sendMessage(conn, reply.getBuffer());
} else if (command == SimpleSgsProtocol.SESSION_MESSAGE) {
String serverMessage = msg.getString();
System.out.println(
"SimpleServer: Received SESSION_MESSAGE: "
+ serverMessage);
if (serverMessage.equals("Join Channel")) {
startMessages(conn);
} else if (serverMessage.equals("Leave Channel")) {
stopMessages(conn);
} else if (serverMessage.equals("Relocate")) {
isRelocating = true;
relocatePort = msg.getInt();
suspendMessages(conn);
} else {
System.out.println("SimpleServer: Unknown server message: " +
serverMessage);
}
} else if (command == SimpleSgsProtocol.LOGOUT_REQUEST) {
System.out.println("SimpleServer: Received LOGOUT_REQUEST");
MessageBuffer reply = new MessageBuffer(1);
reply.putByte(SimpleSgsProtocol.LOGOUT_SUCCESS);
sendMessage(conn, reply.getBuffer());
} else if (command == SimpleSgsProtocol.SUSPEND_MESSAGES_COMPLETE) {
System.out.println(
"SimpleServer: Received SUSPEND_MESSAGES_COMPLETE");
isSuspendComplete = true;
if (isRelocating) {
relocate(conn);
}
} else if (command == SimpleSgsProtocol.RELOCATE_REQUEST) {
System.out.println("SimpleServer: Received RELOCATE_REQUEST");
byte version = msg.getByte();
if (version != SimpleSgsProtocol.VERSION) {
System.out.println("Version number mismatch: " + version
+ " " + SimpleSgsProtocol.VERSION);
return;
}
byte[] relocateKey = msg.getBytes(msg.limit() - msg.position());
if (!Arrays.equals(relocateKey, DEFAULT_RELOCATE_KEY)) {
System.out.println("SimpleServer: Invalid relocateKey: " +
HexDumper.toHexString(relocateKey));
return;
}
byte[] reconnectKey = new byte[] {
0x1a, 0x1b, 0x1c, 0x1d, 0x30, 0x31, 0x32, 0x33
};
isSuspended = false;
MessageBuffer reply = new MessageBuffer(1 + reconnectKey.length);
reply.putByte(SimpleSgsProtocol.RELOCATE_SUCCESS).
putBytes(reconnectKey);
sendMessage(conn, reply.getBuffer());
}
}