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 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 cgossipObj 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 ; Port number as a class variable
public static int NodeNumber ; Will have to get this from the fist argument passed.
public static void mainString args throws Exception
System.out.println
Clark Elliott's Gossip Server starting up listening at port GossipStarter.serverPort
;
ConsoleLooper CL new ConsoleLooper; create a DIFFERENT thread
Thread t new ThreadCL;
tstart; and start it waiting for console input
boolean loopControl true; Keeps the datagram listener running.
try
DatagramSocket DGSocket new DatagramSocketGossipStarterserverPort;
Careful: you may have so many datagrams flying around you loose some for lack of buffer size.
System.out.printlnSERVER: Receive Buffer size: DGSocket.getReceiveBufferSize
;
byte incomingData new byte;
InetAddress IPAddress InetAddress.getByNamelocalhost;
while loopControl Use controlC to manually terminate the server.
DatagramPacket incomingPacket new DatagramPacketincomingData incomingData.length;
DGSocket.receiveincomingPacket;
byte data incomingPacket.getData;
ByteArrayInputStream in new ByteArrayInputStreamdata;
ObjectInputStream is new ObjectInputStreamin;
try
GossipData gossipObj GossipData isreadObject;
if gossipObjuserString.indexOfstopserver
System.out.printlnSERVER: Stopping UDP listener now.
;
loopControl false;
System.out.println
SERVER: Gossip object received gossipObj.userString
;
new GossipWorkergossipObjstart; Spawn a worker thread to handle it Listen for more.
catch ClassNotFoundException e
eprintStackTrace;
catch SocketException e
eprintStackTrace;
catch IOException i
iprintStackTrace;
class ConsoleLooper implements Runnable
public void run RUNning the Console listen loop
System.out.printlnCL: In the Console Looper Thread";
BufferedReader in new BufferedReadernew InputStreamReaderSystemin;
try
String someString;
do
System.out.print
CL: Enter a string to send to the gossipServer, or quitstopserver: ;
System.out.flush ;
someString inreadLine ;
if someStringindexOfquit
System.out.printlnCL: Exiting now by user request.
;
System.exit; 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.printlnCL: Preparing the datagram packet now...";
DatagramSocket DGSocket new DatagramSocket;
InetAddress IPAddress InetAddress.getByNamelocalhost;
GossipData gossipObj new GossipData;
gossipObj.userString someString;
ByteArrayOutputStream outputStream new ByteArrayOutputStream;
ObjectOutputStream os new ObjectOutputStreamoutputStream;
oswriteObjectgossipObj;
byte data outputStream.toByteArray;
DatagramPacket sendPacket new DatagramPacketdata data.length, IPAddress, GossipStarter.serverPort;
DGSocket.sendsendPacket;
System.out.printlnCL: Datagram has been sent.";
catch UnknownHostException UH
System.out.println
CL: Unknown Host problem.
; Test by commenting out uncommenting out above.
UHprintStackTrace;
while true;
catch IOException xxprintStackTrace ;
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
