Directory Server

Create a directory server in Java called DirectoryServer.java. You may include other files/classes as you see fit.

A directory server keeps track of clients logged into a peer-to-peer system. Its purpose is to allow clients to look up other clients that are logged on so a direct connection can be made between the two clients. The server does not make the connections between clients, it just provides information about each client. This server will listen on port 54321.

This directory server will store the following information about each of the clients:

  1. An integer Client ID (automatically assigned by the server when the client first logs in).
  2. The client's alias (a name created by the client)
  3. The IP address of the client.
  4. The client's port number so other clients can contact it. This is NOT the port the client connected on. Think of the client as a program that itself listens to connections from other clients, although you will not implement this feature here. For now, make it some integer specified by the client.
  5. A time indicating when a client's entry will expire (in seconds).
This information remains available until the client's entry expires. Once the information expires, the server should remove it. The client's application can PING the server in order to keep its entry alive. The server can remove outdated information each time it receives a request, rather than a thread or other asynchronous technique

The protocol for client-server interaction is as follows:

  1. The client opens a tcp connection to the server.
  2. The clients sends a single command (as described below).
  3. The server sends an appropriate reply.
  4. The server closes the connection.

Note that connections are NOT persistent. The client connects to sign on to the system and immediately disconnects. Being signed on to the system is not the same as being connected to the server. Since connections consist of issuing a single command only, the server does not need to handle simultaneous connections.

Use netcat to connect to your server and test both correct and erroneous communication.


Server commands

The server should respond to the commands LOGON, PING, LIST and LOGOFF. Notice the commands sent from client to server are delimited by space characters and the replies are delimited by colons. Both requests and responses end with a newline character.

LOGON

The LOGON command should add the client's information to the list of clients. The server will get the host IP information from the Socket. The port specified here will be the port that clients use when they communicate with each other.

LOGON(space)port(space)name\n

The reply should be of the form:

ADDED:id:ttl\n

Where id is a server assigned identification number for the client. The client application will use this id when making other requests. ttl is the time, in seconds, the client's entry will exist before it is removed.


PING

The PING command is issued to the server to indicate that the client application is still logged on. If an application does not ping the server it will be logged off. The client application must include its id, so the server knows it is a valid client.

PING(space)id\n

The reply is of the form:

PONG:ttl\n

where ttl is the new time to live (in seconds) provided by the server.


LIST

The LIST command tells the server to generate a list of all clients connected to the directory server. The client must include its id, so the server knows it is a valid client.

LIST(space)id\n

The reply consists of a header line that includes the number of clients logged on (n), which will also indicate the number of lines that follow (one for each client).

LIST:n\n
ID:name:host:port\n
...
ID:name:host:port\n

LOGOFF

The logoff command should remove the client from the list of clients and invalidate its id.

LOGOFF(space)id\n

The reply is of the form:

DONE:id\n

Errors

The server should respond to erroneous commands as shown below. The message portion can be any string (but should not contain ":" or new lines).

ERROR:MESSAGE\n

The server should be able to handle errors gracefully and continue to run.


Submission

Turn in the file DirectoryServer.java: a documented Java implementation of the server.


Grading

Your program will be graded on the following criteria:

While testing your program I will need to adjust the time to live value, make sure it is defined as a constant.


Directory Client

In case you are interested in developing the client and server at the same time, the next assignment will be a DirectoryClientConnector class and test program.