1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.example.chat;
21
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Set;
26
27 import org.apache.mina.common.IoHandler;
28 import org.apache.mina.common.IoHandlerAdapter;
29 import org.apache.mina.common.IoSession;
30 import org.apache.mina.util.SessionLog;
31
32
33
34
35
36
37
38 public class ChatProtocolHandler extends IoHandlerAdapter {
39 private Set<IoSession> sessions = Collections
40 .synchronizedSet(new HashSet<IoSession>());
41
42 private Set<String> users = Collections
43 .synchronizedSet(new HashSet<String>());
44
45 public void exceptionCaught(IoSession session, Throwable cause) {
46 SessionLog.error(session, "", cause);
47
48 session.close();
49 }
50
51 public void messageReceived(IoSession session, Object message) {
52 String theMessage = (String) message;
53 String[] result = theMessage.split(" ", 2);
54 String theCommand = result[0];
55
56 try {
57
58 ChatCommand command = ChatCommand.valueOf(theCommand);
59 String user = (String) session.getAttribute("user");
60
61 switch (command.toInt()) {
62
63 case ChatCommand.QUIT:
64 session.write("QUIT OK");
65 session.close();
66 break;
67 case ChatCommand.LOGIN:
68
69 if (user != null) {
70 session.write("LOGIN ERROR user " + user
71 + " already logged in.");
72 return;
73 }
74
75 if (result.length == 2) {
76 user = result[1];
77 } else {
78 session.write("LOGIN ERROR invalid login command.");
79 return;
80 }
81
82
83 if (users.contains(user)) {
84 session.write("LOGIN ERROR the name " + user
85 + " is already used.");
86 return;
87 }
88
89 sessions.add(session);
90 session.setAttribute("user", user);
91
92
93 users.add(user);
94 session.write("LOGIN OK");
95 broadcast("The user " + user + " has joined the chat session.");
96 break;
97
98 case ChatCommand.BROADCAST:
99
100 if (result.length == 2) {
101 broadcast(user + ": " + result[1]);
102 }
103 break;
104 default:
105 SessionLog.info(session, "Unhandled command: " + command);
106 break;
107 }
108
109 } catch (IllegalArgumentException e) {
110 SessionLog.debug(session, e.getMessage());
111 }
112 }
113
114 public void broadcast(String message) {
115 synchronized (sessions) {
116 Iterator iter = sessions.iterator();
117 while (iter.hasNext()) {
118 IoSession s = (IoSession) iter.next();
119 if (s.isConnected()) {
120 s.write("BROADCAST OK " + message);
121 }
122 }
123 }
124 }
125
126 public void sessionClosed(IoSession session) throws Exception {
127 String user = (String) session.getAttribute("user");
128 users.remove(user);
129 sessions.remove(session);
130 broadcast("The user " + user + " has left the chat session.");
131 }
132
133 public boolean isChatUser(String name) {
134 return users.contains(name);
135 }
136
137 public int getNumberOfUsers() {
138 return users.size();
139 }
140
141 public void kick(String name) {
142 synchronized (sessions) {
143 Iterator iter = sessions.iterator();
144 while (iter.hasNext()) {
145 IoSession s = (IoSession) iter.next();
146 if (name.equals(s.getAttribute("user"))) {
147 s.close();
148 break;
149 }
150 }
151 }
152 }
153 }