//Setup the Container for where we hold details of our peers
PeerGroup ourPeers = PeerGroup.getInstance();
//Create the Sub Threads
Pulse pulseThread = new Pulse("Pulse", myAddress, 2004, latency);
PulseMonitor monitorThread = new PulseMonitor("Monitor", 2004, ourPeers);
RemoteListener messageListener = new RemoteListener(2002);
//Set them to Daemon
pulseThread.setDaemon(true);
monitorThread.setDaemon(true);
messageListener.setDaemon(true);
//Start the threads
pulseThread.start();
monitorThread.start();
messageListener.start();
appConsole.log("Thread:Main - SubThreads Started");
//Setup for user input
boolean loopFlag=true;
BufferedReader stdIn =null;
//Loop until we
while(loopFlag){
//Yield to other threads - make app more responsive
Thread.yield();
stdIn = new BufferedReader(new InputStreamReader(
System.in));
appConsole.log("Thread:Main - Waiting for User Input");
String userInput;
try {
//Print a message and wait for user input
System.out.println("\nPlease Enter a Command - options: add (filen" +
"ame), bye ,get (filename):");
userInput = stdIn.readLine(); // should block until user enters something
//Process the user input
appConsole.log("Thread:Main - In InputReaderWhileLoop");
appConsole.log("Thread:Main - echo input: '" + userInput + "'");
String[] args = userInput.split(" ");
//Decide what to do with each of the commands
if (args[0].equalsIgnoreCase("bye")) {
appConsole.log("Thread:Main - Terminating session");
loopFlag=false;
} else if (args[0].equalsIgnoreCase("add")) {
Message theMessage = new Message();
theMessage.setCommand("add");
theMessage.setFileId(args[1]);
theMessage.setFileContents(new File(args[1]));
theMessage.setSenderHostName(myAddress);
ourPeers.transmitEventToPeers(theMessage, appConsole);
System.out.println("Add Request Sent to other nodes");
}
else if (args[0].equalsIgnoreCase("get")) {
Message theMessage = new Message();
theMessage.setCommand("get");
theMessage.setFileId(args[1]);
theMessage.setSenderHostName(myAddress);
ourPeers.transmitEventToPeers(theMessage, appConsole);
System.out.println("Get Request Sent to other nodes");
}
//Catch and Log Exception
} catch (Exception e) {
e.printStackTrace();
}
}//END LOOP UNTIL 'bye' IS ENTERED
//Close off user input
System.out.println("Closing stdIn");
stdIn.close();
//Try to close down the threads
System.out.println("setting closedown on threads");
pulseThread.setCloseDown(true);
monitorThread.setCloseDown(true);
messageListener.setCloseDown(true);