`
eyesmore
  • 浏览: 376816 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

基于MINA的客户端(Template-Callback模式)

    博客分类:
  • MINA
阅读更多
/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.mina.example.imagine.step1.client;

import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.RuntimeIoException;
import org.apache.mina.example.imagine.step1.ImageRequest;
import org.apache.mina.example.imagine.step1.ImageResponse;
import org.apache.mina.example.imagine.step1.server.ImageServer;
import org.apache.mina.example.imagine.step1.codec.ImageCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import java.net.InetSocketAddress;

/**
 * client for the {@link ImageServer}
 *
 * @author The Apache MINA Project (dev@mina.apache.org)
 * @version $Rev: 597940 $, $Date: 2007-11-25 02:00:09 +0100 (Sun, 25 Nov 2007) $
 */
public class ImageClient extends IoHandlerAdapter {
    public static final int CONNECT_TIMEOUT = 3000;

    private String host;
    private int port;
    private SocketConnector connector;
    private IoSession session;
    private ImageListener imageListener;

    public ImageClient(String host, int port, ImageListener imageListener) {
        this.host = host;
        this.port = port;
        this.imageListener = imageListener;
        connector = new NioSocketConnector();
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ImageCodecFactory(true)));
        connector.setHandler(this);
    }

    public boolean isConnected() {
        return (session != null && session.isConnected());
    }

    public void connect() {
        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host, port));
        connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT);
        try {
            session = connectFuture.getSession();
        }
        catch (RuntimeIoException e) {
            imageListener.onException(e);
        }
    }

    public void disconnect() {
        if (session != null) {
            session.close().awaitUninterruptibly(CONNECT_TIMEOUT);
            session = null;
        }
    }

    public void sessionOpened(IoSession session) throws Exception {
        imageListener.sessionOpened();
    }

    public void sessionClosed(IoSession session) throws Exception {
        imageListener.sessionClosed();
    }

    public void sendRequest(ImageRequest imageRequest) {
        if (session == null) {
            //noinspection ThrowableInstanceNeverThrown
            imageListener.onException(new Throwable("not connected"));
        } else {
            session.write(imageRequest);
        }
    }

    public void messageReceived(IoSession session, Object message) throws Exception {
        ImageResponse response = (ImageResponse) message;
        imageListener.onImages(response.getImage1(), response.getImage2());
    }

    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        imageListener.onException(cause);
    }

}
 

 

分享到:
评论

相关推荐

    mina2.0 含11个jar包

    mina-core-2.0.0-M6.jar mina-example-2.0.0-M6.jar mina-filter-codec-netty-2.0.0-M6.jar mina-filter-compression-2.0.0-M6.jar mina-integration-beans-2.0.0-M6.jar mina-integration-jmx-2.0.0-M6.jar mina-...

    mina-filter-compression-2.0.7.jar

    mina-filter-compression-2.0.7.jar,mina 过滤器jar包,核心包之一

    mina-core-2.0.0-M1.jar/mina-example-1.0.5.jar

    mina-core-2.0.0-M1.jar/mina-example-1.0.5.jar/slf4j-jdk14-1.6.1.jar/slf4j-log4j12-1.6.1.jar mina 所用jar

    mina-core-2.0.0-RC1.jar,mina-filter-compression-2.0.0-RC1.jar

    在这个场景中,我们关注的是MINA的核心组件以及两个特定的过滤器和传输组件:`mina-core-2.0.0-RC1.jar`、`mina-filter-compression-2.0.0-RC1.jar`和`mina-transport-apr-2.0.0-RC1.jar`。 **MINA Core (mina-core...

    mina-core-2.0.0-M1.jar和slf4j

    mina-core-2.0.0-M1.jar是Apache Mina项目的核心库,它是一个高度可扩展的网络通信框架,主要用于构建高性能、高效率的服务端和客户端应用程序。Mina提供了一种统一的API,可以处理多种传输协议,如TCP/IP、UDP/IP...

    mina-integration-beans-2.0.0-M4.jar

    mina-integration-beans-2.0.0-M4.jar mina-integration-beans-2.0.0-M4.jar

    mina-integration-ognl-2.0.0-M4.jar

    mina-integration-ognl-2.0.0-M4.jar mina-integration-ognl-2.0.0-M4.jar

    mina客户端简单代码示例

    在本示例中,我们关注的是"Mina客户端",即如何使用Mina库创建一个简单的客户端应用。 标题中的"mina客户端简单代码示例"意味着我们将探讨如何使用Mina框架编写一个基础的客户端程序,该程序能够连接到服务器并发送...

    mina-core-2.0及其他相关jar

    mina-core-2.0及其他相关jar,mina-core-2.0及其他相关jarmina-core-2.0及其他相关jarmina-core-2.0及其他相关jarmina-core-2.0及其他相关jar

    mina-transport-apr-2.0.2.jar

    mina-transport-apr-2.0.2.jar

    MINA-2.0.0-M4 英文版

    MINA-2.0.0-M4是该框架的一个中期版本,它提供了许多改进和新特性。"M4"代表这是第四个里程碑版本,通常在正式版本发布之前,会经过一系列的里程碑版本进行测试和优化。这个版本可能包含了bug修复、性能提升、新功能...

    mina-core-2.0.7.jar

    mina-core-2.0.7.jar,MINA核心jar包,用于基于mina的开发

    mina-core-2.0.0-RC1-sources.jar

    mina-core-2.0.0-RC1-sources.jar

    mina-core-2.0.0-M1-sources.jar

    mina-core-2.0.0-M1-sources.jar是构建mina框架的主要文件

    mina-http-2.0.7.jar.zip_mina_mina 获取POST_mina-http_mina-http-2.0

    《mina-http-2.0.7.jar:Java中的MINA框架与HTTP支持详解》 MINA(Multipurpose Infrastructure for Network Applications)是一个强大的网络应用程序框架,它由Apache软件基金会开发,主要用于构建高性能、高效率...

    mina2.0全部jar包

    mina-integration-beans-2.0.7.jar和mina-integration-xbean-2.0.7.jar则是MINA与JavaBeans和XBean集成的相关库,它们允许开发者利用Spring框架或者Apache XBean来管理MINA的配置,实现更加灵活的依赖注入和配置管理...

    apache-mina-2.0.7-bin.tar

    标题中的"apache-mina-2.0.7-bin.tar"指的是Apache Mina 2.0.7版本的二进制发布包,该包包含了编译好的库文件和运行时所需的资源,用户可以直接使用而无需自行编译源代码。 描述中提及的"apache-mina-2.0.7-src.tar...

    apache-mina-2.0.16-jar包-代码.zip

    标题中的"apache-mina-2.0.16-jar包-代码.zip"表明这是一个包含Apache Mina 2.0.16版本源代码的JAR文件压缩包。这个压缩包通常用于开发者进行源码级别的学习、调试或定制工作。通过解压这个文件,你可以获得Mina库的...

    MINA-2.0.0-M3

    标题中的"MINA-2.0.0-M3"指的是MINA框架的第2.0.0-M3版本。这个版本是一个里程碑版本(M3,Milestones),通常在正式版本发布之前,用于收集用户反馈和进行测试。 MINA的核心设计目标是提供一个灵活、可扩展的网络...

    Apache-Mina-2.rar_apache mina_java mina_mina_mina-core-1.1.3

    标题中的"Apache-Mina-2.rar"和"mina-mina-core-1.1.3"表明这是关于Apache Mina 2.x版本,特别是其核心模块的1.1.3版本。 Apache Mina的名称来源于"Multi-purpose Infrastructure for Network Applications",它...

Global site tag (gtag.js) - Google Analytics