COSC 6377 : Computer Networks

Spring 2020

MW 4-530pm at F 162

Homework 1: Message Switching

Due: 3/15/2020

In this homework, we will write an application-level message switching system. Conceptually, this setup has many elements of a packet switching system.

In a typical message switching system, there are two types of entities. First, we have a switch that keeps track of different devices that are connected and accessible through it. Second, the devices or nodes that connect to the switch. In our setup, there are two types of nodes -- client, which needs to send messages to the server, which is the second type of node. The switch and client/server are implemented in Python using basic socket interface.

In our setup, we have a single switch and multiple clients and servers. The clients send text messages to any server identified by an ID and the server responds to the client with a SHA1 hash of the message. The client can then check if the server received the message correctly and display an appropriate transmission status message on the terminal.

The Packet (Message)

Network packets are typically sent in binary format, which are compact. In this homework, we will send our messages in JSON format for convenience.

{
  "type": 0,          // 0 is a message from a client to a server
  "srcid": 999,       // source (client) id
  "destid": 999,      // destination (server) id
  "payloadsize": 999, // payload size
  "payload": "xyz"    // payload
}

The Client

The client wants to send a message to the server. It knows the ID of the server but does not know the port on which the server is running so is not able to open a socket to the server. It uses TCP socket to connect to the switch and sends the packet to the switch expecting the switch to forward the message to the server.

Here is how to run the client:

  python client.py -id ID -switch PORT -pkt PKTFILE
  

Replace ID with the client for the client. Replace PORT with the port number on which switch is accepting connections. Replace PKTFILE with the name of the file in which you have the message in JSON format.

The Switch

The switch listens for incoming connections from the clients and servers on a well-known port.

When a server connects to the switch, the incoming connnection is immediately followed by this setup message:

{
  "type": 1          // 1 is a connection setup message from a server
  "id": 999,         // id of the server
  "listenport": 999  // port on which the server is listening
}

When the switch receives this setup message, it creates a message switching table that records the id of the server and the port number on which the server is listening.

When a switch receives a message of type 0, it looks up in its message switching table for the correct port number, and sends the message to that port. That is how the switch forwards the message from the client to the server.

Here is how we run the switch:

  python switch.py -port PORT
  

Replace PORT with a port number. This is the well-known port the clients and the servers will use to connect to the switch.

The Server

The server listens for a message of type 0, which is a message from the client. It grabs the text in the payload and computes SHA1 hash of the message and sends it back to the client in the payload of the acknowledgment message. Here is what the message looks liks:

{
  "type": 2,          // 2 is an ACK from a server to a client
  "srcid": 999,       // source (server) id
  "destid": 999,      // destination (client) id
  "payloadsize": 999, // payload size
  "payload": "xyz"    // payload
}

Here is how we run the server:

python server.py -id ID -listen LPORT -switch SPORT

Replace ID with the id number for the server. Replace LPORT with the port on which the server listens for messages. Replace SPORT with the well-known port on which the switch is running.

Running the system

Setup the network by running the switch first, then server, then client(s). To make it easy to see how each system works, you can run them on different terminals.

The messages on the switch terminal may look something like this.

% python switch.py -port 123

Running the switch on port 123

Receiving setup message from server id 100 port 124

Received a data message from client 50 payload: {.....}

Forwarding a data message to server id 100 payload: {.....}

Received a data message from server id 100 payload: {.....}

Forwarding a data message to client id 50 payload: {....}

The messages on the server terminal may look something like this.

% python server.py -id 100 -listen 124 -switch 123

Server running with id 100

Listening on port 124

Connecting to the switch on port 123

Received a message from client 50 payload: {......}

Sending a response to the client 50 payload: {......}

The messages on the client terminal may look something like this.

% python client.py -id 50 -switch 123 -pkt mypkt.txt

Sending message {......} to server 100 through switch running on port123

Receiving a response from the server 100 payload: {.....}

Submission

Please upload your code using git invitation for hw1. Please include a README that describes the author, contact information, a short description of the software, and finally limitations of your implementation. Include any interesting observations, measurement results, etc. in your README.