-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCommunicationHandler.java
111 lines (93 loc) · 2.54 KB
/
CommunicationHandler.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
package Client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class CommunicationHandler extends Thread {
private Boolean isActive = true;
private ArrayList<String> userList = new ArrayList<String>();
private ArrayList<String> message = new ArrayList<String>();
private Socket s = new Socket();
private PrintWriter s_out = null;
private BufferedReader s_in = null;
private String response = "";
private Client client;
private Boolean isConnected = false;
public CommunicationHandler(){
super.start();
}
public void run(){
try {
client = new Client(this);
while(isActive){
if(getIsConnected()){
while((response = s_in.readLine()) != null){
if(response.equals("/GAU")){
String uList;
while((uList = s_in.readLine()) != null){
System.out.println("TEST Userlist");
userList = setUserList(uList);
System.out.println("Response:" + userList);
client.updateUserListChatGUI(userList);
if(uList.contains("[END]")){
break;
}
}
}
else if(response.equals("/M")){
String messageResponse;
while((messageResponse = s_in.readLine()) != null){
client.updateMessageChatGUI(messageResponse);
break;
}
System.out.println("Message Test");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<String> setUserList(String rspUserList){
ArrayList<String> parts = new ArrayList<String>(Arrays.asList(rspUserList.split(":")));
parts.remove("[END]"); // Arraylist
return parts;
}
public synchronized Boolean getIsConnected() {
return isConnected;
}
public synchronized void setIsConnected(Boolean isConnected) {
this.isConnected = isConnected;
}
public synchronized Socket getS() {
return s;
}
public synchronized void setS(Socket s) {
this.s = s;
}
public synchronized PrintWriter getS_out() {
return s_out;
}
public synchronized void setS_out(PrintWriter s_out) {
this.s_out = s_out;
}
public synchronized BufferedReader getS_in() {
return s_in;
}
public synchronized void setS_in(BufferedReader s_in) {
this.s_in = s_in;
}
public synchronized Client getClient() {
return client;
}
public synchronized void setClient(Client client) {
this.client = client;
}
}