Network Programming代写:CSE320 Wolfie Chat Part3

Introduction

这是接之前作业的第三部分,这部分主要是实现Client端的编程,接受消息和处理用户输入部分,同样涉及到多路I/O复用(select/poll/epoll),Socket编程等。
从这部分开始,就要编写具体的Wolfie通信协议了。按照作业给出的时序图,完成业务逻辑。

Client

The diagram below represents the general control flow of the Client. Study the figure carefully and ask clarification question on piazza.
The client begins by automatically using the command line args (see usage below) to connect to the server. It then proceeds to the login stage which at this time will be automatic as all that needs to be sent to the server is the client’s username. However, use some planning when implementing this section as you will later add support to ask the user for a password while logging in. Upon a successful login your client proceeds to block on I/O Multiplexing, this means your program is waiting on input from either the server’s socket or stdin.

  1. When receiving input your program will diverge depending on where the input comes from. In the case of stdin your client will handle the command given and do what is described below for the given command.
  2. When receiving input from the server socket, your program will handle the protocol as described in the document. One example would be, printing the time logged when the EM IT verb is received from the server.

Usage:

./client [-hcv] NAME SERVER_IP SERVER_PORT
-h           Displays this help menu, and returns EXIT_SUCCESS.
-c           Requests to server to create a new user
-v           Verbose print all incoming and outgoing protocol verbs content
NAME         This is the username to display when chatting.
SERVER_IP    The ipaddress of the server to connect to.
SERVER_PORT  The port to connect to.
  • To make things easier to read, use the ansii escape codes to help color your messages on the client as you did with the server.

Client commands

/time

When /time is typed into the client’s terminal, it asks the server for how long it has been connected. The server will return in seconds, the duration of the connection. The client program should convert this time in seconds to hours, minutes, and seconds.
Example: If the server sends back 30 seconds, your program should print

connected for 0 hour(s), 0 minute(s), and 30 second(s)

Example: If the server sends back 61 seconds, your program should print

connected for 0 hour(s), 1 minute(s), and 1 second(s)

Example: If the server sends back 7223 seconds, your program should print

connected for 2 hour(s), 0 minute(s), and 23 second(s)

Hours are the largest unit. If you were connected for 2 days, it should print out 48 hour(s), 0 minute(s), and 0 second(s).

/help

When /help is typed into the clients terminal it should list all the commands which the client accepts.

/logout

When /logout is typed into the clients terminal it should disconnect with the server.

/1istu

When /listu is typed into the clients terminal, it asks the server who has been connected.

WOLFIE Protocol

Now we will attempt to describe the protocol. This is how the server and client program communicate with each other. It is important that you follow the protocol described exactly. During the grading process we will test your client program with a server we have made and your server with a client we have made. As long as you follow the protocol correctly none of this should matter.
In the diagrams we use the symbol CR to represent \r and NL to represent \n (The software that creates the diagram parses \n as a newline so we had to change the symbols). You must replace all occurrences of CR NL with the correct values.
Example:

IAM student CR NL CR NL

should be translated to

IAM student \r\n\r\n

Login to the server

When the server accepts the client connection request, it will immediately spawn a login thread to handle the login work. The client will initiate the login transaction with the server by sending the WOLFIE verb to the server. The server will respond to the client by replying with EIFLOW (wolfie backwards). Then the server can expect the client to identify itself with the IAM verb. If everything is correct, the server will respond with HI . At this point the client is logged in. You should retain their login time in the data structure you employed to store user information. You can now send a follow up message to the client which alerts them to the message of the day by sending MOTD . The MOTD should print out on the client upon receipt.

The ellipses … represent that there is more communication further down, but just not illustrated in this part of the diagram.

Failed login

It is possible for a login attempt to fail. If a user logs in using the same name as someone already connected, the server should reject that user. The server will send the ERR verb followed by the and the corresponding .

A list of all error codes can be located at the end of the document.

Query connected time

When a user issues the /time command it will send the TIME verb to the server. The server will calculate the difference in time and send back to the user the verb EMIT followed by the time in seconds the user has been connected .

Use time(2) to record the login time in your user struct. Then to find the difference use time(2) again and take the difference.

List connected users

/listu

When the user types /listu into the terminal, the client program will send the LISTU verb to the server. It will receive back the verb UTSIL followed by a list of users delimited by \r\n.

As you can see the list has a \r\n deliminator and the list ends with \r\n\r\n.

Logout (client Initiates)

When the user types /logout into their terminal, it begins the process of disconnecting cleanly. The client will send BYE and then receive a BYE response from the server. Implementing the client so that it waits for confirmation from the server is a polite way of making sure the server has properly removed the user from it’s data structures and is in an acceptable state for the user to then disconnect.

Shutdown (server initiates)

Finally if the user were to type /shutdown it would initiate the above protocol to terminate the connection with all connected users. It will send BYE to ALL CONNECTED USERS. It would then clean up any open files and sockets it may have open and then terminate.