Repository navigation
Expand file tree
/
Copy pathClientHandler.java
More file actions
135 lines (122 loc) · 3.53 KB
/
Copy pathClientHandler.java
File metadata and controls
135 lines (122 loc) · 3.53 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ClientHandler implements Runnable{
private String chatid = "";
private Socket clientSocket;
private BufferedReader input;
private PrintWriter output;
private ArrayList<ClientHandler> clients;
private int id;
private Lock lock = new ReentrantLock();
private Condition lockCondition = lock.newCondition();
public ClientHandler(Socket socket, ArrayList<ClientHandler> client, int idnum) throws IOException {
this.clientSocket = socket;
this.clients = client;
this.id = idnum;
input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
output = new PrintWriter(clientSocket.getOutputStream(), true);
}
public void run() {
// we may want to use a lock here to add the name in the arraylist the server holds
lock.lock();
try {
this.chatid = getID();
SendEntryMessageToAll(chatid);
server.AddName(chatid);
}finally {
lock.unlock();
}
output.println("Welcome " + chatid + "!");
try {
while(true){
String clientMessage = input.readLine();
if(clientMessage.toUpperCase().equals("LOGOUT")) {
// This is where we need to use locks so that when we try to remove
// we need to make sure the arraylist isnt in use so that we can remove
// without any errors
lock.lock();
try {
server.RemoveClient(this.id, this.chatid);
SendExitMessageToAll(chatid);
}finally {
lock.unlock();
}
//
break;
}
else if(clientMessage.equals("/list")) {
lock.lock();
try{
ListClientNames(this.id);
}finally {
lock.unlock();
}
}
else {
// Here we want to send the message to everyone
SendMessageToAll(clientMessage, chatid);
}
}
}catch (IOException e) {
System.err.println("IOException in client handler!");
e.printStackTrace();
} finally {
output.close();
try {
System.out.println("Closing input!");
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void SendMessageToAll(String message, String cid) {
for(ClientHandler client: clients){
client.output.println("[" + cid + "]: " + message);
}
}
private void SendExitMessageToAll(String cid) {
for(ClientHandler client: clients){
client.output.println(cid + " is leaving the chat room");
}
}
private void ListClientNames(int id) {
output.println("---------- List ----------");
for(String name: server.names) {
output.println(name + " ");
}
}
private void SendEntryMessageToAll(String cid) {
for(ClientHandler client: clients){
client.output.println(cid + " has entered the chat room.");
}
}
public String getID() {
output.println("Enter a chat id: ");
String chatid = "";
try {
chatid = input.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(server.names.contains(chatid)) {
output.println("ChatID is in use, enter a unique ID: ");
try {
chatid = input.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return chatid;
}
}