001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.chat.client;
021:
022: import org.apache.mina.common.IoFilter;
023: import org.apache.mina.common.IoHandler;
024: import org.apache.mina.common.IoHandlerAdapter;
025: import org.apache.mina.common.IoSession;
026: import org.apache.mina.example.chat.ChatCommand;
027: import org.apache.mina.filter.codec.ProtocolCodecFilter;
028: import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
029: import org.apache.mina.filter.logging.LoggingFilter;
030:
031: /**
032: * {@link IoHandler} implementation of the client side of the simple chat protocol.
033: *
034: * @author The Apache MINA Project (dev@mina.apache.org)
035: * @version $Rev$, $Date$
036: */
037: public class SwingChatClientHandler extends IoHandlerAdapter {
038:
039: public interface Callback {
040: void connected();
041:
042: void loggedIn();
043:
044: void loggedOut();
045:
046: void disconnected();
047:
048: void messageReceived(String message);
049:
050: void error(String message);
051: }
052:
053: private static final IoFilter LOGGING_FILTER = new LoggingFilter();
054:
055: private static final IoFilter CODEC_FILTER = new ProtocolCodecFilter(
056: new TextLineCodecFactory());
057:
058: private final Callback callback;
059:
060: public SwingChatClientHandler(Callback callback) {
061: this .callback = callback;
062: }
063:
064: @Override
065: public void sessionCreated(IoSession session) throws Exception {
066: session.getFilterChain().addLast("codec", CODEC_FILTER);
067: session.getFilterChain().addLast("logger", LOGGING_FILTER);
068: }
069:
070: @Override
071: public void sessionOpened(IoSession session) throws Exception {
072: callback.connected();
073: }
074:
075: @Override
076: public void messageReceived(IoSession session, Object message)
077: throws Exception {
078: String theMessage = (String) message;
079: String[] result = theMessage.split(" ", 3);
080: String status = result[1];
081: String theCommand = result[0];
082: ChatCommand command = ChatCommand.valueOf(theCommand);
083:
084: if ("OK".equals(status)) {
085:
086: switch (command.toInt()) {
087:
088: case ChatCommand.BROADCAST:
089: if (result.length == 3) {
090: callback.messageReceived(result[2]);
091: }
092: break;
093: case ChatCommand.LOGIN:
094: callback.loggedIn();
095: break;
096:
097: case ChatCommand.QUIT:
098: callback.loggedOut();
099: break;
100: }
101:
102: } else {
103: if (result.length == 3) {
104: callback.error(result[2]);
105: }
106: }
107: }
108:
109: @Override
110: public void sessionClosed(IoSession session) throws Exception {
111: callback.disconnected();
112: }
113:
114: }
|