Develop a distributed chat without a central coordinators. The processors will f
ID: 3672157 • Letter: D
Question
Develop a distributed chat without a central coordinators.
The processors will form an overlay topology known as a ring. In other words,
every processor will keep only the successor succ(i) as shown in Figure 1.
When a user i wants to talk with a friend user j it floods PUT that
consists of the source id, the destination id as well as the text. We assume
that each user knows the id of the friends.
A user that wants to join the system sends a request REQUEST to a
current participant, say i (if there is any participant, it will act as the only
participant). Let j be the processor that makes the request to i and let i + 1
be the successor of i. When i receives the request, it updates its routing
table by replacing its successor with j and floods a REPLY message to j
with the ip and port of i + 1.
2 Messages
• PUTid: (idsource, iddest, text): Flood the message text.
idsource: initiation id
Figure 1: Ring
iddest: destination Id
text: Message to be sent.
The message is flooded until it reaches either iddest or idsource. If the
message arrives at idsource, then the user was not available.
• REQUESTid: (ip, port): Id provides its own ip and port requests to
join the ring.
ip: IP of the new member.
port: port of the new member.
The peer that receives it, replies to id with the values ipsuccesor, portsuccesor
and updates ipsuccesor, portsuccesor with the values ip, port.
• REPLYidsource: (ip s, port s).
ipsuccesor: IP of the source old succesor.
port: port of the source old succesor.
The new peer receives it and updates its ipsuccesor, portsuccesor with
ips, ports.
• LEAVEidsource: (ip, port, ips, portsuccsesor). : Id is notifying that it
leaves the ring
The message is flooded until it reaches i such that idsource = succ(i) and
updates ipsuccesor, portsuccesor with the received ones ips, portsuccsesor,
3 Flooding
The communication is based on a flooding protocol.
PROTOCOL Flooding .
S ta t u s Values : S = {INITIATOR, IDLE , DONE} ;
$S {INIT} = {INITIATOR, IDLE} ;
$S {TERM} = {DONE}$
R e s t r i c t i o n s : To tal R e l i a b i l i t y , Co n n e c ti vi t y , and
Unique I n i t i a t o r .
INITIATOR
Spon taneou sl y
b egi n
send (M) to N( x ) ;
become DONE;
end
IDLE
R e c ei vi ng ( I )
b egi n
P ro c e s s (M) ;
send (M) to N( x ) ? { s e n d e r } ;
become DONE;
end
Heres the code please complete:
import java.net.*;
import java.io.*;
import java.util.*;
/*****************************//**
* rief It implements a distributed chat.
* It creates a ring and delivers messages
* using flooding
**********************************/
public class Chat {
public enum MSG {
REQUEST,
REPLY,
LEAVE,
PUT
};
// General Message
public class MainMessage implements Serializable {
MSG messageID; // Id of the message REQUEST = 0,
//REPLY = 1, LEAVE =2, PUT = 3
int myPort;
String myIP;
String nextIP;
int nextPort;
String idSource;
String idDest;
String text;
/*
LEAVE : myIP, myPort, nextIP, nextPort
REQUEST : nextIP, nextPort
REPLY : myPort, nextIP
PUT : idSender, idDest, payload
*/
public MainMessage() {
}
}
/*****************************//**
* class Server class "chat.java"
* rief It implements the server
**********************************/
private class Server implements Runnable
{
int port;
public Server(int p)
{
port = p;
}
/*****************************//**
* rief It allows the system to interact with the participants.
**********************************/
public void run() {
ServerSocket servSock = new ServerSocket(port);
while (true)
{
Socket clntSock = servSock.accept(); // Get client connections
ObjectInputStream ois = new
ObjectInputStream(clntSock.getInputStream());
ObjectOutputStream oos = new
ObjectOutputStream(clntSock.getOutputStream());
MainMessage m = (MainMessage)ois.readObject();
// Handle Messages
clntSock.close();
}
}
}
/*****************************//**
* class Client class "chat.java"
* rief It implements the client
**********************************/
private class Client implements Runnable
{
String ip;
int port;
MainMessage m;
public Client(String ipAddress, int p)
{
port = p;
ip = ipAddress;
}
/*****************************//**
* rief It allows the user to interact with the system.
**********************************/
public void run()
{
while (true)
{
// Read commands form the keyboard
//Prepare message m
Socket socket = new Socket(ip, port);
ObjectOutputStream oos = new
ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new
ObjectInputStream(socket.getInputStream());
oos.writeObject(m);
ois.read();
socket.close();
}
}
}
/*****************************//**
* Starts the threads with the client and server:
* param Id unique identifier of the process
* param port where the server will listen
**********************************/
public Chat(String Id, int port) {
// Initialization of the peer
Thread server = new Thread(new Server(port));
Thread client = new Thread(new Client(Id, port));
server.start();
client.start();
client.join();
server.join();
}
public static void main(String[] args) {
if (args.length < 2 ) {
throw new IllegalArgumentException("Parameter: ");
}
Chat chat = new Chat(args[0], Integer.parseInt(args[1]));
}
}
Explanation / Answer
Some observations about your code:
1. Why have you created server and client classes? Problem talks about creating a ring. Its not a client/server architecture.
2. There is no structure for routing table (to hold information about the neighbour) in any of the classes.
3. There is no reference of client in server, so how server and client will interact with each other.
4. You have not defined anything that corresponds to communication protocol mentioned in the problem.
About MainMessage class:
1. What purpose MainMessage class serve in overall setup is not very clear.
2. The function required by you to implement - MainMessage() is the default constructor which can be used just to initialize the data members. How you are going to differentiate which message to generate unless you provide the required parameters.
In my view, you need to redesign your code as per the specifications of the problem. In present form, it doesn't solve the problem.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.