Reference no: EM132244592
Assignment Questions -
Question 1 - As part of this problem, you will implement a simple chat application in Python. A chat app will allow a user to type in a message from the keyboard and send that message to other (one or more) chat apps. The chat app will be using TCP sockets to communicate with each other. The code for the chat client should be saved in chat.py. The chat client may take the following command line arguments.
- First argument: A string that identifies the name of the user running the chat program.
- Second argument: A number indicating the port number on which the chat client will wait for incoming connections.
- Remainder arguments: The remaining arguments are a list of strings that specify the IP address and port number of other chat clients to connect to. For example, the string 192.168.1.5:2000 indicates that the IP address is 192.168.1.5 and the port number is 2000.
As an example, to setup a conversation among three chat apps, you will need to start your chat app three times with the following arguments:
- python chat.py user1 5001 127.0.0.1:5002 127.0.0.1:5003
- python chat.py user2 5002 127.0.0.1:5001 127.0.0.1:5003
- python chat.py user3 5002 127.0.0.1:5001
Under the above configuration, the messages typed by user 1 will be received by users 2 and 3. Similarly, the messages typed by user 2, will be received by users 1 and 3. However, the messages typed by user 3, will be received by user 1 but not user 3. When the user types "exit", the chat client will close opened all sockets and quit. As an important side note, the connections to the clients should be established only when the user types in the first message; otherwise, you will get errors since not all chat clients will be running.
The content of the messages exchanged between users will be specified in JSON. It should include the following information:
- Sequence number (seq) - a unique identifier for the message send by a user starting from 0.
- Source user (user) - the user name as supplied as the first argument to the chat app.
- Message (message) - the actual message that the user typed in.
Below is an example of a message encoded in JSON: {"seq": 0, "user": "octav", "message": "hello"}
Question 2 - A limitation of the above chat app is that you need to know ahead of time the IP addresses and port numbers of the users that you want to communicate. As part of this question, we will refine the chat app to allow users to register with the chat server so that all the users that are registered will receive the messages. You will have to implement the chat client in chat5.py and the server in server5.py. The system will work as follows:
1. When it starts, the chat client will connect to the server and register.
2. When the user types in the message in a chat client, that client will send the message to the server. It is the job of the server to distribute it to all connected chat clients.
3. When the chat client stops, it will send a message to unsubscribe from the server. For this question, the chat client will only accept the following three arguments:
1. A string that identifies the name of the user running the chat program.
2. A number indicating the port number on which the chat client will wait for incoming connections.
3. The IP address and port number of the chat server.
The chat client and server may exchange three types of messages (serialized using JSON): subscribe, unsubscribe, and message. The subscribe/unsubscribe operations will have the following fields:
- Operation (operation): Either subscribe or unsubscribe.
- Client (client): The IP and port number of the client.
An example of a subscribe message from a client running on 127.0.0.1:5000 is shown below: {"operation": "subscribe", "client": "127.0.0.1:5000"}
A message typed into a client is delivered to the other clients in two parts: first, it is delivered to the sever and then, the server delivers it to other connected clients. Similar to question 1, the message will include the following fields:
- Sequence number (seq) - a unique identifier for the message send by a user starting from 0.
- User (user) - the user name as supplied as the first argument to the chat app.
- Message (message) - the actual message that the user typed in.
The job of the server is to maintain a list of connected clients. In response to a subscribe message, the server should add the IP:port combination to the list of connected clients. After the registration is complete, the client will read input from the keyboard. Each provided line will be sent in a single chat message whose format is detailed below. When a chat client receives data from a peer chat client, it should display the received message.