-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.java
110 lines (108 loc) · 4.32 KB
/
Client.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int numServer = sc.nextInt();
String[] hostAddresses = new String[numServer];
int[] tcpPorts = new int[numServer];
//TODO: parse the ips and ports of servers
for (int i = 0; i < numServer; i++) {
String[] line = sc.nextLine().split(":");
hostAddresses[i] = line[0];
tcpPorts[i] = Integer.parseInt(line[1]);
}
int i = 0;
while(sc.hasNextLine()) {
String cmd = sc.nextLine();
String[] tokens = cmd.split(" ");
if (tokens[0].equals("purchase")) {
String userName = tokens[1];
String item = tokens[2];
String quantity = tokens[3];
String toSend = "purchase " + userName + " " + item + " " + quantity;
String messageReceived = null;
while (messageReceived == null && i < numServer)
{
try{
Socket socket = new Socket(hostAddresses[i], tcpPorts[i]);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.writeBytes(toSend + '\n');
messageReceived = in.readLine();
System.out.println("Received from Server: " + messageReceived);
socket.close();
}
catch (UnknownHostException e) {System.err.println(e);}
catch (SocketException e) {System.err.println(e);}
catch (IOException e){System.err.println(e);}
i++;
}
} else if (tokens[0].equals("cancel")) {
String ID = tokens[1];
String toSend = "cancel " + ID;
String messageReceived = null;
while (messageReceived == null && i < numServer)
{
try{
Socket socket = new Socket(hostAddresses[i], tcpPorts[i]);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.writeBytes(toSend + '\n');
messageReceived = in.readLine();
System.out.println("Received from Server: " + messageReceived);
socket.close();
}
catch (UnknownHostException e) {System.err.println(e);}
catch (SocketException e) {System.err.println(e);}
catch (IOException e){System.err.println(e);}
i++;
}
} else if (tokens[0].equals("search")) {
String user = tokens[1];
String toSend = "search " + user;
String messageReceived = null;
while (messageReceived == null && i < numServer)
{
try{
Socket socket = new Socket(hostAddresses[i], tcpPorts[i]);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.writeBytes(toSend + '\n');
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
socket.close();
}
catch (UnknownHostException e) {System.err.println(e);}
catch (SocketException e) {System.err.println(e);}
catch (IOException e){System.err.println(e);}
i++;
}
} else if (tokens[0].equals("list")) {
boolean messageReceived = false;
while (messageReceived == false && i < numServer)
{
try{
Socket socket = new Socket(hostAddresses[i], tcpPorts[i]);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.writeBytes(tokens[0] + '\n');
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
socket.close();
messageReceived = true;
}
catch (UnknownHostException e) {System.err.println(e);}
catch (SocketException e) {System.err.println(e);}
catch (IOException e){System.err.println(e);}
i++;
}
} else {
System.out.println("ERROR: No such command");
}
}
}
}