Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.Scanner;
- public class PeerClientThread implements Runnable {
- private Socket client;
- private PrintStream output;
- private BufferedReader input;
- private int port;
- private static String path = "/Users/erikcolin/Documents/CS550_PA1_files/Peer";
- private static Scanner scan = new Scanner(System.in);
- private FileOutputStream fos;
- private BufferedOutputStream bos;
- public PeerClientThread() {
- try {
- client = new Socket("localhost",9999);
- output = new PrintStream(client.getOutputStream());
- input = new BufferedReader(new InputStreamReader(client.getInputStream()));
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- int id = 0;
- String server_response;
- String file_name;
- String path;
- String message = input.readLine();
- String[] tokens = message.split(" ");
- String pid = tokens[4];
- System.out.println(message);
- System.out.println(pid);
- while(client.isConnected())
- {
- System.out.println();
- System.out.println("Please type in a command for the server from the following:");
- System.out.println("registry: invoked to register all its files with the indexing server");
- System.out.println("search: this procedure should search the index and return all the matching peers to the requestor");
- System.out.println("exit");
- System.out.println();
- String comm = scan.nextLine();
- if(comm.contentEquals("registry"))
- {
- System.out.print("Please enter your ID: ");
- String s = scan.nextLine();
- id = Integer.parseInt(s);
- System.out.print("Enter path of the files to sync with indexing server: ");
- path = scan.nextLine();
- output.println(comm + " " +id + " " + path);
- }
- else if(comm.contentEquals("search"))
- {
- System.out.print("Please enter the file name: ");
- file_name = scan.nextLine();
- output.println(comm + " " +file_name);
- server_response = input.readLine();
- retrieve(server_response,pid,file_name);
- }
- else if(comm.contentEquals("exit"))
- {
- System.exit(0);
- }
- else
- {
- System.out.println("Invalid command, please try again");
- }
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void retrieve(String s,String pid,String f_name) throws NumberFormatException, UnknownHostException, IOException {
- System.out.println("The following peers contain the file you are looking for: ");
- System.out.println("Do you want to connect to one of these peers to retrieve the file? [Y/N]?");
- String ans = scan.nextLine();
- if(ans.contentEquals("Y") || ans.contentEquals("y"))
- {
- System.out.print("Please enter the peer's ID: ");
- String peer_id = scan.nextLine();
- System.out.print("Please enter the peer's port number: ");
- String port_connect = scan.nextLine();
- int current = 0;
- int byteread;
- try
- {
- Socket p2p = new Socket("localhost", Integer.parseInt(port_connect));
- PrintStream out = new PrintStream(p2p.getOutputStream());
- BufferedReader in = new BufferedReader(new InputStreamReader(p2p.getInputStream()));
- System.out.println(in.readLine());
- out.println(peer_id);
- out.println(f_name);
- //System.out.println(in.readLine());
- byte [] mybytearray = new byte[6000000];
- InputStream is = client.getInputStream();
- fos = new FileOutputStream(path + pid + "/" + f_name);
- bos = new BufferedOutputStream(fos);
- byteread = is.read(mybytearray,0,mybytearray.length);
- current = byteread;
- do
- {
- byteread = is.read(mybytearray,current,(mybytearray.length - current));
- if(byteread >= 0)
- {
- current += byteread;
- }
- } while(byteread > -1);
- bos.write(mybytearray,0,current);
- bos.flush();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement