KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > example > chat > ChatProtocolHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.mina.example.chat;
21
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
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  * {@link IoHandler} implementation of a simple chat server protocol.
34  *
35  * @author The Apache Directory Project (mina-dev@directory.apache.org)
36  * @version $Rev$, $Date$
37  */

38 public class ChatProtocolHandler extends IoHandlerAdapter {
39     private Set JavaDoc<IoSession> sessions = Collections
40             .synchronizedSet(new HashSet JavaDoc<IoSession>());
41
42     private Set JavaDoc<String JavaDoc> users = Collections
43             .synchronizedSet(new HashSet JavaDoc<String JavaDoc>());
44
45     public void exceptionCaught(IoSession session, Throwable JavaDoc cause) {
46         SessionLog.error(session, "", cause);
47         // Close connection when unexpected exception is caught.
48
session.close();
49     }
50
51     public void messageReceived(IoSession session, Object JavaDoc message) {
52         String JavaDoc theMessage = (String JavaDoc) message;
53         String JavaDoc[] result = theMessage.split(" ", 2);
54         String JavaDoc theCommand = result[0];
55
56         try {
57
58             ChatCommand command = ChatCommand.valueOf(theCommand);
59             String JavaDoc user = (String JavaDoc) 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                 // check if the username is already used
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                 // Allow all users
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 JavaDoc e) {
110             SessionLog.debug(session, e.getMessage());
111         }
112     }
113
114     public void broadcast(String JavaDoc message) {
115         synchronized (sessions) {
116             Iterator JavaDoc 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 JavaDoc {
127         String JavaDoc user = (String JavaDoc) 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 JavaDoc name) {
134         return users.contains(name);
135     }
136
137     public int getNumberOfUsers() {
138         return users.size();
139     }
140
141     public void kick(String JavaDoc name) {
142         synchronized (sessions) {
143             Iterator JavaDoc 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 }
154
Popular Tags