KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > example > chat > client > SwingChatClientHandler


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.common.IoFilter;
23 import org.apache.mina.common.IoHandler;
24 import org.apache.mina.common.IoHandlerAdapter;
25 import org.apache.mina.common.IoSession;
26 import org.apache.mina.example.chat.ChatCommand;
27 import org.apache.mina.filter.LoggingFilter;
28 import org.apache.mina.filter.codec.ProtocolCodecFilter;
29 import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
30
31 /**
32  * {@link IoHandler} implementation of the client side of the simple chat protocol.
33  *
34  * @author The Apache Directory Project (mina-dev@directory.apache.org)
35  * @version $Rev$, $Date$
36  */

37 public class SwingChatClientHandler extends IoHandlerAdapter {
38
39     public interface Callback {
40         void connected();
41
42         void loggedIn();
43
44         void loggedOut();
45
46         void disconnected();
47
48         void messageReceived(String JavaDoc message);
49
50         void error(String JavaDoc message);
51     }
52
53     private static IoFilter LOGGING_FILTER = new LoggingFilter();
54
55     private static IoFilter CODEC_FILTER = new ProtocolCodecFilter(
56             new TextLineCodecFactory());
57
58     private final Callback callback;
59
60     public SwingChatClientHandler(Callback callback) {
61         this.callback = callback;
62     }
63
64     public void sessionCreated(IoSession session) throws Exception JavaDoc {
65         session.getFilterChain().addLast("codec", CODEC_FILTER);
66         session.getFilterChain().addLast("logger", LOGGING_FILTER);
67     }
68
69     public void sessionOpened(IoSession session) throws Exception JavaDoc {
70         callback.connected();
71     }
72
73     public void messageReceived(IoSession session, Object JavaDoc message)
74             throws Exception JavaDoc {
75         String JavaDoc theMessage = (String JavaDoc) message;
76         String JavaDoc[] result = theMessage.split(" ", 3);
77         String JavaDoc status = result[1];
78         String JavaDoc theCommand = result[0];
79         ChatCommand command = ChatCommand.valueOf(theCommand);
80
81         if ("OK".equals(status)) {
82
83             switch (command.toInt()) {
84
85             case ChatCommand.BROADCAST:
86                 if (result.length == 3) {
87                     callback.messageReceived(result[2]);
88                 }
89                 break;
90             case ChatCommand.LOGIN:
91                 callback.loggedIn();
92                 break;
93
94             case ChatCommand.QUIT:
95                 callback.loggedOut();
96                 break;
97             }
98
99         } else {
100             if (result.length == 3) {
101                 callback.error(result[2]);
102             }
103         }
104     }
105
106     public void sessionClosed(IoSession session) throws Exception JavaDoc {
107         callback.disconnected();
108     }
109
110 }
111
Popular Tags