View Javadoc

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;
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   * {@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 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          // Close connection when unexpected exception is caught.
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                  // check if the username is already used
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                  // Allow all users
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 }