-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyClient.java
executable file
·143 lines (138 loc) · 4.18 KB
/
MyClient.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.io.IOException;
/**
* This class creates a client and is used by ClientConsole
*
* @author Dr. Dave Gibson
* @version February 2017
*/
public class MyClient extends AbstractClient {
// Instance variables **********************************************
/**
* Reference to the client UI
*/
ClientConsole clientUI;
// Constructors ****************************************************
/**
* Constructs an instance of the client.
*
* @param host The server to connect to.
* @param port The port number to connect on.
* @param clientUI The interface type variable.
*/
public MyClient(String host, int port, ClientConsole clientUI) throws IOException {
super(host, port);
this.clientUI = clientUI;
clientUI.display("Type '#connect' to connect to server");
//openConnection();
}
// Instance methods ************************************************
/**
* This method handles all data that comes in from the server.
*
* @param msg The message from the server.
*/
public void handleMessageFromServer(Object msg) {
// See if server sent a BlobManager
if(msg instanceof BlobManager) {
BlobManager blobs = (BlobManager)msg;
clientUI.display("BlobManager received from server says total is:" + blobs.getTotal());
clientUI.display("Complete BlobManager:\n" + blobs + "\n");
}
// See if server sent a Blob
else if(msg instanceof Blob) {
Blob blob = (Blob)msg;
clientUI.display("Blob received from server:" + blob + "\n");
}
// Else, display whatever the server sent.
else {
clientUI.display("Message from server:" + msg.toString() + "\n");
}
}
/**
* This method handles all data coming from the UI
*
* @param message The message from the UI.
* @throws InterruptedException
*/
public void handleMessageFromClientUI(String message) throws InterruptedException {
// See if client wants to connect
if(message.equals("#connect")) {
try {
this.openConnection();
clientUI.display("Connection open.");
}
catch(IOException e) {
clientUI.display("ERROR - Cannot establish connection with server" );
return;
}
}
// Close the connect, but don't quit.
else if(message.equals("#close")) {
try {
//closeConnection();
sendToServer(message);
clientUI.display("Connection closed.");
}
catch(IOException e) {
clientUI.display("ERROR - Cannot close connection.");
}
}
// See if client is connected to server.
else if(message.equals("#isconnected")) {
clientUI.display("Connection to server=" + this.isConnected());
}
// See if client UI sent a series of blobs
else if(message.startsWith("#blobs")) {
// Extract integers as a string that occur after command
String data = message.substring(7);
String[] strNums = data.split(" ");
// Create a BlobManager, create Blobs, add them, and then send BlobManager to server.
BlobManager blobs = new BlobManager();
for(String strNum : strNums) {
blobs.addBlob(new Blob(Integer.parseInt(strNum)));
}
//transportObject = new Blob(Integer.parseInt(data));
try {
sendToServer(blobs);
clientUI.display("BlobManager sent to server");
}
catch(IOException e) {
clientUI.display("ERROR - Could not send BlobManager to server");
}
}
// See if client UI sent a single blob
else if(message.startsWith("#blob")) {
String data = message.substring(6);
Blob b = new Blob(Integer.parseInt(data));
try {
sendToServer(b);
clientUI.display("Blob sent to server");
}
catch(IOException e) {
clientUI.display("ERROR - Could not send Blob to server.");
}
}
// See if client wants to retrieve BlobManager store in client's HashMap on server.
else if(message.startsWith("#getBMan")) {
try {
sendToServer(message);
clientUI.display("Request sent to server for BlobManager.");
}
catch(IOException e) {
clientUI.display("Error - Could not send message to server");
}
}
// See if client wants to quit program.
else if(message.equals("#quit")) {
try {
closeConnection();
clientUI.display("Client shutting down...");
Thread.sleep(3000);
System.exit(0);
}
catch(IOException e) {
System.exit(1);
}
}
}
}