Search in sources :

Example 1 with ApplicationContext

use of com.generallycloud.baseio.container.ApplicationContext in project baseio by generallycloud.

the class ApplicationBootstrapEngine method bootstrap.

@Override
public void bootstrap(String rootPath, boolean deployModel) throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    Properties serverProperties = FileUtil.readPropertiesByCls("server.properties");
    ServerConfiguration sc = new ServerConfiguration();
    new PropertiesSCLoader("SERVER").loadConfiguration(sc, serverProperties);
    ApplicationContext applicationContext = new ApplicationContext(rootPath);
    applicationContext.setDeployModel(deployModel);
    SocketChannelContext channelContext = new NioSocketChannelContext(sc);
    // SocketChannelContext channelContext = new AioSocketChannelContext(sc);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(channelContext);
    try {
        Properties intfProperties = FileUtil.readPropertiesByCls("intf.properties");
        applicationContext.setBlackIPs(loadBlackIPs());
        applicationContext.setChannelContext(channelContext);
        ApplicationConfigurationLoader acLoader = loadConfigurationLoader(intfProperties.getProperty("intf.ApplicationConfigurationLoader"));
        ApplicationExtLoader applicationExtLoader = loadApplicationExtLoader(intfProperties.getProperty("intf.ApplicationExtLoader"));
        ApplicationContextEnricher enricher = loadApplicationContextEnricher(intfProperties.getProperty("intf.ApplicationContextEnricher"));
        applicationContext.setApplicationExtLoader(applicationExtLoader);
        applicationContext.setApplicationConfigurationLoader(acLoader);
        enricher.enrich(applicationContext);
        channelContext.setIoEventHandleAdaptor(new ApplicationIoEventHandle(applicationContext));
        if (sc.isSERVER_ENABLE_SSL()) {
            if (!StringUtil.isNullOrBlank(sc.getSERVER_CERT_KEY())) {
                File certificate = FileUtil.readFileByCls(sc.getSERVER_CERT_CRT(), classLoader);
                File privateKey = FileUtil.readFileByCls(sc.getSERVER_CERT_KEY(), classLoader);
                SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
                channelContext.setSslContext(sslContext);
            } else {
                String keystoreInfo = sc.getSERVER_SSL_KEYSTORE();
                if (StringUtil.isNullOrBlank(keystoreInfo)) {
                    throw new IllegalArgumentException("ssl enabled,but no config for");
                }
                String[] params = keystoreInfo.split(";");
                if (params.length != 4) {
                    throw new IllegalArgumentException("SERVER_SSL_KEYSTORE config error");
                }
                File storeFile = FileUtil.readFileByCls(params[0], classLoader);
                SslContext sslContext = SSLUtil.initServer(storeFile, params[1], params[2], params[3]);
                channelContext.setSslContext(sslContext);
            }
        }
        sc.setSERVER_PORT(getServerPort(sc.getSERVER_PORT(), sc.isSERVER_ENABLE_SSL()));
        acceptor.bind();
    } catch (Throwable e) {
        Logger logger = LoggerFactory.getLogger(getClass());
        logger.error(e.getMessage(), e);
        CloseUtil.unbind(acceptor);
    }
}
Also used : PropertiesSCLoader(com.generallycloud.baseio.configuration.PropertiesSCLoader) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ApplicationIoEventHandle(com.generallycloud.baseio.container.ApplicationIoEventHandle) Properties(com.generallycloud.baseio.common.Properties) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) Logger(com.generallycloud.baseio.log.Logger) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ApplicationContext(com.generallycloud.baseio.container.ApplicationContext) ApplicationExtLoader(com.generallycloud.baseio.container.ApplicationExtLoader) ApplicationContextEnricher(com.generallycloud.baseio.container.ApplicationContextEnricher) ApplicationConfigurationLoader(com.generallycloud.baseio.container.configuration.ApplicationConfigurationLoader) File(java.io.File) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Example 2 with ApplicationContext

use of com.generallycloud.baseio.container.ApplicationContext in project baseio by generallycloud.

the class RTPServerDPAcceptor method execute.

@Override
protected void execute(DatagramSession dSession, DatagramRequest request) {
    String serviceName = request.getFutureName();
    if (BIND_SESSION.equals(serviceName)) {
        Parameters parameters = request.getParameters();
        ApplicationContext context = ApplicationContext.getInstance();
        LoginCenter loginCenter = AuthorityContext.getInstance().getLoginCenter();
        if (!loginCenter.isValidate(parameters)) {
            return;
        }
        // FIXME udp
        SocketChannelContext channelContext = context.getChannelContext();
        SocketSessionManager sessionManager = channelContext.getSessionManager();
        // Session session = factory.getSession(username);
        SocketSession session = null;
        if (session == null) {
            return;
        }
        // session.setDatagramChannel(channel); //FIXME udp
        ProtobaseFuture future = new ProtobaseFutureImpl(session.getContext(), BIND_SESSION_CALLBACK);
        logger.debug("___________________bind___session___{}", session);
        future.write("1");
        session.flush(future);
    } else {
        logger.debug(">>>> {}", request.getFutureName());
    }
}
Also used : ApplicationContext(com.generallycloud.baseio.container.ApplicationContext) Parameters(com.generallycloud.baseio.component.Parameters) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) SocketSessionManager(com.generallycloud.baseio.component.SocketSessionManager) LoginCenter(com.generallycloud.baseio.container.LoginCenter) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext)

Example 3 with ApplicationContext

use of com.generallycloud.baseio.container.ApplicationContext 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)

Aggregations

SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)3 ApplicationContext (com.generallycloud.baseio.container.ApplicationContext)3 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)2 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)1 ByteBufAllocatorManager (com.generallycloud.baseio.buffer.ByteBufAllocatorManager)1 PooledByteBufAllocatorManager (com.generallycloud.baseio.buffer.PooledByteBufAllocatorManager)1 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)1 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)1 Properties (com.generallycloud.baseio.common.Properties)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1 Parameters (com.generallycloud.baseio.component.Parameters)1 SocketSession (com.generallycloud.baseio.component.SocketSession)1 SocketSessionManager (com.generallycloud.baseio.component.SocketSessionManager)1 SslContext (com.generallycloud.baseio.component.ssl.SslContext)1 PropertiesSCLoader (com.generallycloud.baseio.configuration.PropertiesSCLoader)1 ApplicationContextEnricher (com.generallycloud.baseio.container.ApplicationContextEnricher)1 ApplicationExtLoader (com.generallycloud.baseio.container.ApplicationExtLoader)1 ApplicationIoEventHandle (com.generallycloud.baseio.container.ApplicationIoEventHandle)1 LoginCenter (com.generallycloud.baseio.container.LoginCenter)1 ApplicationConfigurationLoader (com.generallycloud.baseio.container.configuration.ApplicationConfigurationLoader)1