/** @param args */ public static void main(String[] args) { System.out.println("Initializing..."); // start peer to peer service new Peer(); // start peer function class PeerFunc peerFunction = new PeerFunc(); // get host's ip and name peerFunction.intialize(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // the client may change the default directory changeDirectory(); // monitor the file, automatically update new AutoUpdate(peerFunction); process1(peerFunction); // process2(peerFunction); try { br.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Bye!"); System.exit(0); }
/** * Compute the average response time per client search request by measuring the response time seen * by a client , there will be 1000 sequential requests. */ public static void process2(PeerFunc peerFuntion) { long startTime1, startTime2, endTime1, endTime2; startTime1 = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { peerFuntion.register(PeerInfo.local.ID, i + ".txt"); } endTime1 = System.currentTimeMillis(); startTime2 = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { peerFuntion.search(i + ".txt"); } endTime2 = System.currentTimeMillis(); System.out.println( "Average running time of registry: " + (endTime1 - startTime1) / 1000 + "ms"); System.out.println("Average running time of search: " + (endTime2 - startTime2) / 1000 + "ms"); }
/** * standard process of using this program * * @param peerFunction */ public static void process1(PeerFunc peerFunction) { String filename = null; boolean exit = false; long startTime, endTime; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner scan = new Scanner(System.in); while (!exit) { System.out.println("\n1 Register a file\n2 Search a file\n3 Exit"); switch (scan.nextInt()) { case 1: { System.out.println("Enter the file name:"); try { filename = br.readLine(); // counting running time of registry startTime = System.currentTimeMillis(); peerFunction.register(PeerInfo.local.ID, filename); endTime = System.currentTimeMillis(); System.out.println("Running time of registry: " + (endTime - startTime) + "ms"); } catch (IOException e) { e.printStackTrace(); } break; } case 2: { System.out.println("Enter the file name:"); try { filename = br.readLine(); // counting running time of search startTime = System.currentTimeMillis(); boolean found = peerFunction.search(filename); endTime = System.currentTimeMillis(); System.out.println("Running time of search: " + (endTime - startTime) + "ms"); if (found) { System.out.println("\n1 Download the file\n2 Cancel and back"); switch (scan.nextInt()) { case 1: // counting running time of download startTime = System.currentTimeMillis(); peerFunction.download(filename); endTime = System.currentTimeMillis(); System.out.println("Running time of download: " + (endTime - startTime) + "ms"); break; case 2: break; default: break; } } } catch (IOException e) { e.printStackTrace(); } break; } case 3: exit = true; break; default: break; } } scan.close(); }