Advertisement
Guest User

PeerClientThread.java

a guest
Feb 21st, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.BufferedReader;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintStream;
  8. import java.net.Socket;
  9. import java.net.UnknownHostException;
  10. import java.util.Scanner;
  11.  
  12. public class PeerClientThread implements Runnable {
  13.     private Socket client;
  14.     private PrintStream output;
  15.     private BufferedReader input;
  16.     private int port;
  17.     private static String path = "/Users/erikcolin/Documents/CS550_PA1_files/Peer";
  18.     private static Scanner scan = new Scanner(System.in);
  19.     private FileOutputStream fos;
  20.     private BufferedOutputStream bos;
  21.    
  22.     public PeerClientThread() {
  23.         try {
  24.         client = new Socket("localhost",9999);
  25.        
  26.         output = new PrintStream(client.getOutputStream());        
  27.         input = new BufferedReader(new InputStreamReader(client.getInputStream()));
  28.         }
  29.         catch(Exception e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.    
  34.     @Override
  35.     public void run() {
  36.         // TODO Auto-generated method stub
  37.         try {
  38.             int id = 0;
  39.             String server_response;
  40.             String file_name;
  41.             String path;
  42.            
  43.             String message = input.readLine();
  44.             String[] tokens = message.split(" ");
  45.             String pid = tokens[4];
  46.             System.out.println(message);
  47.             System.out.println(pid);
  48.            
  49.                 while(client.isConnected())
  50.                 {
  51.                    
  52.                     System.out.println();
  53.                     System.out.println("Please type in a command for the server from the following:");
  54.                     System.out.println("registry: invoked to register all its files with the indexing server");
  55.                     System.out.println("search: this procedure should search the index and return all the matching peers to the requestor");
  56.                     System.out.println("exit");
  57.                     System.out.println();
  58.                    
  59.                     String comm = scan.nextLine();
  60.                     if(comm.contentEquals("registry"))
  61.                     {
  62.                        
  63.                         System.out.print("Please enter your ID: ");
  64.                         String s = scan.nextLine();
  65.                         id = Integer.parseInt(s);
  66.                        
  67.                         System.out.print("Enter path of the files to sync with indexing server: ");
  68.                         path = scan.nextLine();
  69.                         output.println(comm + " " +id + " " + path);
  70.                        
  71.                     }
  72.            
  73.                     else if(comm.contentEquals("search"))
  74.                     {
  75.                         System.out.print("Please enter the file name: ");
  76.                         file_name = scan.nextLine();
  77.                         output.println(comm + " " +file_name);
  78.                         server_response = input.readLine();
  79.                         retrieve(server_response,pid,file_name);
  80.                     }
  81.            
  82.                     else if(comm.contentEquals("exit"))
  83.                     {
  84.                         System.exit(0);
  85.                     }
  86.            
  87.                     else
  88.                     {
  89.                         System.out.println("Invalid command, please try again");
  90.                     }
  91.            
  92.                 }
  93.            
  94.         }
  95.         catch (Exception e) {
  96.             e.printStackTrace();
  97.         }
  98.     }
  99.     public void retrieve(String s,String pid,String f_name) throws NumberFormatException, UnknownHostException, IOException {
  100.        
  101.         System.out.println("The following peers contain the file you are looking for: ");
  102.         System.out.println("Do you want to connect to one of these peers to retrieve the file? [Y/N]?");
  103.         String ans = scan.nextLine();
  104.         if(ans.contentEquals("Y") || ans.contentEquals("y"))
  105.         {
  106.             System.out.print("Please enter the peer's ID: ");
  107.             String peer_id = scan.nextLine();
  108.             System.out.print("Please enter the peer's port number: ");
  109.             String port_connect = scan.nextLine();
  110.             int current = 0;
  111.             int byteread;
  112.             try
  113.             {
  114.                 Socket p2p = new Socket("localhost", Integer.parseInt(port_connect));
  115.                 PrintStream out = new PrintStream(p2p.getOutputStream());
  116.                 BufferedReader in = new BufferedReader(new InputStreamReader(p2p.getInputStream()));
  117.                     System.out.println(in.readLine());
  118.                     out.println(peer_id);
  119.                     out.println(f_name);
  120.                     //System.out.println(in.readLine());
  121.                     byte [] mybytearray = new byte[6000000];
  122.                     InputStream is = client.getInputStream();
  123.                     fos = new FileOutputStream(path + pid + "/" + f_name);
  124.                     bos = new BufferedOutputStream(fos);
  125.                     byteread = is.read(mybytearray,0,mybytearray.length);
  126.                     current = byteread;
  127.                    
  128.                     do
  129.                     {
  130.                         byteread = is.read(mybytearray,current,(mybytearray.length - current));
  131.                         if(byteread >= 0)
  132.                         {
  133.                             current += byteread;
  134.                         }
  135.                     } while(byteread > -1);
  136.                     bos.write(mybytearray,0,current);
  137.                     bos.flush();
  138.                    
  139.                
  140.             }  
  141.             catch (Exception e) {
  142.                 e.printStackTrace();
  143.             }
  144.            
  145.            
  146.         }
  147.     }
  148.    
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement