Write a program which implements a simple Instant Messaging (IM) system. The system consists of two components:
Both components should be written in Erlang.
The IM system is built around chat-rooms. Chat-rooms are identified by unique names, these names can be registered or discovered by the name server. Any message sent by a client to the chat-room is relayed to all connected clients.
Grading: points are given in square brackets.
[10] There is a single name server process which accepts incoming connection_request
messages from clients:
There are separate processes for each chat-room.
Upon incoming connection_request
the name server should check if there is already a chat-room process active.
If there is none, it should be spawned.
The name server should reply the client informing it about chat-room's PID.
Further communication takes place between the client and chat-room processes.
Additionally the name server informs the chat-room about the new client sending its PID.
The name server can also be asked about all available chat-rooms. It replies with a list of their names.
[10] A trusted client can send a message
to the chat-room.
The client is trusted if its PID is obtained from the name-server.
Untrusted clients' messages are not relayed, appropriate error message is sent.
The chat-room process forwards a payload of the incoming message to all connected clients (processes), including the sender
The chat-room keeps track of all connected (trusted) clients.
A client can disconnect itself from the chat-room by sending appropriate disconnect
message.
If there are no clients connected the chat-room should terminate itself - in such a case the name server should be informed that the chat room no longer exists.
[10] The client implements the following functions which subsequently send messages to appropriate processes:
get_chatrooms
- returns a list of chat-rooms,connect
- connects to a chat-room,disconnect
- disconnects from a chat-room,receive
- returns all received messages along with their origins,send
- sends a message.Decide by yourself what arguments the functions should have.
[5] Make the above functions work to provide an interactive client which can connect to a chat-room. While connected it gets all the messages, and simultaneously it is capable of sending messages.
There is a single name server, multiple chat-rooms and multiple clients.
Make the name server a registered process which name is defined upon creating the process (i.e.: name_server
) - it would allow to extend the system to have different chat servers running.
A client should know the name of the name server to connect to (it might be the client functions' parameter).
Furthermore it allows to test the system using multiple shells or Erlang nodes easily.