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   */
32  public class SwingChatClientHandler extends IoHandlerAdapter {
33  
34      public interface Callback {
35          void connected();
36  
37          void loggedIn();
38  
39          void loggedOut();
40  
41          void disconnected();
42  
43          void messageReceived(String message);
44  
45          void error(String message);
46      }
47  
48      private final Callback callback;
49  
50      public SwingChatClientHandler(Callback callback) {
51          this.callback = callback;
52      }
53  
54      @Override
55      public void sessionOpened(IoSession session) throws Exception {
56          callback.connected();
57      }
58  
59      @Override
60      public void messageReceived(IoSession session, Object message)
61              throws Exception {
62          String theMessage = (String) message;
63          String[] result = theMessage.split(" ", 3);
64          String status = result[1];
65          String theCommand = result[0];
66          ChatCommand command = ChatCommand.valueOf(theCommand);
67  
68          if ("OK".equals(status)) {
69  
70              switch (command.toInt()) {
71  
72              case ChatCommand.BROADCAST:
73                  if (result.length == 3) {
74                      callback.messageReceived(result[2]);
75                  }
76                  break;
77              case ChatCommand.LOGIN:
78                  callback.loggedIn();
79                  break;
80  
81              case ChatCommand.QUIT:
82                  callback.loggedOut();
83                  break;
84              }
85  
86          } else {
87              if (result.length == 3) {
88                  callback.error(result[2]);
89              }
90          }
91      }
92  
93      @Override
94      public void sessionClosed(IoSession session) throws Exception {
95          callback.disconnected();
96      }
97  
98  }