Search in sources :

Example 21 with SocketChannelContext

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

the class BalanceServerBootStrap method getBalanceChannelContext.

private SocketChannelContext getBalanceChannelContext(BalanceContext balanceContext, ServerConfiguration configuration, ProtocolFactory protocolFactory) {
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    // SocketChannelContext context = new AioSocketChannelContext(configuration);
    context.setIoEventHandleAdaptor(balanceContext.getBalanceFacadeAcceptorHandler());
    context.addSessionEventListener(balanceContext.getBalanceFacadeAcceptorSEListener());
    context.setProtocolFactory(protocolFactory);
    context.setBeatFutureFactory(balanceBeatFutureFactory);
    if (balanceSessionEventListeners != null) {
        addSessionEventListener2Context(context, balanceSessionEventListeners);
    }
    if (balanceSessionIdleEventListeners != null) {
        addSessionIdleEventListener2Context(context, balanceSessionIdleEventListeners);
    }
    if (sslContext != null) {
        context.setSslContext(sslContext);
    }
    return context;
}
Also used : NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 22 with SocketChannelContext

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

the class TestPrintBusyServlet method doAccept.

@Override
protected void doAccept(HttpSession session, HttpFuture future) throws Exception {
    SocketChannelContext context = session.getIoSession().getContext();
    PooledByteBufAllocatorManager allocator = (PooledByteBufAllocatorManager) context.getByteBufAllocatorManager();
    allocator.printBusy();
    future.write("true");
    future.setResponseHeader("Content-Type", "text/html");
    session.flush(future);
}
Also used : PooledByteBufAllocatorManager(com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext)

Example 23 with SocketChannelContext

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

the class TestShowMemoryServlet method doAccept.

@Override
protected void doAccept(HttpSession session, HttpFuture future) throws Exception {
    ApplicationContext appContext = ApplicationContext.getInstance();
    FutureAcceptorServiceLoader fasLoader = appContext.getFutureAcceptorServiceLoader();
    TestWebSocketChatServlet chatServlet = (TestWebSocketChatServlet) fasLoader.getFutureAcceptor("/web-socket-chat");
    TestWebSocketRumpetrollServlet rumpetrollServlet = (TestWebSocketRumpetrollServlet) fasLoader.getFutureAcceptor("/web-socket-rumpetroll");
    WebSocketMsgAdapter chatMsgAdapter = chatServlet.getMsgAdapter();
    WebSocketMsgAdapter rumpetrollMsgAdapter = rumpetrollServlet.getMsgAdapter();
    SocketChannelContext context = session.getIoSession().getContext();
    HttpContext httpContext = session.getContext();
    BigDecimal time = new BigDecimal(System.currentTimeMillis() - context.getStartupTime());
    BigDecimal anHour = new BigDecimal(60 * 60 * 1000);
    BigDecimal hour = time.divide(anHour, 3, RoundingMode.HALF_UP);
    ByteBufAllocatorManager allocator = context.getByteBufAllocatorManager();
    String allocatorDes = "unpooled";
    if (allocator instanceof PooledByteBufAllocatorManager) {
        allocatorDes = ((PooledByteBufAllocatorManager) allocator).toDebugString();
    }
    ServerConfiguration configuration = context.getServerConfiguration();
    int SERVER_CORE_SIZE = configuration.getSERVER_CORE_SIZE();
    int SERVER_MEMORY_POOL_CAPACITY = configuration.getSERVER_MEMORY_POOL_CAPACITY() * SERVER_CORE_SIZE;
    int SERVER_MEMORY_POOL_UNIT = configuration.getSERVER_MEMORY_POOL_UNIT();
    double MEMORY_POOL_SIZE = new BigDecimal(SERVER_MEMORY_POOL_CAPACITY * SERVER_MEMORY_POOL_UNIT).divide(new BigDecimal(1024 * 1024), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
    int M = 1024 * 1024;
    Runtime runtime = Runtime.getRuntime();
    StringBuilder builder = new StringBuilder(HtmlUtil.HTML_HEADER);
    builder.append("		<div style=\"margin-left:20px;\">\n");
    builder.append("服务器内存使用情况:</BR>\n");
    builder.append("虚拟机占用内存:");
    builder.append(runtime.totalMemory() / M);
    builder.append("M;\n</BR>已占用内存:");
    builder.append((runtime.totalMemory() - runtime.freeMemory()) / M);
    builder.append("M;\n</BR>空闲内存:");
    builder.append(runtime.freeMemory() / M);
    builder.append("M;\n</BR>内存池大小:");
    builder.append(MEMORY_POOL_SIZE);
    builder.append("M;\n</BR>内存池状态(Heap):");
    builder.append(allocatorDes);
    builder.append("\n</BR>聊天室(WebSocket)客户端数量:");
    builder.append(chatMsgAdapter.getClientSize());
    builder.append("\n</BR>小蝌蚪(WebSocket)客户端数量:");
    builder.append(rumpetrollMsgAdapter.getClientSize());
    builder.append("\n</BR>服务器当前连接数(io-session):");
    builder.append(context.getSessionManager().getManagedSessionSize());
    builder.append(";\n</BR>服务器当前会话数(http-session):");
    builder.append(httpContext.getHttpSessionManager().getManagedSessionSize());
    builder.append(";\n</BR>服务运行时间:");
    builder.append(hour + "H;");
    builder.append("		</div>\n");
    builder.append(HtmlUtil.HTML_POWER_BY);
    builder.append(HtmlUtil.HTML_BOTTOM);
    future.write(builder.toString());
    future.setResponseHeader("Content-Type", "text/html");
    session.flush(future);
}
Also used : ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) HttpContext(com.generallycloud.baseio.container.http11.HttpContext) PooledByteBufAllocatorManager(com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager) FutureAcceptorServiceLoader(com.generallycloud.baseio.container.service.FutureAcceptorServiceLoader) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ByteBufAllocatorManager(com.generallycloud.baseio.buffer.ByteBufAllocatorManager) PooledByteBufAllocatorManager(com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager) BigDecimal(java.math.BigDecimal) ApplicationContext(com.generallycloud.baseio.container.ApplicationContext)

Example 24 with SocketChannelContext

use of com.generallycloud.baseio.component.SocketChannelContext 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 25 with SocketChannelContext

use of com.generallycloud.baseio.component.SocketChannelContext 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)

Aggregations

SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)65 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)55 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)55 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)54 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)40 SocketSession (com.generallycloud.baseio.component.SocketSession)36 Future (com.generallycloud.baseio.protocol.Future)33 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)29 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)27 FixedSession (com.generallycloud.baseio.container.FixedSession)20 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)20 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)17 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)12 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)12 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)8 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)8 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)7 ParamedProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)6 MessageConsumer (com.generallycloud.baseio.container.jms.client.MessageConsumer)6 DefaultMessageConsumer (com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer)6