-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
323 lines (301 loc) · 10.1 KB
/
Server.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package chat;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
/**
* Server class for Socket Programmed Chat
*
* @author Robert Scott - 2018
*/
public class Server {
int numConnections; // number of connections
ArrayList<UserThread> listClients; // list of clients/connections
boolean validConnection; // if server connection is alive ornot
Room chatLog; // for logging purposes
/**
* constructor for Server class
*/
private Server() {
validConnection = true; // initially assume connection is valid
listClients = new ArrayList<>(); // init list connections
chatLog = new Room("./log.txt"); // create or load log
}
/**
* method to launch server, establishing connection
*/
private void launch() {
try {
sendServer("Setting up server...");
// accept new connection requests from clients
try (ServerSocket requests = new ServerSocket(5000)) {
sendServer("Open for user connections.");
// if connection is still valid, take clients
while (validConnection) {
Socket sock = requests.accept();
addClient(sock); // append to list of clients
}
}
} catch (IOException e) {
sendServer("Error setting up server.");
sendServer(e);
}
/**
* if you reach this point, the server is dead anyways, so ensure it's
* closed properly by closing all of the streams, etc
*/
kill();
}
/**
* method which kills all server processes
*/
private void kill() {
/**
* for each client, make sure it's closed
*/
listClients.forEach((c) -> {
c.disconnect(); // close socket and streams of client
});
}
/**
* method which adds a client to the list of clients
*
* @param s - the client socket
*/
private void addClient(Socket s) {
numConnections++; // more connections, increment this
UserThread newClient = new UserThread(s); // create new
listClients.add(newClient); // append to list
newClient.start(); // start client thread
}
/**
* method which sends a message to the server console
*
* @param m - the message to send
*/
private void sendServer(String m) {
System.out.println(" * " + m);
/**
* only write server messages to log. This includes server status
* messages as well as chats. Does not include private messages.
*/
chatLog.writeLog(m);
}
/**
* overloaded method for sending to server. This one is meant for exceptions
* and is not written to log
*
* @param e - the exception to send to server
*/
private void sendServer(Exception e) {
System.out.println(" * " + e);
}
/**
* method to send a message to all connected clients. Synchronized so
* multiple clients cannot block each other.
*
* @param m - the message to send
*/
private synchronized void sendAll(String m) {
// for each client, do this
listClients.forEach((c) -> {
try {
c.outStream.writeObject(m);
} catch (IOException e) {
/**
* if sending failed, assume user has disconnected and log them
* out
*/
logOut(c.clientId);
}
});
sendServer(m); // also send to server console
}
/**
* method which sends a message to a particular client/user
*
* @param u - the user to send to
* @param m - the message to send
* @param s - the sender
*/
private synchronized void sendClient(String u, String m, UserThread s) {
boolean foundUser = false; // assume user doesn't exist initially
for (UserThread c : listClients) {
if (c.getUsername().equals(u)) { // if found
foundUser = true;
try { // try to send them the message
c.outStream.writeObject(m);
} catch (IOException e) { // if failed, assume disconnected
logOut(c.clientId); // log them out
// let yourself know it failed
sendSelf("Unable to send message to " + u + ".", s);
} finally { // in either case, don't continue searching for user
break;
}
}
}
if (!foundUser) { // if cannot find
sendSelf("User " + u + " not found.", s);
} else { // let yourself know it worked
sendSelf("Message sent to " + u + ".", s);
}
}
/**
* method to send message to self
*
* @param m - the message to send
* @param c - the client who is to receive it
*/
private synchronized void sendSelf(String m, UserThread c) {
try {
c.outStream.writeObject(" * " + m);
} catch (IOException e) {
sendServer("Error sending message to self.");
sendServer(e);
}
}
/**
* method which checks which users are online
*
* @return the online users in a concatenated string
*/
private String checkOnline() {
String toReturn = "";
int i = 0;
for (UserThread c : listClients) {
toReturn += c.getUsername();
i++;
if (i != listClients.size()) {
toReturn += ", ";
}
}
return toReturn;
}
/**
* method to log out a specific client
*
* @param id - the client to log out
*/
private synchronized void logOut(int id) {
int i = 0;
for (UserThread c : listClients) {
if (c.clientId == id) {
listClients.remove(i);
c.validClient = false; // invalidate client
sendAll(c.getUsername() + " has disconnected.");
break; // don't continue looking for him
}
i++;
}
}
/**
* main driver for Server class
*
* @param args - unused
*/
public static void main(String[] args) {
Server server = new Server(); // init server
server.launch(); // launch it
}
/**
* UserThread class
*/
class UserThread extends Thread {
int clientId; // id for this class
Message clientMessage; // used later, hold messages
boolean validClient; // if client is alive or not
Socket sock; // socket
String username; // client's user's username
ObjectInputStream inStream; // in socket stream
ObjectOutputStream outStream; // out socket stream
/**
* constructor for UserThread class
*
* @param s - socket user is connecting from
*/
UserThread(Socket s) {
validClient = true; // assume connection is viable
clientId = numConnections; // easy way to number clients
sock = s; // init socket
try {
// init socket streams
outStream = new ObjectOutputStream(s.getOutputStream());
inStream = new ObjectInputStream(s.getInputStream());
// get username after signed in
username = (String) inStream.readObject();
// let everyone know you joined
sendAll(username + " has joined the chat room.");
} catch (IOException | ClassNotFoundException e) {
sendServer("Error connecting client.");
sendServer(e);
}
}
/**
* getter method to return client username
*
* @return the client's username
*/
private String getUsername() {
return username;
}
/**
* main thread method
*/
@Override
public void run() {
while (validClient) { // while client is alive
try {
// get messages
clientMessage = (Message) inStream.readObject();
} catch (IOException | ClassNotFoundException e) {
validClient = false; // if unable to, kill client
break;
}
// interpret message to send it to correct spot
decipherMessage(clientMessage.getMessage());
}
// if you hit here, client is dead and can be logged out
logOut(clientId);
disconnect(); // disconnect its socket and streams
}
/**
* method to disconnect client from socket and streams
*/
void disconnect() {
try {
inStream.close();
outStream.close();
sock.close();
} catch (IOException e) {
System.out.println(" * Error disconnecting from server.");
System.out.println(e);
}
}
/**
* method to interpret type of message to send it to correct spot
*
* @param m - the message to send
*/
private void decipherMessage(String m) {
// find substring of message containing only message contents
String mS = m.split(" ", 4)[3];
if (mS.equalsIgnoreCase("/logout")) { // if '/logout' said
logOut(clientId); // log self out
} else if (mS.equalsIgnoreCase("/online")) { // if '/online' said
try {
// write to self who is online
outStream.writeObject(checkOnline());
} catch (IOException e) {
sendServer("Error reading online list.");
sendServer(e);
}
} else if (mS.charAt(0) == '@') { // if private message
sendClient(mS.split(" ", 2)[0].substring(1), m, this);
} else { // otherwise public
sendAll(m);
}
}
}
}