-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAcmeClient.java
66 lines (59 loc) · 1.64 KB
/
AcmeClient.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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class AcmeClient extends AbstractClient {
public AcmeClient(String host, int port) throws IOException {
super(host, port);
}
public void handleMessageFromServer(Object msg) {
// See if server sent a BlobManager
if(msg instanceof Message) {
Message message = (Message) msg;
switch(message.command) {
case "!punch":
System.out.printf("We punched the timeclock.\n");
break;
case "!timesheet":
System.out.printf("Got a timesheet.\n");
break;
default:
System.out.printf("Invalid command.\n");
break;
}
}
}
public void sendMessageToServer(String command) throws IOException {
Message message = new Message(command, null);
switch(message.command) {
case "!punch":
this.sendToServer(message);
break;
case "!timesheet":
this.sendToServer(message);
break;
default:
System.out.printf("Invalid command.\n");
break;
}
}
public static void main(String[] args) {
String host = "localhost";
int port = 5555;
System.out.printf("Client running.\n");
System.out.printf("Client connecting to server on port:\t%d\n", port);
try {
AcmeClient client = new AcmeClient(host, port);
client.openConnection();
BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
String message;
while (true) {
message = fromConsole.readLine();
client.sendMessageToServer(message);
}
} catch(IOException e) {
System.out.println(e);
System.out.printf("ERROR - Can't setup connection, terminating client.\n");
System.exit(1);
}
}
}