Search in sources :

Example 16 with IoSession

use of org.apache.mina.core.session.IoSession in project vert.x by eclipse.

the class FakeDNSServer method start.

@Override
public void start() throws IOException {
    UdpTransport transport = new UdpTransport(ipAddress, port);
    setTransports(transport);
    acceptor = transport.getAcceptor();
    acceptor.setHandler(new DnsProtocolHandler(this, new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
            RecordStore actual = store;
            if (actual == null) {
                return Collections.emptySet();
            } else {
                return actual.getRecords(question);
            }
        }
    }) {

        @Override
        public void sessionCreated(IoSession session) throws Exception {
            // Use our own codec to support AAAA testing
            session.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory()));
        }

        @Override
        public void messageReceived(IoSession session, Object message) {
            if (message instanceof DnsMessage) {
                synchronized (FakeDNSServer.this) {
                    currentMessage.add((DnsMessage) message);
                }
            }
            super.messageReceived(session, message);
        }
    });
    // Allow the port to be reused even if the socket is in TIME_WAIT state
    ((DatagramSessionConfig) acceptor.getSessionConfig()).setReuseAddress(true);
    // Start the listener
    acceptor.bind();
}
Also used : UdpTransport(org.apache.directory.server.protocol.shared.transport.UdpTransport) QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) DatagramSessionConfig(org.apache.mina.transport.socket.DatagramSessionConfig) DnsMessage(org.apache.directory.server.dns.messages.DnsMessage) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) DnsProtocolHandler(org.apache.directory.server.dns.protocol.DnsProtocolHandler) DnsException(org.apache.directory.server.dns.DnsException) IOException(java.io.IOException) RecordStore(org.apache.directory.server.dns.store.RecordStore) IoSession(org.apache.mina.core.session.IoSession)

Example 17 with IoSession

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

the class ClientTestServer method getIOSession.

/**
 * Get io session io session.
 *
 * @param connector the connector
 * @return the io session
 */
public IoSession getIOSession(IoConnector connector) {
    ConnectFuture future = connector.connect(new InetSocketAddress("192.168.2.55", 1255));
    // 等待是否连接成功,相当于是转异步执行为同步执行。
    future.awaitUninterruptibly();
    // 连接成功后获取会话对象。 如果没有上面的等待, 由于connect()方法是异步的, session可能会无法获取。
    IoSession session = null;
    try {
        session = future.getSession();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return session;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ConnectFuture(org.apache.mina.core.future.ConnectFuture) IoSession(org.apache.mina.core.session.IoSession)

Example 18 with IoSession

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

the class ClientTestServer method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
public static void main(String[] args) {
    for (int i = 0; i < 4000; i++) {
        ClientTestServer client = new ClientTestServer();
        IoConnector connector = client.creatClient();
        IoSession session = client.getIOSession(connector);
        client.sendMsg(session, Arrays.toString(new byte[1000]) + ":" + System.currentTimeMillis());
    }
}
Also used : IoConnector(org.apache.mina.core.service.IoConnector) IoSession(org.apache.mina.core.session.IoSession)

Example 19 with IoSession

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

the class TestDnsServer method start.

/**
 * Start the {@link TestDnsServer} but drop all {@code AAAA} queries and not send any response to these at all.
 */
public void start(final boolean dropAAAAQueries) throws IOException {
    InetSocketAddress address = new InetSocketAddress(NetUtil.LOCALHOST4, 0);
    UdpTransport transport = new UdpTransport(address.getHostName(), address.getPort());
    setTransports(transport);
    DatagramAcceptor acceptor = transport.getAcceptor();
    acceptor.setHandler(new DnsProtocolHandler(this, store) {

        @Override
        public void sessionCreated(IoSession session) {
            // USe our own codec to support AAAA testing
            session.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory(dropAAAAQueries)));
        }
    });
    ((DatagramSessionConfig) acceptor.getSessionConfig()).setReuseAddress(true);
    // Start the listener
    acceptor.bind();
}
Also used : UdpTransport(org.apache.directory.server.protocol.shared.transport.UdpTransport) DatagramAcceptor(org.apache.mina.transport.socket.DatagramAcceptor) InetSocketAddress(java.net.InetSocketAddress) DatagramSessionConfig(org.apache.mina.transport.socket.DatagramSessionConfig) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) DnsProtocolHandler(org.apache.directory.server.dns.protocol.DnsProtocolHandler) IoSession(org.apache.mina.core.session.IoSession)

Example 20 with IoSession

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

the class MINAStatCollector method stop.

/**
 * Stop collecting stats. all the {@link IoSessionStat} object will be removed of the
 * polled session attachements.
 */
public void stop() {
    synchronized (this) {
        service.removeListener(serviceListener);
        // stop worker
        worker.stop = true;
        worker.interrupt();
        while (worker.isAlive()) {
            try {
                worker.join();
            } catch (InterruptedException e) {
            // ignore since this is shutdown time
            }
        }
        for (IoSession session : polledSessions) {
            session.removeAttribute(KEY);
        }
        polledSessions.clear();
    }
}
Also used : IoSession(org.apache.mina.core.session.IoSession)

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