Search in sources :

Example 11 with SocketSession

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

the class WebSocketMsgAdapter method doLoop.

@Override
protected void doLoop() throws InterruptedException {
    Msg msg = msgs.poll(16, TimeUnit.MILLISECONDS);
    if (msg == null) {
        return;
    }
    synchronized (this) {
        SocketSession session = msg.session;
        if (session != null) {
            WebSocketFuture f = new WebSocketFutureImpl(session.getContext());
            f.write(msg.msg);
            session.flush(f);
            return;
        }
        for (int i = 0; i < clients.size(); i++) {
            SocketSession s = clients.get(i);
            if (s.isOpened()) {
                WebSocketFuture f = new WebSocketFutureImpl(s.getContext());
                f.write(msg.msg);
                s.flush(f);
            } else {
                removeClient(s);
                i--;
            }
        }
    }
}
Also used : SocketSession(com.generallycloud.baseio.component.SocketSession) WebSocketFutureImpl(com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture)

Example 12 with SocketSession

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

the class TestProtobufClient method main.

public static void main(String[] args) throws Exception {
    ProtobufUtil protobufUtil = new ProtobufUtil();
    protobufUtil.regist(SearchRequest.getDefaultInstance());
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            SearchRequest res = (SearchRequest) protobufUtil.getMessage(f);
            System.out.println();
            System.out.println("________" + res);
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SessionActiveSEListener());
    // context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    SocketSession session = connector.connect();
    ProtobaseFuture f = new ProtobaseFutureImpl(context);
    ByteString byteString = ByteString.copyFrom("222".getBytes());
    SearchRequest request = SearchRequest.newBuilder().setCorpus(Corpus.IMAGES).setPageNumber(100).setQuery("test").setQueryBytes(byteString).setResultPerPage(-1).build();
    protobufUtil.writeProtobuf(request, f);
    session.flush(f);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : SearchRequest(com.generallycloud.test.io.protobuf.TestProtoBufBean.SearchRequest) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ByteString(com.google.protobuf.ByteString) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ProtobufUtil(com.generallycloud.baseio.codec.protobuf.ProtobufUtil) 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) 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)

Example 13 with SocketSession

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

the class TestReconnectClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 8300));
    ReconnectableConnector connector = new ReconnectableConnector(context);
    connector.setRetryTime(5000);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    // context.addSessionEventListener(new CloseConnectorSEListener(connector.getRealConnector()));
    connector.connect();
    ThreadUtil.sleep(Long.MAX_VALUE);
    CloseUtil.close(connector);
}
Also used : ReconnectableConnector(com.generallycloud.baseio.connector.ReconnectableConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) 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) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 14 with SocketSession

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

the class TestRedisClient method main.

public static void main(String[] args) throws Exception {
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(6379));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(new RedisIOEventHandle());
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new RedisProtocolFactory());
    SocketSession session = connector.connect();
    RedisClient client = new RedisClient(session);
    String value = client.set("name222", "hello redis!");
    System.out.println("__________________res______" + value);
    value = client.get("name222");
    System.out.println("__________________res______" + value);
    value = client.set("debug", "PONG");
    System.out.println("__________________res______" + value);
    value = client.get("debug");
    System.out.println("__________________res______" + value);
    value = client.ping();
    System.out.println("__________________res______" + value);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : RedisClient(com.generallycloud.baseio.codec.redis.future.RedisClient) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) RedisIOEventHandle(com.generallycloud.baseio.codec.redis.future.RedisIOEventHandle) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) RedisProtocolFactory(com.generallycloud.baseio.codec.redis.RedisProtocolFactory) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 15 with SocketSession

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

the class TestHttpLoadServerAio method main.

public static void main(String[] args) throws Exception {
    final AtomicInteger res = new AtomicInteger();
    final AtomicInteger req = new AtomicInteger();
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            HttpFuture f = (HttpFuture) future;
            String res;
            if (f.hasBodyContent()) {
                byte[] array = f.getBodyContent();
                res = "yes server already accept your message :) </BR><PRE style='font-size: 18px;color: #FF9800;'>" + new String(array) + "</PRE>";
            } else {
                res = "yes server already accept your message :) " + f.getRequestParams();
            }
            f.write(res);
            session.flush(f);
        // System.out.println("req======================"+req.getAndIncrement());
        }
    };
    ServerConfiguration c = new ServerConfiguration(80);
    c.setSERVER_MEMORY_POOL_CAPACITY(2560000);
    c.setSERVER_MEMORY_POOL_UNIT(256);
    c.setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
    c.setSERVER_CORE_SIZE(4);
    c.setSERVER_ENABLE_MEMORY_POOL(true);
    c.setSERVER_MEMORY_POOL_CAPACITY_RATE(0.5);
    SocketChannelContext context = new AioSocketChannelContext(c);
    context.setProtocolFactory(new ServerHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    new SocketChannelAcceptor(context).bind();
    ThreadUtil.sleep(99999999);
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) ServerHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ServerHTTPProtocolFactory) AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketSession(com.generallycloud.baseio.component.SocketSession) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

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