The project requires no additional installations, as it relies on libraries that are part of the standard Python 3 library.
To run the project, follow these steps:
- Start the first daemon using the command:
python3 simp_daemon.py <IP_ADDRESS>
(for testing we used 127.0.0.1) - Start the second daemon using the command:
python3 simp_daemon.py <IP_ADDRESS>
(for testing we used the private IP address of the computer)
- Connect to the daemon with a client. (If the daemon is running it will ask for username)
- After this, one of the following 2 will hapen:
- Wait or start
- If the client choose wait, it will wait for a request indefinitely. (Currently there is no way to go back or disconnect in this stage. The error in case of receiving a request while in the choosing state is not handled, so the client must be in waiting state to receive connection request correctly.)
- If the client choose start, it will ask for the IP address of the other daemon. After the IP address is sent the daemon will send the request to the other daemon. After this the client will wait for the other daemon to accept or reject the request.
- If the client choose wait, it will wait for a request indefinitely. (Currently there is no way to go back or disconnect in this stage. The error in case of receiving a request while in the choosing state is not handled, so the client must be in waiting state to receive connection request correctly.)
- Accept or decline
- If the client choose accept, the connection will be established by a three-way handshake and the chat will start.
- If the client choose decline, the connection will be terminated and the client will be asked if they want to wait or start again. A FIN will be sent to the other client, and they get kicked out from the daemon with an error message.
- If the client choose accept, the connection will be established by a three-way handshake and the chat will start.
- Message sending:
Both clients can send messages to each other. The messages will be displayed on the screen. When a message sent the client is waiting for an ack. Other messages are stores in a buffer before sending. If the ack from the other client does not arrive in 5 seconds the message will be sent again.
(The stop and wait only works for the message, for other control packets it is not.)
- Terminating the connection:
Both clients can terminate the connection during the chat with sending "q" as message. In this case a FIN will be sent to the other client. When the client receives a FIN it will send an ACK and terminate the connection. When the terminating client gets the ACK it will terminate the connection on its wn end as well.
All the messages between the client and the daemon are starting with a byte that indicates the type of the message. Action is taken based on this byte.
An instance of a SIMP protocol server as the packet that will be sent over the network.
In the code the packet is created by setting the fields of the class with readable values.
The encode()
function will translate the packet to a byte array that can be sent over the network.
message = SIMP_Socket(
type='chat',
operation='message',
sequence='request',
user='test',
payload='Hello World'
)
will be encoded to:
b'\x02\x01\x00test\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0bHello World'
There are fix lengths for every field in the packet. The username field contains the username in readable format and the rest is padded up to 32 bytes, that is the max length of the username.
This byte array will be decoded on the other side and translated back to a SIMP_Socket object.