|
@@ -37,116 +37,118 @@ class ReceiveThread extends Thread {
|
37
|
37
|
}
|
38
|
38
|
public void run()
|
39
|
39
|
{
|
40
|
|
- ObjectInputStream in;
|
41
|
40
|
boolean exit = false;
|
42
|
|
- while(!exit)
|
43
|
|
- {
|
44
|
|
- try
|
|
41
|
+ try
|
|
42
|
+ {
|
|
43
|
+ while(!exit)
|
45
|
44
|
{
|
46
|
|
- in = new ObjectInputStream(socket.getInputStream());
|
47
|
|
- Notification notif = (Notification) in.readObject();
|
48
|
|
-
|
49
|
|
- if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor))
|
50
|
|
- {
|
51
|
|
- for(Socket s:outdoor_dest_sockets)
|
52
|
|
- {
|
53
|
|
- try
|
|
45
|
+ ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
|
46
|
+ Notification notif = (Notification) in.readObject();
|
|
47
|
+
|
|
48
|
+ if(!(notif.getAuthor().equals(user)) && !notif.isRedirected())
|
|
49
|
+ {
|
|
50
|
+ for(Socket s:outdoor_dest_sockets)
|
|
51
|
+ {
|
|
52
|
+ try
|
|
53
|
+ {
|
|
54
|
+ ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
|
55
|
+ notif.setRedirected(true);
|
|
56
|
+ out.writeObject(notif);
|
|
57
|
+ notif.setRedirected(false);
|
|
58
|
+ }
|
|
59
|
+ catch(IOException e){}
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ if(isOutdoor)
|
|
64
|
+ {
|
|
65
|
+ for(Socket s:dest_sockets)
|
54
|
66
|
{
|
55
|
|
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
56
|
|
-
|
57
|
|
- out.writeObject(notif);
|
58
|
|
-
|
|
67
|
+ try
|
|
68
|
+ {
|
|
69
|
+ ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
|
70
|
+ notif.setRedirected(true);
|
|
71
|
+ out.writeObject(notif);
|
|
72
|
+ }
|
|
73
|
+ catch(IOException e){}
|
59
|
74
|
}
|
60
|
|
- catch(IOException e){}
|
61
|
|
- }
|
62
|
|
- }
|
63
|
|
-
|
64
|
|
- if(isOutdoor)
|
65
|
|
- {
|
66
|
|
- for(Socket s:dest_sockets)
|
67
|
|
- {
|
68
|
|
- try
|
69
|
|
- {
|
70
|
|
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
71
|
|
-
|
72
|
|
- out.writeObject(notif);
|
73
|
|
- }
|
74
|
|
- catch(IOException e){}
|
75
|
|
- }
|
76
|
|
-
|
77
|
|
- }
|
78
|
|
- else
|
79
|
|
- {
|
80
|
|
- if(notif instanceof Message)
|
81
|
|
- {
|
82
|
|
- Message m = (Message) notif;
|
83
|
|
- displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n");
|
84
|
|
- }
|
85
|
|
- else if (notif instanceof DisconnectNotification)
|
86
|
|
- {
|
87
|
|
- DisconnectNotification dn = (DisconnectNotification) notif;
|
88
|
|
-
|
89
|
|
- displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
|
90
|
|
-
|
91
|
|
- known_users.remove(dn.getAuthor());
|
92
|
|
- known_outdoor_users.remove(dn.getAuthor());
|
93
|
|
-
|
94
|
|
- if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor))
|
95
|
|
- {
|
96
|
|
- exit = true;
|
97
|
|
-
|
98
|
|
- socket.close();
|
99
|
|
- if(!isOutdoor)
|
100
|
|
- dest_sockets.remove(socket);
|
101
|
|
- else
|
102
|
|
- outdoor_dest_sockets.remove(socket);
|
103
|
|
- }
|
104
|
|
- }
|
105
|
|
-
|
106
|
|
- else if (notif instanceof ConnectNotification)
|
107
|
|
- {
|
108
|
|
- ConnectNotification cn = (ConnectNotification) notif;
|
109
|
|
-
|
110
|
|
- displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
|
111
|
|
- if(cn.getAuthor().isOutdoor())
|
|
75
|
+ }
|
|
76
|
+ else
|
|
77
|
+ {
|
|
78
|
+ if(notif instanceof Message)
|
|
79
|
+ {
|
|
80
|
+ Message m = (Message) notif;
|
|
81
|
+ displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n");
|
|
82
|
+ }
|
|
83
|
+ else if (notif instanceof DisconnectNotification)
|
|
84
|
+ {
|
|
85
|
+ DisconnectNotification dn = (DisconnectNotification) notif;
|
|
86
|
+
|
|
87
|
+ displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
|
|
88
|
+
|
|
89
|
+ known_users.remove(dn.getAuthor());
|
|
90
|
+ known_outdoor_users.remove(dn.getAuthor());
|
|
91
|
+
|
|
92
|
+ if(!dn.isRedirected() && !dn.getAuthor().equals(user))
|
|
93
|
+ {
|
|
94
|
+ exit = true;
|
|
95
|
+
|
|
96
|
+ socket.close();
|
|
97
|
+ if(!isOutdoor)
|
|
98
|
+ dest_sockets.remove(socket);
|
|
99
|
+ else
|
|
100
|
+ outdoor_dest_sockets.remove(socket);
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ else if (notif instanceof ConnectNotification)
|
|
105
|
+ {
|
|
106
|
+ ConnectNotification cn = (ConnectNotification) notif;
|
|
107
|
+
|
|
108
|
+ displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
|
|
109
|
+ if(cn.isOutdoor())
|
|
110
|
+ {
|
|
111
|
+ if(!known_outdoor_users.contains(cn.getAuthor()))
|
|
112
|
+ {
|
|
113
|
+ known_outdoor_users.add(cn.getAuthor());
|
|
114
|
+ Collections.sort(known_outdoor_users);
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+ else
|
|
118
|
+ {
|
|
119
|
+ if(!known_users.contains(cn.getAuthor()))
|
|
120
|
+ {
|
|
121
|
+ known_users.add(cn.getAuthor());
|
|
122
|
+ Collections.sort(known_users);
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+ }
|
|
126
|
+ knownUsersPanel.setText("");
|
|
127
|
+ knownUsersPanel.append("Indoor users:\n");
|
|
128
|
+ for(User a:known_users)
|
112
|
129
|
{
|
113
|
|
- if(!known_outdoor_users.contains(cn.getAuthor()))
|
114
|
|
- {
|
115
|
|
- known_outdoor_users.add(cn.getAuthor());
|
116
|
|
- Collections.sort(known_outdoor_users);
|
117
|
|
- }
|
|
130
|
+ knownUsersPanel.append(" " + a.getName() + " \n");
|
118
|
131
|
}
|
119
|
|
- else
|
|
132
|
+ knownUsersPanel.append("\nOutdoor users:\n");
|
|
133
|
+ for(User a:known_outdoor_users)
|
120
|
134
|
{
|
121
|
|
- if(!known_users.contains(cn.getAuthor()))
|
122
|
|
- {
|
123
|
|
- known_users.add(cn.getAuthor());
|
124
|
|
- Collections.sort(known_users);
|
125
|
|
- }
|
|
135
|
+ knownUsersPanel.append(" " + a.getName() + " \n");
|
126
|
136
|
}
|
127
|
|
- }
|
128
|
|
- knownUsersPanel.setText("");
|
129
|
|
- knownUsersPanel.append("Indoor users:\n");
|
130
|
|
- for(User a:known_users)
|
131
|
|
- {
|
132
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
133
|
|
- }
|
134
|
|
- knownUsersPanel.append("\nOutdoor users:\n");
|
135
|
|
- for(User a:known_outdoor_users)
|
136
|
|
- {
|
137
|
|
- knownUsersPanel.append(" " + a.getName() + " \n");
|
138
|
|
- }
|
139
|
|
- displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
140
|
|
- }
|
141
|
|
- }
|
142
|
|
- catch(Exception e)
|
143
|
|
- {
|
144
|
|
- in.reset();
|
145
|
|
- e.printStackTrace();
|
146
|
|
- /*System.out.println("Socket closed");
|
147
|
|
- dest_sockets.remove(socket);
|
148
|
|
- outdoor_dest_sockets.remove(socket);*/
|
149
|
|
- }
|
|
137
|
+ displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
|
138
|
+ }
|
|
139
|
+ }
|
|
140
|
+ }
|
|
141
|
+ catch(EOFException e) {}
|
|
142
|
+ catch(SocketException e)
|
|
143
|
+ {
|
|
144
|
+ if(!isOutdoor)
|
|
145
|
+ dest_sockets.remove(socket);
|
|
146
|
+ else
|
|
147
|
+ outdoor_dest_sockets.remove(socket);
|
150
|
148
|
}
|
|
149
|
+ catch(Exception e)
|
|
150
|
+ {
|
|
151
|
+ e.printStackTrace();
|
|
152
|
+ }
|
151
|
153
|
}
|
152
|
154
|
}
|