Distributed Systems代写:COMP90015 Multi-Server Chat System

Introduction

代写一个分布式的聊天室应用程序,和常见的聊天室应用不同的是,这个作业的每一个Client,是由两部分组成:常规的Client以及一个Server,这样就能实现多人的群聊功能。

Synopsis

The assignment is about creating a “chat” application. The chat system consists of two main distributed components: chat servers and chat clients, which may run on different hosts in the network. The architecture of the system is presented in the following diagram.
Chat clients are Java programs that can connect to a single chat server; this server can be any of the servers available in the system. Chat clients can be used to send requests for creating, deleting, joining and quitting a chat room. They can also be used to send requests to see the list of available chat rooms in the system and the list of client identities currently connected to a given chat room. Finally, they can be used to send chat messages to other chat clients connected to the same chat room.
Chat servers are Java programs accepting multiple incoming TCP connections from chat clients. There are multiple servers working together to serve chat clients. The number of servers is fixed and does not change once the system is active. Each server is responsible for managing a subset of the system’s chat rooms. In particular, a server manages only those chat rooms that were created locally after receiving a request from a client. In order to join a particular chat room, clients must be connected to the server managing that chat room. As a result, clients are redirected between servers when a client wants to join a chat room managed by a different server. Chat servers are also responsible for broadcasting messages received from clients to all other clients connected to the same chat room.
In this assignment, the chat client program is provided to you and you need to design and implement the chat server program.

Chat Client

The chat client executable jar file is provided to you, there is no need to implement your own. A chat client is executed as follows:

java -jar client.jar -h server_address [-p server_port] -i identity [-d]

Square brackets are used to indicate optional arguments.

  • server_address corresponds to the ip address or hostname of the server to which the client is connecting to.
  • server_port corresponds to the port in which the server is listening for incoming client connections. The default server_port is 4444.
  • identity corresponds to the client’s identity (i.e. username) which must be unique in the entire system. If the identity already exists, the server will send a message to the client indicating the error and will then close the connection. If the identity doesn’t exist, a connection is established and the server places the client on its MainHall chat room. More details on this are given later.
  • The -d option can be used to start the client in debug mode. This means that all the received and sent messages will be printed on the standard output.

The client user interface is command-line based and reads input from the standard input. Each line of input is terminated by a new line and is interpreted by the client as either a command or a message. If the line of input starts with a hash character “#” then it is interpreted as a command, otherwise it is interpreted as a message that should be broadcasted by the chat server to other clients in the same chat room. The list of commands supported by chat clients is as follows:

#list
#who
#createroom roomid
#joinroom roomid
#deleteroom roomid
#quit

Pressing Ctrl-C terminate chat clients and works similar to #quit.
A client may receive messages at anytime, and these will be written to the standard output as soon as they are received, even if the client is in the middle of typing a message.

Chat Server

A chat server listens for incoming connection requests from clients. It is capable of accepting and managing multiple client connections simultaneously.
A chat server must store locally (there is no need to use persistent storage, data may be stored in memory):

  • A list of all the existing chat rooms in the system, including those managed by other servers.
  • A list of all the client identities currently connected to that server.

Communications in a chat server can be categorized into two main groups: Communication with chat clients and Communication with other chat servers.

Communication with chat clients

A chat server is primarily responsible for managing all the clients currently connected to it and for broadcasting chat messages. To achieve this, TCP connections between clients and servers are established and remain active throughout the duration of the client-server interaction. That is, the connection is only closed when:

  • The identity of the client already exists,
  • The client quits the system,
  • The client program is abruptly terminated (e.g. using Ctrl-C),
  • The client is redirected to a different server.

In any of above cases, it is the responsibility of the server to remove the identity of the client, the client’s chat room (if any), associated thread(s), the TCP connection with client (if any), after sending the appropriate protocol message to the client (if required).

Communications among chat servers

To maintain consistency among servers’ data, such as ensuring the global uniqueness of client identities and chat rooms, servers need to communicate with each other to reach consensus. To achieve this, servers use a protocol that consists of two phases:

  • The lock (voting) phase, in which a coordinating server sends a lock request, lockidentity or lockroomid, to all other servers which in turn reply with their vote to either allow or deny the creation of the new identity/chat room, based on their local data (local list of client identities and list of chat rooms).
  • The release (commit) phase, in which the coordinating server decides whether to allow (if all servers voted to allow) or to deny (if at least one server voted to deny) the creation of the new identity/chat room. Regardless of the result, the coordinating server notifies the result to all the other servers by sending them a releaseidentity or releaseroomid message.

Servers communicate with each other using TCP connections. There is no need for these connections to remain active throughout the lifetime of the system, and instead, they should be established and destroyed as required.

Chat Rooms

A chat room is a place in which clients can exchange chat messages. Each server initially has a chat room called MainHall-serverid. When a client initially connects to a server, it is placed in the MainHall chat room of that server.
The following rules regarding chat rooms must be enforced by a chat server:

  • Each client is only allowed to be a member of one chat room at a time.
  • It is the chat server’s job to broadcast messages to all the chat clients in a room.
  • Clients who are members of a chat room must be connected to the server managing that chat room.
  • If a client creates a chat room, then that chat room will be managed by the server that the client is connected to.
  • A chat client is able to owns one chat room at a time.
  • Each server must maintain a list of globally available chat rooms and a list of its locally managed chat rooms.
  • If a client joins a room managed by the server it is connected to, then the server simply places the client in the corresponding room. If the room is managed by another server, then the server sends a message to the client redirecting it to the server managing that chat room.
  • Only the owner of a chat room (i.e., the client that created the chat room) can delete a chat room.
  • If a client deletes a chat room, the members of the room are moved to the MainHall of the server. The
    server will send a message to all the corresponding clients informing them they have changed rooms.
  • A chat room is deleted if the owner client quits or disconnects abruptly. The remaining members of the chat room are moved into the MainHall of the server.

The protocol (including all the messages and their format) that the chat server must follow is specified later in the project specification.

JSON Format

All communication in the system takes place by exchanging messages encapsulated in JSON format passing through TCP connections.

  • All messages will be sent as JSON objects with a newline (“\n”) to delimit objects.
  • A JSON library must be used to marshal and unmarshal JSON objects. Do not implement JSON (un)marshaling yourself.
  • All data written to the TCP connection must be UTF8 encoded.

Executing a chat server

Chat servers should be executable exactly as follows:

java -jar server.jar -n serverid -l servers_conf
  • serverid is the name of the server,
  • servers_conf is the path to a text file containing the configuration of servers.

Each line of the configuration file contains the following “tab” delimited data:

serverid    server_address    clients_port    coordination_port
  • serverid is the name of the server,
  • server_address is the ip address or hostname of the server,
  • clients_port is the port used to communicate with clients,
  • coordination_port is the port used by the server to communicate with other servers.

A sample configuration file looks like:

s1  localhost   4444   5555
s2  localhost   4445   5556
s3  192.168.1.2 4444   5000
  • Each server will find its own confguiration in the configuration file using serverid.
  • Use command line argument parsing (e.g. using the args4j library or your choice).
  • Clients can connect to servers only after all the servers have been started.

Chat Protocol

The chat protocol specifies exactly how the client-server and server-server communication happen.

  • Your solution must implement the protocol exactly as stated.
  • Incomplete implementation and deviation from the protocol will result in dedcution of marks.

New Identity

The client sends a newidentity request with its identity immediately after connecting to the server.
The identity must be an alphanumeric string starting with an upper or lower case character. It must be at least 3 characters and no more than 16 characters long.
If the identity is not in use by any of local clients of the server or is not locked by any other server, then server sends a lockidentity message to all the other servers.

List

The client can ask for the list of chat rooms in the system with the command “#list”. The command is sent to server as a JSON message.

Who

The client can ask for the list of clients in the current chat room using the command “#who”.

Create Room

A connected client can create a chat room by using the command “#createroom roomid”, if and only if the client is not the owner of another chat room. When a client successfully creates a room, it should automatically join the room.
The roomid must be an alphanumeric string starting with an upper or lower case character. It must be at least 3 characters and no more than 16 characters long.

Join Room

The client can join other rooms if he/she is not the owner of the current chat room by using the command “#joinroom roomid”.

Delete Room

If the client is the owner of the chat room, he/she can delete the room by using the command “#deleteroom roomid”.

Quit

The client can send a quit message at any time by using the command “#quit”.

Abrupt disconnections by clients

If a client gets disconnected (e.g. if the TCP connection is broken), then the server treats this as if the client had sent a quit message.
The server should send appropriate messages to other clients in the system and deletes the client’s identity and his/her chat room.

Assumptions

In this project, we need not to consider failure handling for servers. We assume that chat servers do not fail or crash.