-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionThread.java
More file actions
114 lines (107 loc) · 4.08 KB
/
ConnectionThread.java
File metadata and controls
114 lines (107 loc) · 4.08 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* Class which represents the connection between a client and the server.
* Handles the message flow and extends Thread so the server can be
* multi-threaded.
*/
public class ConnectionThread extends Thread {
// declaring fields
private Socket connection; // client side
private BufferedReader clientIn;
private PrintWriter clientOut;
private final ChatServer server; // server side
private boolean serverClosed = false;
private boolean isUsernameSet = false;
private String userName;
private static final String CONFIRMATION_MESSAGE = "Thank you for joining the server...enjoy!";
/**
* Constructor which creates the data streams between the Client and the server
* @param connection Client side connection
* @param server Server side connection
*/
public ConnectionThread(Socket connection, ChatServer server){
this.server = server;
this.connection = connection;
try {
InputStreamReader r = new InputStreamReader(connection.getInputStream());
// Data input stream from Client to server
clientIn = new BufferedReader(r);
// Data output stream from server to Client
clientOut = new PrintWriter(connection.getOutputStream(), true);
} catch (IOException e){
e.printStackTrace();
}
}
/**
* Set that the server is closed.
*/
public void setServerClosed(){
serverClosed = true;
}
/**
* Return the client side connection
* @return Socket object representing the client side of the connection
*/
public Socket getConnection(){
return connection;
}
/**
* Return the output stream from the server to the client
* @return PrintWriter object representing the output stream from the server to the Client
*/
public PrintWriter getClientOut(){
return clientOut;
}
/**
* Run method of the class. Handles the data flow between a client and the server.
*/
@Override
public void run(){
// set the username of the client
try {
while (!isUsernameSet) {
String username = clientIn.readLine();
if (server.getUserNames().size() != 0) {
if (!server.getUserNames().contains(username)) {
isUsernameSet = true;
server.addUser(username);
userName = username;
clientOut.println(CONFIRMATION_MESSAGE);
break;
}
clientOut.println("Please try again...this username is already taken");
} else {
isUsernameSet = true;
userName = username;
server.addUser(username);
clientOut.println(CONFIRMATION_MESSAGE);
}
}
} catch (IOException e) {
System.out.println("Server has shut down...");
}
// listen for messages from the client and make the server broadcast them
while (!serverClosed){
try{
String userInput = clientIn.readLine();
// check if the user disconnects
if(userInput.equals("/disconnect")){
connection.close();
break;
}
server.printMessageToAllClients(userInput);
} catch (IOException e){
// print to the server console that the user has disconnected
System.out.println("There is something wrong with " + userName +
". Probably the server is shutting down.");
} catch (NullPointerException e){
server.disconnect(userName, this);
serverClosed = true;
}
}
}
}