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 sessions = Collections.synchronizedSet(new HashSet());
40
41 private Set users = Collections.synchronizedSet(new HashSet());
42
43 public void exceptionCaught(IoSession session, Throwable cause) {
44 SessionLog.error(session, "", cause);
45
46 session.close();
47 }
48
49 public void messageReceived(IoSession session, Object message) {
50 String theMessage = (String) message;
51 String[] result = theMessage.split(" ", 2);
52 String theCommand = result[0];
53
54 try {
55
56 ChatCommand command = ChatCommand.valueOf(theCommand);
57 String user = (String) session.getAttribute("user");
58
59 switch (command.toInt()) {
60
61 case ChatCommand.QUIT:
62 session.write("QUIT OK");
63 session.close();
64 break;
65 case ChatCommand.LOGIN:
66
67 if (user != null) {
68 session.write("LOGIN ERROR user " + user
69 + " already logged in.");
70 return;
71 }
72
73 if (result.length == 2) {
74 user = result[1];
75 } else {
76 session.write("LOGIN ERROR invalid login command.");
77 return;
78 }
79
80
81 if (users.contains(user)) {
82 session.write("LOGIN ERROR the name " + user
83 + " is already used.");
84 return;
85 }
86
87 sessions.add(session);
88 session.setAttribute("user", user);
89
90
91 users.add(user);
92 session.write("LOGIN OK");
93 broadcast("The user " + user + " has joined the chat session.");
94 break;
95
96 case ChatCommand.BROADCAST:
97
98 if (result.length == 2) {
99 broadcast(user + ": " + result[1]);
100 }
101 break;
102 default:
103 SessionLog.info(session, "Unhandled command: " + command);
104 break;
105 }
106
107 } catch (IllegalArgumentException e) {
108 SessionLog.debug(session, e.getMessage());
109 }
110 }
111
112 public void broadcast(String message) {
113 synchronized (sessions) {
114 Iterator iter = sessions.iterator();
115 while (iter.hasNext()) {
116 IoSession s = (IoSession) iter.next();
117 if (s.isConnected()) {
118 s.write("BROADCAST OK " + message);
119 }
120 }
121 }
122 }
123
124 public void sessionClosed(IoSession session) throws Exception {
125 String user = (String) session.getAttribute("user");
126 users.remove(user);
127 sessions.remove(session);
128 broadcast("The user " + user + " has left the chat session.");
129 }
130
131 public boolean isChatUser(String name) {
132 return users.contains(name);
133 }
134
135 public int getNumberOfUsers() {
136 return users.size();
137 }
138
139 public void kick(String name) {
140 synchronized (sessions) {
141 Iterator iter = sessions.iterator();
142 while (iter.hasNext()) {
143 IoSession s = (IoSession) iter.next();
144 if (name.equals(s.getAttribute("user"))) {
145 s.close();
146 break;
147 }
148 }
149 }
150 }
151 }