-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatChannel.java
More file actions
111 lines (97 loc) · 2.83 KB
/
ChatChannel.java
File metadata and controls
111 lines (97 loc) · 2.83 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
// CSD 2013, Pablo Galdámez
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
//
// Simple ChatChannel implementation
//
public class ChatChannel
extends UnicastRemoteObject
implements IChatChannel
{
public static final String LEAVE = "LEAVE";
public static final String JOIN = "JOIN";
private String name;
private Hashtable <String, IChatUser> users = new Hashtable<String, IChatUser> ();
public ChatChannel (String name) throws RemoteException {
super (ChatConfiguration.the().getMyPort());
this.name = name;
}
//
// ISA IChatChannel
//
public String getName () throws RemoteException {
return name;
}
//
// ISA IChatChannel
//
public boolean join (IChatUser usr) throws RemoteException
{
String nick = usr.getNick();
String keyNick = nick.trim().toLowerCase();
if (users.get (keyNick) != null) return false; // User already in channel
users.put (keyNick, usr);
notifyUsers (JOIN, nick);
return true;
}
//
// ISA IChatChannel
//
public boolean leave (IChatUser usr) throws RemoteException
{
String nick = usr.getNick();
String keyNick = nick.trim().toLowerCase();
if (users.get (keyNick) == null) return false; // User not found
users.remove (keyNick);
// Channel sends a control message to all users at the channel---> one user left
notifyUsers (LEAVE, nick);
return true;
}
//
// ISA IChatChannel
//
public void sendMessage (IChatMessage msg) throws RemoteException
{
purge ();
for (IChatUser usr: users.values()) {
usr.sendMessage (msg);
}
}
//
// ISA IChatChannel
//
public IChatUser [] listUsers () throws RemoteException {
purge ();
return users.values().toArray(new IChatUser[users.size()]);
}
//
// private function to purge stale users
//
private void purge (){
String [] keys = users.keySet().toArray(new String [users.size()]);
for (int i=0; i<keys.length; i++) {
try {
users.get (keys[i]).getNick(); // Remote invo to check if stale or alive
} catch (Exception e){
users.remove (keys[i]); // Remove stale users
notifyUsers (LEAVE, keys[i]); // this notification sends lowercase nicks (keys)
}
}
}
//
// When users leave or join this channel, we send a message to the remaning users, so that
// they can update their fancy UI's :)
//
private void notifyUsers (String code, String nick) {
IChatMessage msg = null;
try {
msg = new ChatMessage (null, this, code + " " + nick);
} catch (Exception e) {return;}
for (IChatUser usr: users.values()) {
try {
usr.sendMessage (msg);
} catch (Exception e) {} // Ignore errors when sending channel notifications
}
}
}