Search in sources :

Example 36 with SocketSession

use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.

the class TestBeat 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());
        }
    };
    String serviceKey = "TestSimpleServlet";
    ServerConfiguration configuration = new ServerConfiguration(18300);
    configuration.setSERVER_SESSION_IDLE_TIME(10);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.addSessionIdleEventListener(new SocketSessionActiveSEListener());
    context.setBeatFutureFactory(new ProtobaseBeatFutureFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    SocketSession session = connector.connect();
    String param = "tttt";
    long old = System.currentTimeMillis();
    for (int i = 0; i < 5; i++) {
        Future future = new ProtobaseFutureImpl(context, serviceKey);
        future.write(param);
        session.flush(future);
        ThreadUtil.sleep(300);
    }
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    Thread.sleep(2000);
    CloseUtil.close(connector);
}
Also used : 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) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) ProtobaseBeatFutureFactory(com.generallycloud.baseio.codec.protobase.future.ProtobaseBeatFutureFactory) SocketSession(com.generallycloud.baseio.component.SocketSession) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) Future(com.generallycloud.baseio.protocol.Future) SocketSessionActiveSEListener(com.generallycloud.baseio.component.SocketSessionActiveSEListener)

Example 37 with SocketSession

use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.

the class TestDownload method main.

public static void main(String[] args) throws Exception {
    String serviceName = "TestDownloadServlet";
    String fileName = "upload-flashmail-2.4.exe";
    JSONObject j = new JSONObject();
    j.put(FileReceiveUtil.FILE_NAME, fileName);
    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());
    connector.getContext().setProtocolFactory(new ProtobaseProtocolFactory());
    FixedSession session = new FixedSession(connector.connect());
    final FileReceiveUtil fileReceiveUtil = new FileReceiveUtil("download-");
    session.listen(serviceName, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            try {
                fileReceiveUtil.accept(session, (ParamedProtobaseFuture) future, false);
            } catch (Exception e) {
                DebugUtil.debug(e);
            }
        }
    });
    long old = System.currentTimeMillis();
    session.write(serviceName, j.toJSONString());
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    ThreadUtil.sleep(5000);
    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) JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) FileReceiveUtil(com.generallycloud.baseio.container.FileReceiveUtil) Future(com.generallycloud.baseio.protocol.Future) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture) OnFuture(com.generallycloud.baseio.container.OnFuture) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)

Example 38 with SocketSession

use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.

the class TestListenSimple method main.

public static void main(String[] args) throws Exception {
    String serviceKey = "TestListenSimpleServlet";
    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());
    ProtobaseFuture future = session.request(serviceKey, param);
    System.out.println(future.getReadText());
    session.listen(serviceKey, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            ProtobaseFuture f = (ProtobaseFuture) future;
            System.out.println(f.getReadText());
        }
    });
    session.write(serviceKey, param);
    ThreadUtil.sleep(1000);
    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 39 with SocketSession

use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.

the class TestLineBasedBroadcastServer method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            long old = System.currentTimeMillis();
            String res = "hello world!";
            future.write(res);
            ChannelAcceptor acceptor = (ChannelAcceptor) session.getContext().getChannelService();
            acceptor.broadcast(future);
            long now = System.currentTimeMillis();
            System.out.println("广播花费时间:" + (now - old) + ",连接数:" + session.getContext().getSessionManager().getManagedSessionSize());
        }
    };
    ServerConfiguration configuration = new ServerConfiguration();
    configuration.setSERVER_PORT(18300);
    configuration.setSERVER_SESSION_IDLE_TIME(180000);
    configuration.setSERVER_MEMORY_POOL_CAPACITY(1024 * 512);
    configuration.setSERVER_MEMORY_POOL_UNIT(64);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new CharBasedProtocolFactory());
    acceptor.bind();
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) ChannelAcceptor(com.generallycloud.baseio.acceptor.ChannelAcceptor) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)

Example 40 with SocketSession

use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.

the class TestLineBasedClient 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(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new CharBasedProtocolFactory());
    SocketSession session = connector.connect();
    CharBasedFuture future = new CharBasedFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) CharBasedFutureImpl(com.generallycloud.baseio.codec.charbased.future.CharBasedFutureImpl) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)

Aggregations

SocketSession (com.generallycloud.baseio.component.SocketSession)45 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)36 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)35 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)35 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)33 Future (com.generallycloud.baseio.protocol.Future)33 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)29 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)23 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)15 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)13 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)12 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)11 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)9 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)8 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)8 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 JSONObject (com.alibaba.fastjson.JSONObject)4 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)4 ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)4