Package main

Source Code of main.DistFileServer

package main;

import peer.*;
import main.ui.ILog;
import main.ui.NodeConsole;
import network.*;

import network.Message;
import network.Pulse;
import network.PulseMonitor;
import network.Transmitter;

import java.io.*;
import java.net.InetAddress;

import util.*;

public class DistFileServer {

  //Handle to the on screen pulse display
  static NodeConsole appConsole = null;

  public static void main(String[] argv) throws Exception {
    if (argv.length == 0) {
      System.out.println("Latency value was ommitted, please supply and restart");
      return;
    }
   
    int lategcy = Integer.parseInt(argv[0]);
    // setup the main logging console
    appConsole = new NodeConsole();
    appConsole.start("Autonomic Application and Requests Out",
        "Log of main application messages\n");

    //Call the method to start the thread
    appConsole.log("Thread:Main - Starting");
    DistFileServer theServer = new DistFileServer();
    theServer.go(lategcy);
    appConsole.log("Thread:Main - Ended");
  }

  private void go(int latency) throws IOException {
   
    //Get my Local Domain Name and Address
    String myAddress = null;
    try {
      myAddress = InetAddress.getLocalHost().getHostAddress();
    } catch (Exception e) {
      e.printStackTrace();
    }
    appConsole.log("Thread:Main - PCAddress: " + myAddress);
    appConsole.log("Thread:Main - Starting SubThreads");

    //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);
     
   
   
    appConsole.log("Thread:Main - SubThreads completed");
    System.out.println("Node Stopped - press CTRL-C or close GUI when ready");
  }
 
 
/*
  private void testAddMessage(PeerGroup peers) throws FileNotFoundException,
      IOException {
    Message testMessage = new Message();
    testMessage.setCommand("add");
    testMessage.setFileId("testfile.txt");
    testMessage.setFileContents(new File("testfile.txt"));

    peers.transmitEventToPeers(testMessage, new Log());
  }
*/

}
 
TOP

Related Classes of main.DistFileServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.