Ex1 in the Computer Communications Course

Last updated on March 5



Exercise 1



Contents:



Target and Motivation

In this exercise you will design and implement a protocol for peer-to-peer network. For many years the client-server approach has been dominating in network applications. Recently a new approach becomes more and more popular. In this approach the protocol participant has functionality of both the server and the client. You are required to design a program for simple peer-to-peer network.

Node

Every participant in this protocol is called a node. Every node has its unique identity. The nodes communicate using TCP/IP connections. When a node starts running, it receives one argument. The argument is the name of the configuration file. Each line in this file sets a certain value for a parameter of the corresponding node (node A).

Configuration file

Below is the description of the configuration file:

Command file

Each line in the command file contains one of the two commands:

Program execution

A node reads the configuration file and performs the following:
  1. Opens the log file.
  2. Starts "listening" to the port specified in Line 4 in the configuration file.
  3. Accepts connections and handles messages received via those connections.
  4. Tries to connect to node B. If the attempt fails, node A continues to try every 10 seconds until the connection is established.
  5. Reads instructions line-by-line from the commands file and executes them. When a node reads the "send" instruction from the command file, it uses the protocol to send a message to the <node_id>. Each such message includes its number (1 is the number of the first message. 2 is the number of the second message, etc). Protocol should be able to deliver a message to any node (node C), even if node A has no direct link to node C. Please look in error handling section, to know how to manage messages that cannot be delivered to the destination (e.g. all connections of the node had failed).
  6. If the node reads "sleep" command, it waits for a specified number of milliseconds before reading the next command. When the end of the command file is reached, the node terminates with exit status 0. Note that items 2-5 are to be handled simultaneously.
When a message reaches the node to which it was sent, the latter sends an acknowledgement to the sender and writes the following information in the log file:
  1. List of nodes that this message has traversed, separated by a comma.
  2. The number of the message. The format of each line in the log file is the following:
In case of a regular message received by the DESIGNATED RECEIVER, the receiver adds:
<nodes><space><number>
In case of an acknowledgement received by the SENDER (the receiver of the ack i.e. the regular message SENDER) adds:
<nodes><space><ACK><space><number>
In both cases, the nodes are listed in the order in which the message/acknowledgement has traversed them. See also the example files.

Relevant Java classes

Socket, ServerSocket, DataInputStream, DataOutputStream, Thread.

Error handling

  1. In all cases of error the program must print an informative error message to stderr.
  2. In case of an unrecoverable error (e.g. node cannot read the configuration file) the program, terminates with exit status 1.
  3. If a TCP connection fails, an error message is to be printed to System.err and the node that initiated the connection has to try to restore it every 10 seconds.
  4. If a message or its acknowledgement cannot be delivered to its destination, the message is to be discarded (nothing is to be printed to log or System.err).
  5. If a node receives a message twice, it discards the message. This should also prevent from a message to travel in the network in circles.

Question

Why DataInputStream is recommended to be used for exchange information between two computers?

General

You may assume that:
  1. A string from command file does not contain spaces.
  2. There are no two nodes with the same name (id).

File examples

nodeA.cfg nodeB.cfg nodeC.cfg nodeD.cfg
nodeA.exe nodeB.exe nodeC.exe nodeD.exe
nodeA.log nodeB.log nodeC.log nodeD.log

Submission guidelines

You may do this exercise in pairs. This exercise is to be done in Java. There is no need to write Makefile for this exercise. You should submit ex1.tar file with your sources (main function has to be in Node.java) and README file. In the README, you have to put your login(s) in the first line. You are also required to give short description of your solution and answer the question above. Please follow the general instructions in the course guidelines.

Go Back to the Computer Communications Course Home Page