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:
- Line 1 specifies id of node A.
- Line 2 specifies the name of a computer where the other node (node B) is running.
- Line 3 specifies the port number at which node B "listens".
- Line 4 specifies the port number at which node A "listens".
- Line 5 specifies the name of the log file for node A.
- Line 6 specifies the name of the command file for node A.
Command file
Each line in the command file contains one of the two commands:
- send <node_id>
- sleep <milliseconds>
Program execution
A node reads the configuration file and performs the following:
- Opens the log file.
- Starts "listening" to the port specified in Line 4 in the
configuration file.
- Accepts connections and handles messages received via those
connections.
- Tries to connect to node B. If the attempt fails, node A continues
to try every 10 seconds until the connection is established.
- 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).
- 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:
- List of nodes that this message has traversed, separated by a comma.
- 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
- In all cases of error the program must print an informative error
message to stderr.
- In case of an unrecoverable error (e.g. node cannot read the
configuration file) the program, terminates with exit status 1.
- 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.
- 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).
- 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:
- A string from command file does not contain spaces.
- 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