Example usage for org.apache.mina.api IdleStatus READ_IDLE

List of usage examples for org.apache.mina.api IdleStatus READ_IDLE

Introduction

In this page you can find the example usage for org.apache.mina.api IdleStatus READ_IDLE.

Prototype

IdleStatus READ_IDLE

To view the source code for org.apache.mina.api IdleStatus READ_IDLE.

Click Source Link

Usage

From source file:org.eclipse.mihini.coap.assetfactory.CoapServer.java

License:Apache License

public void start() {

    final Map<String, IoSession> registration = new ConcurrentHashMap<String, IoSession>();

    reg = new ResourceRegistry();

    BioUdpServer server = new BioUdpServer();
    final RequestFilter<CoapMessage, CoapMessage> rq = new RequestFilter<CoapMessage, CoapMessage>();

    server.setFilters(/* new LoggingFilter(), */new ProtocolCodecFilter<CoapMessage, ByteBuffer, Void, Void>(
            new CoapEncoder(), new CoapDecoder()), rq);
    // idle in 10 minute
    server.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 10 * 60 * 1000);
    server.setIoHandler(new AbstractIoHandler() {

        long start = System.currentTimeMillis();
        int count = 0;

        @Override//from w  w w .  j  av a 2 s  .  c  o m
        public void messageReceived(IoSession session, Object message) {
            System.err.println("rcv : " + message);

            CoapMessage resp = reg.respond((CoapMessage) message, session);
            System.err.println("resp : " + resp);
            session.write(resp);
            count++;
            if (count >= 100000) {
                System.err.println("time for 100k msg : " + (System.currentTimeMillis() - start));
                count = 0;
                start = System.currentTimeMillis();
            }
        }

        @Override
        public void messageSent(IoSession session, Object message) {
            System.err.println("sent : " + message);
        }

        @Override
        public void sessionIdle(IoSession session, IdleStatus status) {
            System.err.println("idle closing");
            session.close(false);
        }
    });

    try {
        server.bind(5683);
        new Thread() {
            @Override
            public void run() {
                for (;;) {
                    for (IoSession s : registration.values()) {
                        rq.request(s, CoapMessage.get("st", true), 15000)
                                .register(new AbstractIoFutureListener<CoapMessage>() {
                                    @Override
                                    public void completed(CoapMessage result) {
                                        System.err.println("status : " + result);
                                    }
                                });
                    }

                    try {
                        // let's poll every 10 seconds
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }.start();

        for (;;) {
            Thread.sleep(1000);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

From source file:org.geb.core.thrift.TcpServer.java

public TcpServer(final int port) {
    this.port = port;

    final TcpSessionConfig tcpSessionConfig = new DefaultTcpSessionConfig();

    tcpSessionConfig.setReadBufferSize(8192);
    tcpSessionConfig.setSendBufferSize(8192);

    tcpSessionConfig.setIdleTimeInMillis(IdleStatus.READ_IDLE, 10000l);
    tcpSessionConfig.setIdleTimeInMillis(IdleStatus.WRITE_IDLE, 10000l);

    ioServer = new NioTcpServer(tcpSessionConfig);
}

From source file:org.geb.core.transport.tcp.mina.MinaTcpServer.java

public MinaTcpServer(final int port, final String srcPeerID, final boolean noProp) throws UnknownHostException {
    this.port = port;
    this.protocolAddress = InetAddress.getLocalHost().getHostAddress() + ":" + port;
    this.srcPeerID = srcPeerID;
    //        this.welcomeMessage = welcomeMessage;
    this.noProp = noProp;

    final TcpSessionConfig tcpSessionConfig = new DefaultTcpSessionConfig();

    tcpSessionConfig.setReadBufferSize(Constants.MAX_MESSAGE_LENGTH);
    tcpSessionConfig.setSendBufferSize(Constants.MAX_MESSAGE_LENGTH);

    tcpSessionConfig.setIdleTimeInMillis(IdleStatus.READ_IDLE, 10000l);
    tcpSessionConfig.setIdleTimeInMillis(IdleStatus.WRITE_IDLE, 10000l);

    ioServer = new NioTcpServer(tcpSessionConfig);
}