A company named Outdaters is running a small computer wire line network, called Outernet. Not like Internet, Outernet is not base on the TCP/IP protocol. Due to lacking of money, not all the computers in Outernet can communicate with each other directly.
Outdaters have already found the solution. They created a protocol to make all computers in the network become application proxies. An application proxy can receive data from a connected computer and send them out to another connected computer. Therefore, by using this protocol in Outernet, if a computer wants to send something to a computer not linked directly, it has to send them to a connected computer/application proxy and ask it to help sending them to the destination or another connected computer/application proxy.
The protocol is described as
A. Port
Application proxies use port to indicate each connected computer. Port number is an integer number from 0 to 32,767. To an application proxy, 0 means the application proxy itself, each other port number represents a unique computer connected to the application proxy.
B. Commands
The application proxy accepts only 3 commands, case sensitive: TO, DATA, QUIT.
To each incoming command, application proxy will response with 3-digit result code in a line to the incoming port after handled this command.
Format:
xxx< LF > xxx -- the 3-digit result code
Result codes:
100: OK. No error/Data routed to destination
101: OK. Data routed to application. (Destination computer is application proxy itself.)
200: Session ends (Response to QUIT command)
300: Unknown command
301: Unknown destination
302: No session began
303: Looping not allowed (when incoming port = outgoing port)
Details for each command:
1. TO:< destination computer name >< LF >
Tell the application proxy, the following data need to be sent to < destination computer name > ,and cancel the last "TO" command's effect (send a "QUIT" command to the original destination computer). If TO command fails (result code is neither 100 nor 101), the state of the application proxy will not be changed.
Possible result codes are:
100: The destination computer is found in routing table, and not the application proxy itself.
101: The destination computer is found in routing table, and IS the application proxy itself.
301: The destination computer is not found in routing table
303: The destination computer is found in routing table, but incoming port = outgoing port
2. DATA< LF >< the data >< a dot "." > < LF >
Send < the data > to destination computer. < the data > will be regarded as a data stream, and sent to the destination without any alteration if the destination computer is not the application proxy itself. The backslash ("\") is the meta character, "\." means a simple dot ".", instead of then end indicator, and "\\" means "\".
Possible result codes are:
100: The destination computer is found in routing table, and not the application proxy itself. The data is routed to the corresponding outgoing port.
101: The destination computer is found in routing table, and IS the application proxy itself.The data is routed to the application running on this application proxy.
302: No session began, this command is ignored.
3. QUIT< LF >
End this communication session.
Possible result codes are:
200: Session ends(Response to QUIT command)
302: No session began, this command is ignored.
C. Session
When a computer (the requester) sends a "TO" command to an application proxy, a communication session begins; when a "QUIT" command is sent to the application proxy, the session ends. In a session, the requester can send multiple "TO" and "DATA" commands to an application proxy to send out multiple messages.
An application proxy is able to handle sessions simultaneously from different ports.
D. Routing table
Each application proxy holds a routing table. It uses this table to find which port should be used the destination computer name. Each line in the routing table contains 2 fields, the first is the destination computer name, and the second is the outgoing port number. It means, the data to be sent to a computer with the destination computer name, will be sent out via the port with the outgoing port number. Port number 0 means, the data should be routed to the application running on this application proxy; that destination computer name is actually the application proxy's name.
E. Routing
Application proxies use the same "TO", "DATA", "QUIT" commands to route the incoming data if the routing is possible.
After searching on the routing table, if the outgoing port found, application proxies must create a complete session on the outgoing port for each valid incoming "TO" command: one "TO" command at the beginning, zero or more DATA commands to route the data, one "QUIT" command in the end if the incoming session ends or another incoming "TO" command is received.
Port 0 is handled as same as other outgoing ports except that no actually outgoing command is sent , i.e. all the commands' result code will be sent to the incoming port, but no commands will be sent to any outgoing port.
Now, Outdaters hires you to write the engine to implement the protocol for the application proxy.