Class Server

java.lang.Object
  extended by Server
All Implemented Interfaces:
Login

public class Server
extends java.lang.Object
implements Login

Represents a Java Messenger server.


Constructor Summary
Server()
          Constructs a new server.
 
Method Summary
 int addUser(java.lang.String name, java.lang.String password)
          Tries to register a new user with a given screen name and password.
 int login(java.lang.String name, java.lang.String password)
          Tries to login a user with a given screen name and password.
 void logout(MsgUser u)
          Removes a given user u from the set of all logged-in users.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Server

public Server()
Constructs a new server. Initializes the map of users to an empty map (a TreeMap), keyed by user's name. Initializes the set of active (logged-in) users to an empty set (a TreeSet).

Method Detail

addUser

public int addUser(java.lang.String name,
                   java.lang.String password)
Tries to register a new user with a given screen name and password. If successful, creates a user object for this user and adds this user to the map of all users (using the screen name as the key).

Specified by:
addUser in interface Login
Parameters:
name - the screen name of the user.
password - the password for the user.
Returns:
0 if successful, or an error code (a negative integer) if failed:
-1 -- invalid screen name (must be 4-10 chars)
-2 -- invalid password (must be 2-10 chars)
-3 -- the screen name is already taken.

login

public int login(java.lang.String name,
                 java.lang.String password)
Tries to login a user with a given screen name and password. If successful:
1. Adds this user as a buddy to all other logged-in users;
2. Opens a dialog window for the user by calling user's openDialog() method;
3. Adds the user to the set of all logged-in users.

Specified by:
login in interface Login
Parameters:
name - the screen name of the user.
password - the password for the user.
Returns:
0 if successful, or an error code (a negative integer) if failed:
-1 -- screen name not found
-2 -- invalid password
-3 -- user is already logged in.

logout

public void logout(MsgUser u)
Removes a given user u from the set of all logged-in users. Removes u from the buddy list of each logged-in user.

Parameters:
u - user to be logged out.