// Already registered?
if (connection.name != null)
{
logMessage("Discarded login from " + c.getID() + " (already logged in as " + connection.name + ")");
Disconnect dc = new Disconnect();
dc.reason = Reason.AlreadyLoggedIn;
sendToTCP(connection.getID(), dc);
connection.close();
return;
}
// Invalid name?
String name = ((Login) object).name;
if (name == null)
{
logMessage("Discarded login from " + c.getRemoteAddressTCP() + " (name missing)");
Disconnect dc = new Disconnect();
dc.reason = Reason.NoName;
sendToTCP(connection.getID(), dc);
connection.close();
return;
}
name = name.trim();
if (name.length() == 0)
{
logMessage("Discarded login from " + c.getRemoteAddressTCP() + " (name has zero length)");
Disconnect dc = new Disconnect();
dc.reason = Reason.ZeroNameLength;
sendToTCP(connection.getID(), dc);
connection.close();
return;
}
// Store the name on the connection
connection.name = name;
// TODO: verify connection with ServerEventHandler
clientConnected(connection);
// Replicate existing nodes
replicateAllNodes(connection.getID());
// Notify users
UserConnect uc = new UserConnect();
uc.name = connection.name;
sendToAllTCP(uc);
logMessage("Accepted login from " + c.getRemoteAddressTCP() + ": " + name);
return;
}
// Ignore any other messages by unregistered users
if (connection.name == null)
{
// TODO: disconnect user?
logMessage("Disconnected " + c.getRemoteAddressTCP() + " (not registered)");
Disconnect dc = new Disconnect();
dc.reason = Reason.DidNotRegister;
sendToTCP(connection.getID(), dc);
connection.close();
return;