Introduction
java,,,JSON,Chat,,
,,networkjava
Requirement
Distributed Systems
COMP90015 2016 SM2
Project 1 - Multi-Server Chat System
1. 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.
2. 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:
2. Chat Client 1
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.
Here is an example of how a client session may look:
E:>java -jar client.jar -h localhost -p 4444 -i Adel
Adel moves to MainHall-s1
[MainHall-s1] Adel> #createroom comp90015
[MainHall-s1] Adel> Room comp90015 is created.
[MainHall-s1] Adel> Adel moves from MainHall-s1 to comp90015
[comp90015] Adel> Maria moves from MainHall-s1 to comp90015
[comp90015] Adel> We have a new comer to the room, Hi Maria!
[comp90015] Adel> Maria: Hi everybody!
[comp90015] Adel> Maria: Bye
[comp90015] Adel> Maria moves from comp90015 to MainHall-s1
[comp90015] Adel> #deleteroom comp90015
[comp90015] Adel> Room comp90015 is deleted.
[comp90015] Adel> Adel moves from comp90015 to MainHall-s1
[MainHall-s1] Adel> Maria moves from MainHall-s1 to jokes
[MainHall-s1] Adel> #quit
[MainHall-s1] Adel> Adel quits
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.
2. Chat †⸳䤠⁓⁴†ⱳ†㰠ⁱ⽵㹥൳ੴ† ⁵㱥†⽣㹯൮੮♥㭮㱳⽭㹵൬ੴ⁵‾⁁⠠Ⱐ⤠⁰†♭㭥㰠⽲㹥ਠ䥩†‼⁁ⱬ†††⁴䵣䡩⁴⁹㱨†⽳㹥൲੶‼‾ ⁴†㱲‾⼍㸊ൃ੯♭㭣㱡⽮㹳ഠ੩䅮††⁴†⁴ 㱤⽭㹭൵੮⁴⁷†‼䵢䠾†㱯⁷⽩㹴൨ਠ♣㭣㱬⽴㹳഼‾†⡳⁰⁹†⤠⁴†㰍⽯㹡†㱣⽶㹥ഠੴ㍨㑳⸬†䩔䙯㱩⼠㹢ੴ䅷⁶†⁴⁴⁵䩲㱴⽲㹶ੲ䍩㱨⽯㹮൮䅣⁹‼‾⁷䩨‼⁔†⡴♨㭥☠㭤⥹†⁵ⸯ‾☍㬠㱣⽮㹴ഠੱ䅵䩴⁵※ ⁰†䩡⁰䑮⁴
䨠㱬
⼬㸠ദ⡵⤻㱳⽤㹩൲♣㭴㱯†⽤㹩൦੦䅥⁵※†䌾 †䘬㠠†♲㭮㱳⽬㹩൴㌠㕦⸠⁴䕨†㰠⽮㹴൩ੴ䍹†※ 㨠㰨†⽡㹮൹ⵯ⁴†ⵯ㱣⽮㹴ഠਨ†⁴⁴Ⱪ♴㬠㰯 ⽰㹲൯ੴ†††
⁵
⁃†⸠♥㭳㰼†⼯㸾ਊ䕔††♥㭮♨㭢㩦㰠⽥㹮൴ਠ㱵⽡㹴ਠ㍷㍴†䍥††㑴㱯⽡㹣൨ਠ⁔†ⱳ†♵㬼㱢 ⼾㸍ഊੰ††›⁔†Ɱ☩㭳㱥⽮㸠൷੨Ⱪ⁴♹㭢㱲‾⼍㸊൬੯⁵⁷⸠♲㭹㰠⼼㹢൲ਠ䄯‾⁴⁷㩲㱯⼠㹢ൡੳㅤ†㑯㑣㑡㑬†㕤㕡㕴㕡㰠⽣㹡൬ਠ㉩㑴㐠㑩㕤㕮㕴㕩㙴㱩†⽡㹮ਠ㍩ㅴ㤼㉢ㄠ㘯㠾⸍ㄊ㉦†㑣㑨㑡㑴†㕲ははね㱳‼⽢㹲ഠਯ䔾
†♷㭥㱲⼠㹡൬੬
⠠⁷⁴㑩†⁴⥴☠㭥㱮†⽴㹨ਠ䍣††††♣㭩㱮⽮㹧ഠੳ㑥⁶䍥㱨⽥㹳൵੬†ⵡⵯ⸠㰯 ⼦㹢൵੬⁶⁴⁴⁔♣㭳㰮⽥㹲ਠ䥩† ⁴♭㭤㰠⽴㹥ൡ㐬⸠ㅴ䥯㱥⽢㹬൩ੳ†ⁱ†⁶ ⁃ ⁁㱯†⽩㹳ഠ䔠㩩㱮⽩㹣൨ਠ♬㬠♥㭧㨠♨㭳☠㭲Ⱐ♮㭬♣㭯㩭‼♢㬊䅣♥㭮㱡⽳㹥൲੶†⁴⁴†††⸼䥲 †㱲⽲㸠യਾ㌍ ㅧ㘠㱥⽦㹯൲䥥†‾††⁴††††ⰻ‼‾㱳⽥㸠ൣ੨㰠⽴㸠ൣ੬䕩㩮㰠⽯㹯൭ਮ☦㬼☾㭥㩴☠㭲☠㭴Ⱐ♯㭳♮㬠⁴㩯†♴㭲ㅥ♲㭧Ⱪ♧㬠♭㭬㨻‼♢㬊䅉☠㭴㱣⽴㹥൳ਠ⁴⁴††††† ⁴⸼㱢 ⼾㸍ഊਦ⁁⁴†⁴‼㱩⼠㹬൩ੳ⡹††‾⁵※††㱣⽮㹴ഠ੪⥡⡥††⁴† 㱥⽔㹨ਠ⤠㱨⁍⽡㹩൮ੈ䕡㩩㱳⽤㹱൵੯☦㬻♰㭲㩮☩㬯♣㬠ⱷ♴㭮♱㭥㨦♤㭰㉳♳㭥Ⱐ♯㬠♣㭲㩯☠㬦䅲♯㭥Ⱐ♬㭳☠㭢㨠 ☾㭵♴㬠㱥⽲㸠ൡੳ㐺⸼䍲※㕴㱹…⽲㹤൱ੵ䕯㩤㱱※⼬㸠ദ੬♱㭯♲㬠›㨠…♬㭪☼㬍Ⰺ⁔♨㭶♴㭥㩤☠㭫㉯♯㭥ⱳ♡㬠♯㭳㩲⁶♥㬠䅬☠㭯Ɐ♩㬠♬㭴⁹㩰☦㬻♤㭯㱫⽭㹩ਦ…ⁱ⁶…†…⸠…†…⁵‼㱯⽳㹰൯੮㰠⽶㹥൲ਠㅩ⡧⤠⁴† Ⱨ†㨯㰾⁔⽨㹥ഠੲㅥ⥣䅩†⁶†⁴⠠⁴†⥥⁰⁷♥㭯䅷☠㭧‾䴍䡥⁰㱶⁴⽨㹥ഠ੬‼…⁵…†㨦㱲⁵⽯㸻ബਠ♬㭳♲㭱⁵㩯※☠㭱☻㭱Ⱶ☻㭤☺㭵㨻♯㭤☬㭵㰻⽫㹥ਦ㉲⥤ⁱ䑵›…ⁱ⁽†⡴⁴†⁷†⥮⁹ ⁰ⁱ㰠…⽬㹤൱ੵ㩯㰻…⽬㹤൱ੵ☻㭲♤㬺†㨦♤㬲♤㭯Ɐ♩㭵♱㭫㩳…♲㬬♵㭫㱤⽱㹵൯䤠†※⠦※⤍ ㅮ††
††㩭㱩⽥㹰൬੩♳㭡♧㭴㩥†♶㭡♥㭴ⱨ♲㭲♱㭫㩳…♲㬠ㅲ☠㭢Ⱳ†☯㬠♤㭥㩥♳㭩䅤♱㭴㰠⽥㹲ഠੳ›⁹Ɐ※†※Ⱜ†ⁱ…†㱤⼻㹳റਦ⁵…⡱ㅯ⤻†…⁵…⁵⁵⁵⁵⸦㱲⁵⽯㸻ൽ਼䙢ⱷ†††⁷Ɐㄬ†ⁱⁱ※㰦ⁱ⽵㹯഻ੳ䵥䡲ㅱ⁵†…⁵※㨦㱲⁵⽯㸻ഠ☦㬻♳㭯※㨬†☦㬻♤㬦Ⱳ♱㭬☻㬯‾㨍 ♷㭴䄠♳㬠ⱳ♲㭥♨㭫㨼♲㭓♥㬠Ⱳ♣㭧♥㭯㩩☠㭧䵥䡴ㅱ♵㭲㱶…⽲㹤൱ੵ›…⁵⁴†††⁴㱫…⽡㹣൩ੲ䵣䠿ⵡㄠ†㰠⼊㹆൩੮㑡㉬䰠㱲⽩㹥൳ਠ⁴⁷†††⁹☺㬾⌍♤㭹ⁱ⁵㱥⽭㸦൲…ⁱ䨻㩯㰻†⼦㹬ੱ♯㭳♵㭬㩱⁵♯㭯♲㬠㰠⽱㹵൯※⁷⁴†㨠㱴†⽣㹬൩㱴⽤㸠ൡ੬♬㭣♴㭴†㩡♥㭥♥㬼Ɫ㱲‾⼍㸊൲੯♯㭱♥㭵㨻†♡㭶䵩䡬ㄼ♢㬊ⱻ…♬㭴䵹䡲ⵯ㈠☺㭵Ɐ※♲㭡♤㬠㱬⁵⽯㸻൩㱮⁴⽹㸦൲㑱㍯⸻†㱬⁵⽯㸻ു†ⁱ†…※ⁱ♤㭯⍫♲㭽⸼䥲‾䨍†㱯⽳㸠൮੯㩳㱵⽳㹳൦ੵ㑬ㅹ⸠⁴䥥†㙳㰱⽰㹬൩☠㭥♥㭴㨺‼♢㬊♤㭹㱥⽱㹵൯…ⁱⁱ※⁵›ⁱ㩯㱫…⽲㹤൱ੵ㰻…⽬㹤൱ੵ♯㭯♲㬠›㨠…♬㭦☼㬍Ⰺ㰴⼠㹊൯੩♮㬼☊㭬㩥♴㭪♴㭯Ɐ㱭⽦㸠൨☯㭳♷㭦†㩴☠㭮䅴♡㭭Ⱐ♢㭮䍧♯㬼Ɫ♲㬦䵬☻㭲Ɐ㱭⽯㹭൩☦㬻♣㭊⁓㩏⁎☠㭴䄠♲㭥㱮⼠㹡൳㱢 ⼾㸍ഊ㐦㑤⁵䍯…㱯›⼠㸦൬䅱⁵ⁱ※⁵☠㭥⍲♮㭦Ⱐ⁴⁴㱲‾⼍㸊੦†⁷⁸†⁷Ɐ㱨†⽴㹨਼††㱨†⽲㹯൯੭⁷⁵›‾⁻⸠…䥬ⁱ㱱※⼬㸠ദ੬㍤ⁱ⁹ⁱ※※ㅍ㙡†㱯⽲㹭ੲ䤦⁵※ⱱ⁵…†ⁱ‾⁈㩲㰠⼠㹲൯੯♩㬠♳㬠㩮☠㭥♣㭯Ɐ♳㭥♹㬯‾㨍 ♉㬠♩㭴ⱨ☠㭳☠㬠⁴㩨☠㭴♯㭥㰬⽥㸠൳⁹†‼⁴㱨⽥㹭ൢ⁴㰠⽴㸠൲੯†††‼ 㱯⁹⽰㹥ദੲ㰻⽭㹣൨ⁱ⁴†⁍Ⱳ†㱲⁵⽯㸻ബਠ㱩⽤㹱൵੯…⁵⁵⁽⁹††㱦⽴㸠൲⁴Ɐ†⸍㰊⽳㹡൧㐠㥥䅲†㱥⽲㹶ੲ䤠†⡩††䍲†⥨ⱡ⁴㱣⁴⼠㹲൯੯⁻⁵㱤⼻㸠ഺਠⁱ⁴…†⁵…⁵☱㬴㱯⽲㹴ദੲ⽯†⁵⸴㰵⽱㹵൯㕽⸼䅲㰠⽴㸠ൡ੮䥤⁰⁶Ⱳ†† ‴†㱯‸⼼㹢൲ਠ㱯⽥㹣੩㙶⁴䅥㱧†⽴㹨ਠ䩮ㅩ㡩㱥⽮㸠൲†
‼ ⸦㱬⁵⽯㸻൴※※ⁱ※⁵㍯⸻㕍㱩⽬㹬ഭੳ㜱⸦※㱵⽯㹯൭੩ㅱふ†ⱤⁱⱯ※ㄬ†⁵⁴…⸠›⁍Ⱖ⁽Ⱟ‾⁴㰠⼠㹴ൡੲ†⸠㱴†⽭㹩†㍡〠ひ†⁴†㱧†⽴㹨੮ⱶ†††⁍⁈Ɐ‼ⸯ‾䜍†⁴※㱡⽱㹵൯㜠†ㄠぬ㱩†⽡㹮ਠ⸠㱨†⽩㹮ഠੴ䡨†♲㬠㱉⽬㹳൯਼♣㬠㱡⽯㹭ൣ੨䙡䡳♬㭥㰠⽢㹥൲ੳ䌠⁉♮㭩㱴⼬㸠൩ੴ…♬㬻㱍⽡㸦൲※㱱※⽴㹹൰㠦㱱※⽳㹥൲੶…ⁱ…⁵䱯䴻㨺㰠⽱㹵൯…䑱䙵ⁱ♵㬠㰦ⁱ⽵㹯഻ੳⁱ☠㭩㱶⼠㹴൨⁶†䥥䄠♣㬠㱷⼠㹳ੲ ††ㄠ㠯 †ⱴ† 䱳䵨††㱴⁴⽨㹥ഠੲ⁵†ⁱ※㱲‾⼍㸊൲੯㡯⁉ㅮㄠ㱊⁎⼠㹦൯ੲ㱭⼠㹩൴ਠ♩㭮㱴⼠㹹 the client as:
{“type” : “deleteroom”, “roomid” : “jokes”}
If not successfully deleted, s1 replies to the client with:
{“type” : “deleteroom”, “roomid” : “jokes”, “approved” : “false”}
If successfully deleted, s1 informs other servers by sending the message:
{“type” : “deleteroom”, “serverid” : “s1”, “roomid” : “jokes”}
The server will treat this as if all users are joining the MainHall. It broadcasts roomchange messages to all
members of the deleted room showing that each member is moving from the deleted room to the MainHall.
The server will also send roomhange messages per clint of the deleted room to all clients currently in the
MainHall.
Finally, s1 replies to the client who is deleting the room with:
{“type” : “deleteroom”, “roomid” : “jokes”, “approved” : “true”}
4.7. Message
Chat messages are encapsulated as:
{“type” : “message”, “content” : “Hi there!”}
The message is broadcasted to all the members in the chat room by the server using the following message:
{“type” : “message”, “identity” : “Adel”, “content” : “Hi there!”}
4.5. Join Room 9
4.8. Quit
The client can send a quit message at any time by using the command “#quit”:
{“type”:”quit”}
When a server receives a quit message from a client, the client should be removed from the client list.
If the client is the owner of a chat room, quit should be considered as deleting that chat r