-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandler.java
More file actions
255 lines (229 loc) · 5.33 KB
/
MessageHandler.java
File metadata and controls
255 lines (229 loc) · 5.33 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.Scanner;
public class MessageHandler
{
private ObjectOutputStream out;
private ObjectInputStream in;
private Scanner scanner;
public MessageHandler(Socket socket)
{
try
{
this.out = new ObjectOutputStream(socket.getOutputStream());
this.out.flush();
this.in = new ObjectInputStream(socket.getInputStream());
}
catch (IOException e)
{
System.out.println("MessageHandler " + e.getMessage());
}
scanner = new Scanner(System.in);
}
/**
* Send String over the network
*/
public void sendString(String msg)
{
try
{
out.writeObject(msg);
out.flush();
}
catch (Exception e)
{
System.out.println("sendString " + e.getMessage());
}
}
/**
* Send int over the network
*/
public void sendNumber(int msg)
{
try
{
out.writeObject(msg);
out.flush();
}
catch (Exception e)
{
System.out.println("sendNumber " + e.getMessage());
}
}
/**
* Send Boolean over the network
*/
public void sendBoolean(Boolean state)
{
try
{
out.writeObject(state);
out.flush();
}
catch (Exception e)
{
System.out.println("sendBoolean " + e.getMessage());
}
}
/**
* Read a string sent over the network
*/
public String readString()
{
try
{
return (String) in.readObject();
}
catch (Exception e)
{
System.out.println("readString " + e.getMessage());
}
return "";
}
/**
* Read an int sent over the network
*/
public int readNumber()
{
try
{
return (int) in.readObject();
}
catch (Exception e)
{
System.out.println("readNumber " + e.getMessage());
}
return -1;
}
/**
* Read a bool sent over the network
*/
public Boolean readBoolean()
{
try
{
return (Boolean) in.readObject();
}
catch (Exception e)
{
System.out.println("readBoolean " + e.getMessage());
}
return false;
}
public int requestNumber(String prompt)
{
sendString(prompt);
return readNumber();
}
public String requestString(String prompt)
{
sendString(prompt);
return readString();
}
public LocalDateTime requestDate(String prompt)
{
String result = "";
Boolean invalidInput = true;
while (invalidInput)
{
result = requestString(prompt);
invalidInput = false;
try
{
LocalDateTime.parse(result);
}
catch (Exception e)
{
invalidInput = true;
}
sendBoolean(invalidInput);
}
return LocalDateTime.parse(result);
}
/**
* Requests a number in a range rerequesting it if the user sends a wrong number
*
* @param min Range inclusive
* @param max Range inclusive
* @param prompt The prompt to show the client
* @return Returns only the valid number the user has sent back
*/
public int requestNumber(String prompt, int min, int max)
{
int result = -1;
Boolean invalidInput = true;
while (invalidInput)
{
result = requestNumber(prompt);
invalidInput = result < min || result > max;
sendBoolean(invalidInput);
}
return result;
}
public int handleNumberFailable()
{
int result = 0;
Boolean failed = true;
while (failed)
{
result = handleNumber();
failed = readBoolean();
}
return result;
}
public int handleNumberFailable(String m)
{
int result = 0;
Boolean failed = true;
while (failed)
{
result = handleNumber();
failed = readBoolean();
if (failed)
{
System.out.println(m);
}
}
return result;
}
public String handleDate()
{
String result = "";
do
{
result = handleString();
// 0.4. Check bool for error
} while (readBoolean());
return result;
}
public int handleNumber()
{
System.out.print(readString());
int response;
try
{
response = Integer.parseInt(scanner.nextLine());
}
catch (Exception e)
{
response = -1;
}
sendNumber(response);
return response;
}
/**
* Request a string from the user
*
* @return What the user has input that is sent over the network
*/
public String handleString()
{
System.out.print(readString());
String response = scanner.nextLine();
sendString(response);
return response;
}
}