Search in sources :

Example 11 with Future

use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.

the class SimpleTestProtobaseServer 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("receive:" + future.getReadText());
            future.write("yes server already accept your message:");
            future.write(future.getReadText());
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    context.getServerConfiguration().setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
    context.getServerConfiguration().setSERVER_SESSION_IDLE_TIME(60 * 60 * 1000);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setBeatFutureFactory(new ProtobaseBeatFutureFactory());
    context.addSessionIdleEventListener(new SocketSessionAliveSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    acceptor.bind();
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ProtobaseBeatFutureFactory(com.generallycloud.baseio.codec.protobase.future.ProtobaseBeatFutureFactory) SocketSessionAliveSEListener(com.generallycloud.baseio.component.SocketSessionAliveSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 12 with Future

use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.

the class TestSessionDisconnect method main.

public static void main(String[] args) throws Exception {
    String serviceName = "TestSessionDisconnectServlet";
    String param = "ttt";
    LoggerFactory.configure();
    SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
    ServerConfiguration configuration = new ServerConfiguration(8300);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandle);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    FixedSession session = new FixedSession(connector.connect());
    session.login("admin", "admin100");
    ProtobaseFuture future = session.request(serviceName, param);
    System.out.println(future.getReadText());
    session.listen(serviceName, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            ProtobaseFuture f = (ProtobaseFuture) future;
            System.out.println(f.getReadText());
        }
    });
    session.write(serviceName, param);
    ThreadUtil.sleep(9999);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnFuture(com.generallycloud.baseio.container.OnFuture) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) OnFuture(com.generallycloud.baseio.container.OnFuture)

Example 13 with Future

use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.

the class TestSimple method main.

public static void main(String[] args) throws Exception {
    String serviceKey = "/test-simple";
    String param = "ttt";
    IoEventHandleAdaptor eventHandle = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println("________________________" + future.getReadText());
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandle);
    SocketSession session = connector.connect();
    ProtobaseFuture f = new ProtobaseFutureImpl(connector.getContext(), serviceKey);
    f.write(param);
    session.flush(f);
    ThreadUtil.sleep(500);
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 14 with Future

use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.

the class AbstractFutureAcceptor method acceptHeartBeat.

private void acceptHeartBeat(final SocketSession session, final ChannelFuture future) {
    if (future.isPING()) {
        heartBeatLogger.logRequest(session);
        SocketChannelContext context = session.getContext();
        BeatFutureFactory factory = context.getBeatFutureFactory();
        if (factory == null) {
            RuntimeException e = new RuntimeException("none factory of BeatFuture");
            CloseUtil.close(session);
            logger.error(e.getMessage(), e);
            return;
        }
        Future f = factory.createPONGPacket(session);
        session.flush(f);
    } else {
        heartBeatLogger.logResponse(session);
    }
}
Also used : ChannelFuture(com.generallycloud.baseio.protocol.ChannelFuture) Future(com.generallycloud.baseio.protocol.Future)

Example 15 with Future

use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.

the class SocketSessionActiveSEListener method sessionIdled.

@Override
public void sessionIdled(SocketSession session, long lastIdleTime, long currentTime) {
    if (session.isClosed()) {
        logger.info("closed session");
        return;
    }
    if (session.getLastAccessTime() < lastIdleTime) {
        logger.info("Did not detect heartbeat messages in heartbeat cycle, prepare to disconnect {}", session);
        CloseUtil.close(session);
    } else {
        SocketChannelContext context = session.getContext();
        BeatFutureFactory factory = context.getBeatFutureFactory();
        if (factory == null) {
            RuntimeException e = new RuntimeException("none factory of BeatFuture");
            CloseUtil.close(session);
            logger.error(e.getMessage(), e);
            return;
        }
        Future future = factory.createPINGPacket(session);
        if (future == null) {
            // 该session无需心跳,比如HTTP协议
            return;
        }
        session.flush(future);
    }
}
Also used : Future(com.generallycloud.baseio.protocol.Future)

Aggregations

Future (com.generallycloud.baseio.protocol.Future)36 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)33 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)33 SocketSession (com.generallycloud.baseio.component.SocketSession)33 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)33 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)31 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)29 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)21 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)15 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)12 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)12 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)11 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)8 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)7 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)4 FixedSession (com.generallycloud.baseio.container.FixedSession)4 OnFuture (com.generallycloud.baseio.container.OnFuture)4 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)4