-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrokerThread.java
More file actions
82 lines (71 loc) · 2.63 KB
/
BrokerThread.java
File metadata and controls
82 lines (71 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class BrokerThread implements Runnable {
String ip;
String port;
public BrokerThread(String ip, String port) {
this.ip=ip;
this.port=port;
}
public void run() {
//SET UP
try {
while(true){
DatagramSocket clientSocket = new DatagramSocket(); // for network
BufferedReader fromKeyboard = new BufferedReader(new InputStreamReader(System.in)); // from keyboard
InetAddress serverAddress = InetAddress.getByName(ip); // Server address
int servPort = Integer.parseInt(port); // get port number
String clientInput; // from user
//SEND COMMAND TO SERVER
clientInput = fromKeyboard.readLine();
byte[] buf = clientInput.getBytes(); // byte array for data to SEND
// create a UDP (datagram) packet TO send to server
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddress, servPort);
clientSocket.send(packet); // now send it
//RECIEVE PACKET FROM SERVER
byte[] rbuf = new byte[256]; // new byte array to RECEIVE from server
packet = new DatagramPacket(rbuf, rbuf.length);
clientSocket.receive(packet); // receive it
String serverData = new String(packet.getData(),0,packet.getLength());
//ACTIONS
//SEARCH FOR FILE
if(clientInput.contains("search")) {
if(!serverData.equals("File could not be found")){
System.out.println("please select which client you want to request the "
+ "file from by typing the ip and port separated by a space: ");
System.out.println(serverData);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String cin = keyboard.readLine(); //IP address
// CONNECT TO PEER AND RECIEVE
String[] arr = cin.split(" ");
Thread request = new Thread(new RecieveFileThread(arr[0], arr[1], clientInput.substring(7, clientInput.length())));
request.start();
}
}
//RATE THE FILE FOR A GIVEN IP
else if(clientInput.contains("rate")) {
System.out.println(serverData);
}
//REGISTER A NEW FILE
else if(clientInput.contains("register")) {
System.out.println(serverData);
}
//UNREGISTER FILE
else if(clientInput.contains("unregister")) {
System.out.println(serverData);
}
//HANDLE INVALID COMMAND
else {
System.out.println("Please enter a valid command");
}
}
}
catch(IOException e){
System.err.println("IOException");
}
}
}