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.client;
21  
22  import org.apache.mina.core.service.IoHandler;
23  import org.apache.mina.core.service.IoHandlerAdapter;
24  import org.apache.mina.core.session.IoSession;
25  import org.apache.mina.example.chat.ChatCommand;
26  
27  /**
28   * {@link IoHandler} implementation of the client side of the simple chat protocol.
29   *
30   * @author The Apache MINA Project (dev@mina.apache.org)
31   * @version $Rev$, $Date$
32   */
33  public class SwingChatClientHandler extends IoHandlerAdapter {
34  
35      public interface Callback {
36          void connected();
37  
38          void loggedIn();
39  
40          void loggedOut();
41  
42          void disconnected();
43  
44          void messageReceived(String message);
45  
46          void error(String message);
47      }
48  
49      private final Callback callback;
50  
51      public SwingChatClientHandler(Callback callback) {
52          this.callback = callback;
53      }
54  
55      @Override
56      public void sessionOpened(IoSession session) throws Exception {
57          callback.connected();
58      }
59  
60      @Override
61      public void messageReceived(IoSession session, Object message)
62              throws Exception {
63          String theMessage = (String) message;
64          String[] result = theMessage.split(" ", 3);
65          String status = result[1];
66          String theCommand = result[0];
67          ChatCommand command = ChatCommand.valueOf(theCommand);
68  
69          if ("OK".equals(status)) {
70  
71              switch (command.toInt()) {
72  
73              case ChatCommand.BROADCAST:
74                  if (result.length == 3) {
75                      callback.messageReceived(result[2]);
76                  }
77                  break;
78              case ChatCommand.LOGIN:
79                  callback.loggedIn();
80                  break;
81  
82              case ChatCommand.QUIT:
83                  callback.loggedOut();
84                  break;
85              }
86  
87          } else {
88              if (result.length == 3) {
89                  callback.error(result[2]);
90              }
91          }
92      }
93  
94      @Override
95      public void sessionClosed(IoSession session) throws Exception {
96          callback.disconnected();
97      }
98  
99  }