Question: import java.io . * ; import java.net. * ; class GossipData implements Serializable { / / Must be serializable to send 1 bit after another

import java.io.*;
import java.net.*;
class GossipData implements Serializable{// Must be serializable to send 1 bit after another over the network.
int nodeNumber;
int average;
int highValue;
int lowValue;
String userString;
// Whatever else you might want to send.
}
class GossipWorker extends Thread {// Class definition. Many of these worker threads may run simultaneously.
GossipData gossipObj; // Class member, gossipObj, local to GossipWorker.
GossipWorker (GossipData c){gossipObj = c;}// Constructor, assign arg c to local object
public void run(){// Do whatever you like here:
System.out.println("
GW: In Gossip worker: "+ gossipObj.userString +"
");
}
}
public class GossipStarter {
public static int serverPort =45565; // Port number as a class variable
public static int NodeNumber =0; // Will have to get this from the fist argument passed.
public static void main(String[] args) throws Exception {
System.out.println
("Clark Elliott's Gossip Server 1.0 starting up, listening at port "+ GossipStarter.serverPort +".
");
ConsoleLooper CL = new ConsoleLooper(); // create a DIFFERENT thread
Thread t = new Thread(CL);
t.start(); //...and start it, waiting for console input
boolean loopControl = true; // Keeps the datagram listener running.
try{
DatagramSocket DGSocket = new DatagramSocket(GossipStarter.serverPort);
// Careful: you may have so many datagrams flying around you loose some for lack of buffer size.
System.out.println("SERVER: Receive Buffer size: "+ DGSocket.getReceiveBufferSize()+"
");
byte[] incomingData = new byte[1024];
InetAddress IPAddress = InetAddress.getByName("localhost");
while (loopControl){// Use control-C to manually terminate the server.
DatagramPacket incomingPacket = new DatagramPacket(incomingData, incomingData.length);
DGSocket.receive(incomingPacket);
byte[] data = incomingPacket.getData();
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
try {
GossipData gossipObj =(GossipData) is.readObject();
if (gossipObj.userString.indexOf("stopserver")>-1){
System.out.println("SERVER: Stopping UDP listener now.
");
loopControl = false;
}
System.out.println("
SERVER: Gossip object received ="+ gossipObj.userString +"
");
new GossipWorker(gossipObj).start(); // Spawn a worker thread to handle it. Listen for more.
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
} catch (SocketException e){
e.printStackTrace();
} catch (IOException i){
i.printStackTrace();
}
}
}
class ConsoleLooper implements Runnable {
public void run(){// RUNning the Console listen loop
System.out.println("CL: In the Console Looper Thread");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
String someString;
do {
System.out.print
("CL: Enter a string to send to the gossipServer, (or, quit/stopserver): ");
System.out.flush ();
someString = in.readLine ();
if (someString.indexOf("quit")>-1){
System.out.println("CL: Exiting now by user request.
");
System.exit(0); // Ugly way to stop. You can fix with a more elegant throw.
}
// Trigger some action by sending a datagram packet to ourself:
try{
System.out.println("CL: Preparing the datagram packet now...");
DatagramSocket DGSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
GossipData gossipObj = new GossipData();
gossipObj.userString = someString;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(outputStream);
os.writeObject(gossipObj);
byte[] data = outputStream.toByteArray();
DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddress, GossipStarter.serverPort);
DGSocket.send(sendPacket);
System.out.println("CL: Datagram has been sent.");
} catch (UnknownHostException UH){
System.out.println("
CL: Unknown Host problem.
"); // Test by commenting out / uncommenting out above.
UH.printStackTrace();
}
} while (true);
} catch (IOException x){x.printStackTrace ();}
}
Features of the assignment:
There is no central control node. All nodes are equal and run from exactly the same code. Node number is determined by an argument passed to the JVM at run time.
You must implement at least two threads at each node, because you must always be ready to accept console input from a user, and also, in the background, perform network gossip calcuations. That is, when performing the work of a gossip cycle, your acceptance of console input must remain active. You can have as many threads of excution as you like.
Each node should be started in its own terminal window, so you do}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!