-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathConnectionHandler.java
169 lines (141 loc) · 4.76 KB
/
ConnectionHandler.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
package Server;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class ConnectionHandler extends Thread {
private HashMap<String,ClientHandler> clients = new HashMap<String,ClientHandler>(); //Username,Client
private ArrayList<String> userList = new ArrayList<String>();
private Boolean isActive = true;
private Boolean newUser = true;
private ServerSocket s;
private String host = "Admin-PC";
private int portNumber;
private PrintWriter s_out;
private BufferedReader s_in;
private String serverName;
private Socket conn;
public ConnectionHandler(){
portNumber = 5000;
serverName = "Test";
super.start();
}
public ConnectionHandler(String serverName,int portNumber){
this.portNumber = portNumber;
this.serverName = serverName;
super.start();
}
public void run(){
while(isActive){
try{
s = null;
conn = null;
try{
//1. creating a server socket - 1st parameter is port number and 2nd is the backlog
InetAddress addr = InetAddress.getByName("Admin-PC");
s = new ServerSocket(portNumber , 10, addr);
s.setReuseAddress(true);
System.out.println("InetAddress : " + s.getInetAddress());
while(true){
if(newUser){
newUser = false;
}else{
//get the connection socket
conn = s.accept();
System.out.println("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort());
//create new thread to handle client
new ClientHandler(this,conn);
}
}
}
catch(IOException e){
System.err.println("IOException");
}
//2. close the connections and stream
try{
s.close();
}catch(IOException ioException){
System.err.println("Unable to close. IOexception");
}
}catch(Exception e){
System.out.println(e);
}
}
}
public void test(PrintStream output){
output.println("TEST");
}
public void sendMessage(ClientHandler sender,String clientRCVR,String message) throws IOException{
OutputStream outstream = conn.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
if(clients.size() > 1){
if(clientRCVR.equals("GLOBAL")){
message = "[GLOBAL] " + message;
for (Iterator it = clients.keySet().iterator(); it.hasNext(); ){
Object key = it.next();
ClientHandler client = clients.get(key);
client.sendMessage(message);
}
}else{
try{
System.out.println("Contain PLZ: " + clients.containsKey(clientRCVR));
if(clients.containsKey(clientRCVR)){
message = "[PRIVATE] " + message;
clients.get(clientRCVR).sendMessage(message);
}else{
message = "[MESSAGE COULD NOT BE SENDED]";
sender.sendMessage(message);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
else{
message = "[MESSAGE COULD NOT BE SENDED]";
sender.sendMessage(message);
}
}
public synchronized void addClient(String username, ClientHandler ch){
clients.put(username,ch);
informAllClientsUserlist();
}
public synchronized void removeClient(String username){
clients.remove(username);
informAllClientsUserlist();
}
public void informAllClientsUserlist(){
fillUserList();
for (Iterator it = clients.keySet().iterator(); it.hasNext(); ){
Object key = it.next();
ClientHandler client = clients.get(key);
client.sendUserList(userList);
}
}
private void fillUserList(){
userList.clear();
for (Iterator it = clients.keySet().iterator(); it.hasNext(); ){
Object key = it.next();
ClientHandler client = clients.get(key);
userList.add(client.getUsername());
}
}
public void terminate(){
isActive = false;
}
public synchronized HashMap<String, ClientHandler> getClients() {
return clients;
}
public synchronized void setClients(HashMap<String, ClientHandler> clients) {
this.clients = clients;
}
}