Search in sources :

Example 1 with IoSession

use of org.apache.mina.core.session.IoSession in project camel by apache.

the class Mina2UdpProtocolCodecFactory method getEncoder.

public ProtocolEncoder getEncoder(IoSession session) throws Exception {
    return new ProtocolEncoder() {

        public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
            IoBuffer buf = toIoBuffer(message);
            buf.flip();
            out.write(buf);
        }

        public void dispose(IoSession session) throws Exception {
        // do nothing
        }
    };
}
Also used : ProtocolEncoder(org.apache.mina.filter.codec.ProtocolEncoder) IoSession(org.apache.mina.core.session.IoSession) ProtocolEncoderOutput(org.apache.mina.filter.codec.ProtocolEncoderOutput) IoBuffer(org.apache.mina.core.buffer.IoBuffer)

Example 2 with IoSession

use of org.apache.mina.core.session.IoSession in project pancm_project by xuwujing.

the class MinaClient method main.

/* 
    * 测试服务端与客户端程序!
    a. 启动服务端,然后再启动客户端(客户端发送的消息是"why are you so diao ")
    b. 服务端接收消息并处理成功;
    */
public static void main(String[] args) {
    // 创建一个非阻塞的客户端程序
    IoConnector connector = new NioSocketConnector();
    // 设置链接超时时间
    connector.setConnectTimeout(30000);
    ProtocolCodecFilter pf = new ProtocolCodecFilter((new MyTextLineCodecFactory(Charset.forName("utf-8"), "\r\n")));
    // 添加过滤器
    connector.getFilterChain().addLast("codec", pf);
    // 添加业务逻辑处理器类
    connector.setHandler(new MinaClientHandler());
    IoSession session = null;
    try {
        ConnectFuture future = connector.connect(new InetSocketAddress(HOST, // 创建连接
        PORT));
        // 等待连接创建完成
        future.awaitUninterruptibly();
        // 获得session
        session = future.getSession();
        String msg = "hello \r\n";
        // 发送消息
        session.write(msg);
        logger.info("客户端与服务端建立连接成功...发送的消息为:" + msg);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("客户端链接异常...", e);
    }
    // 等待连接断开
    session.getCloseFuture().awaitUninterruptibly();
    connector.dispose();
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) InetSocketAddress(java.net.InetSocketAddress) ConnectFuture(org.apache.mina.core.future.ConnectFuture) IoConnector(org.apache.mina.core.service.IoConnector) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) IoSession(org.apache.mina.core.session.IoSession)

Example 3 with IoSession

use of org.apache.mina.core.session.IoSession in project streamsx.topology by IBMStreams.

the class TupleCollection method addTCPServerAndSink.

/**
     * Add a TCP server that will list for tuples to be directed to handlers.
     * Adds a sink to the topology to capture those tuples and deliver them to
     * the current jvm to run Junit type tests.
     */
private void addTCPServerAndSink() throws Exception {
    tcpServer = new TCPTestServer(0, new IoHandlerAdapter() {

        @Override
        public void messageReceived(IoSession session, Object message) throws Exception {
            TestTuple tuple = (TestTuple) message;
            TestTupleInjector injector = injectors.get(tuple.getTesterId());
            injector.tuple(tuple.getTupleData());
        }
    });
    InetSocketAddress testAddr = tcpServer.start();
    addTesterSink(testAddr);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IoHandlerAdapter(org.apache.mina.core.service.IoHandlerAdapter) IoSession(org.apache.mina.core.session.IoSession)

Example 4 with IoSession

use of org.apache.mina.core.session.IoSession in project opennms by OpenNMS.

the class AsyncBasicDetectorMinaImpl method isServiceDetected.

/**
 * {@inheritDoc}
 */
@Override
public final DetectFuture isServiceDetected(final InetAddress address) {
    final DetectFutureMinaImpl detectFuture = new DetectFutureMinaImpl(this);
    try {
        // Set this up here because it can throw an Exception, which we want
        // to throw now, not in initializeSession
        final SSLContext c = createClientSSLContext();
        // Create an IoSessionInitializer that will configure this individual
        // session. Previously, all this was done on a new Connector each time
        // but that was leaking file handles all over the place. This way gives
        // us per-connection settings without the overhead of creating new
        // Connectors each time
        IoSessionInitializer<ConnectFuture> init = new IoSessionInitializer<ConnectFuture>() {

            @Override
            public void initializeSession(IoSession session, ConnectFuture future) {
                // Add filters to the session
                if (isUseSSLFilter()) {
                    final SslFilter filter = new SslFilter(c);
                    filter.setUseClientMode(true);
                    session.getFilterChain().addFirst("SSL", filter);
                }
                session.getFilterChain().addLast("logger", getLoggingFilter() != null ? getLoggingFilter() : new SlightlyMoreVerboseLoggingFilter());
                session.getFilterChain().addLast("codec", getProtocolCodecFilter());
                // Make the minimum idle timeout 1 second
                int idleTimeInSeconds = Math.max(1, Math.round(getIdleTime() / 1000.0f));
                // Set all of the idle time limits. Make sure to specify values in
                // seconds!!!
                session.getConfig().setReaderIdleTime(idleTimeInSeconds);
                session.getConfig().setWriterIdleTime(idleTimeInSeconds);
                session.getConfig().setBothIdleTime(idleTimeInSeconds);
            }
        };
        // Start communication
        final InetSocketAddress socketAddress = new InetSocketAddress(address, getPort());
        final ConnectFuture cf = m_connectionFactory.connect(socketAddress, init, createDetectorHandler(detectFuture));
        cf.addListener(retryAttemptListener(detectFuture, socketAddress, init, getRetries()));
    } catch (KeyManagementException e) {
        detectFuture.setException(e);
    } catch (NoSuchAlgorithmException e) {
        detectFuture.setException(e);
    } catch (Throwable e) {
        detectFuture.setException(e);
    }
    return detectFuture;
}
Also used : SslFilter(org.apache.mina.filter.ssl.SslFilter) InetSocketAddress(java.net.InetSocketAddress) SSLContext(javax.net.ssl.SSLContext) ConnectFuture(org.apache.mina.core.future.ConnectFuture) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) IoSessionInitializer(org.apache.mina.core.session.IoSessionInitializer) IoSession(org.apache.mina.core.session.IoSession)

Example 5 with IoSession

use of org.apache.mina.core.session.IoSession in project directory-ldap-api by apache.

the class AbstractCodecServiceTest method setupLdapCodecService.

/**
 * Initialize the codec service
 */
@BeforeClass
public static void setupLdapCodecService() {
    codec = new DefaultLdapCodecService();
    codec.registerProtocolCodecFactory(new ProtocolCodecFactory() {

        public ProtocolEncoder getEncoder(IoSession session) throws Exception {
            return null;
        }

        public ProtocolDecoder getDecoder(IoSession session) throws Exception {
            return null;
        }
    });
    if (LdapApiServiceFactory.isInitialized() == false) {
        LdapApiServiceFactory.initialize(codec);
    }
    encoder = new LdapEncoder(codec);
}
Also used : LdapEncoder(org.apache.directory.api.ldap.codec.api.LdapEncoder) DefaultLdapCodecService(org.apache.directory.api.ldap.codec.osgi.DefaultLdapCodecService) ProtocolCodecFactory(org.apache.mina.filter.codec.ProtocolCodecFactory) ProtocolDecoder(org.apache.mina.filter.codec.ProtocolDecoder) ProtocolEncoder(org.apache.mina.filter.codec.ProtocolEncoder) IoSession(org.apache.mina.core.session.IoSession) BeforeClass(org.junit.BeforeClass)

Aggregations

IoSession (org.apache.mina.core.session.IoSession)21 InetSocketAddress (java.net.InetSocketAddress)7 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)4 IOException (java.io.IOException)3 DnsProtocolHandler (org.apache.directory.server.dns.protocol.DnsProtocolHandler)3 UdpTransport (org.apache.directory.server.protocol.shared.transport.UdpTransport)3 ConnectFuture (org.apache.mina.core.future.ConnectFuture)3 DatagramSessionConfig (org.apache.mina.transport.socket.DatagramSessionConfig)3 ArrayList (java.util.ArrayList)2 DnsException (org.apache.directory.server.dns.DnsException)2 IoConnector (org.apache.mina.core.service.IoConnector)2 IoHandlerAdapter (org.apache.mina.core.service.IoHandlerAdapter)2 ProtocolEncoder (org.apache.mina.filter.codec.ProtocolEncoder)2 DatagramAcceptor (org.apache.mina.transport.socket.DatagramAcceptor)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 SocketAddress (java.net.SocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1