Search in sources :

Example 6 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl in project baseio by generallycloud.

the class TestHeartBeat method main.

public static void main(String[] args) throws Exception {
    DebugUtil.setEnableDebug(true);
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            DebugUtil.debug("______________" + future.getReadText());
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(18300);
    configuration.setSERVER_SESSION_IDLE_TIME(20);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.addSessionIdleEventListener(new SocketSessionActiveSEListener());
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    SocketSession session = connector.connect();
    String param = "tttt";
    long old = System.currentTimeMillis();
    for (int i = 0; i < 5; i++) {
        Future future = new FixedLengthFutureImpl(context);
        future.write(param);
        session.flush(future);
        ThreadUtil.sleep(300);
    }
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    Thread.sleep(2000);
    CloseUtil.close(connector);
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) FLBeatFutureFactory(com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) SocketSessionActiveSEListener(com.generallycloud.baseio.component.SocketSessionActiveSEListener)

Example 7 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl in project baseio by generallycloud.

the class SimpleTestFIxedLengthBroadcastServer method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            future.write("yes server already accept your message:");
            future.write(future.getReadText());
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(8300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.addSessionEventListener(new SetOptionListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    acceptor.bind();
    ThreadUtil.execute(new Runnable() {

        @Override
        public void run() {
            for (; ; ) {
                ThreadUtil.sleep(1000);
                FixedLengthFuture future = new FixedLengthFutureImpl(context);
                future.write("broadcast msg .........................");
                try {
                    acceptor.broadcast(future);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Example 8 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl in project baseio by generallycloud.

the class SimpleTestFIxedLengthClient method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 8300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    SocketSession session = connector.connect();
    FixedLengthFuture future = new FixedLengthFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Aggregations

FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)8 SocketSession (com.generallycloud.baseio.component.SocketSession)8 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)7 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)7 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)7 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)7 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)7 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)7 Future (com.generallycloud.baseio.protocol.Future)7 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)6 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)5 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)2 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)2 IOException (java.io.IOException)2 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)1 AioSocketChannelContext (com.generallycloud.baseio.component.AioSocketChannelContext)1 SocketSessionActiveSEListener (com.generallycloud.baseio.component.SocketSessionActiveSEListener)1 SocketSessionEventListener (com.generallycloud.baseio.component.SocketSessionEventListener)1 SocketSessionManager (com.generallycloud.baseio.component.SocketSessionManager)1 SslContext (com.generallycloud.baseio.component.ssl.SslContext)1