Search in sources :

Example 1 with NioSocketChannelContext

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

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

the class NioSocketChannelAcceptor method initSelectorLoops.

private void initSelectorLoops() {
    // FIXME socket selector event loop ?
    ServerConfiguration configuration = getContext().getServerConfiguration();
    String eventLoopName = "nio-process(tcp-" + configuration.getSERVER_PORT() + ")";
    int core_size = configuration.getSERVER_CORE_SIZE();
    this.selectorEventLoopGroup = new SocketSelectorEventLoopGroup((NioSocketChannelContext) getContext(), eventLoopName, core_size);
    LifeCycleUtil.start(selectorEventLoopGroup);
}
Also used : ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) SocketSelectorEventLoopGroup(com.generallycloud.baseio.component.SocketSelectorEventLoopGroup) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 3 with NioSocketChannelContext

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

the class TestBalanceBroadcast method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            if (BalanceContext.BALANCE_CHANNEL_LOST.equals(f.getFutureName())) {
                System.out.println("客户端已下线:" + f.getReadText());
            } else {
                System.out.println("~~~~~~收到报文:" + future.toString());
                String res = "(***" + f.getReadText() + "***)";
                System.out.println("~~~~~~处理报文:" + res);
                f.write(res);
                session.flush(future);
            }
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(8800);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    SocketSession session = connector.connect();
    for (; session.isOpened(); ) {
        ProtobaseFuture future = new ProtobaseFutureImpl(context, "broadcast");
        future.setBroadcast(true);
        String msg = "broadcast msg___S:" + System.currentTimeMillis();
        future.write(msg);
        future.writeBinary("__^^^binary^^^__".getBytes());
        session.flush(future);
        ThreadUtil.sleep(10);
    }
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) 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 4 with NioSocketChannelContext

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

the class TestBalanceClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            if (f.hasReadBinary()) {
                System.out.println(f.getReadText() + new String(f.getReadBinary()) + "______R:" + System.currentTimeMillis());
            } else {
                System.out.println(f.getReadText() + "______R:" + System.currentTimeMillis());
            }
            res.incrementAndGet();
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(8600);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.setSocketSessionFactory(new BalanceClientSocketSessionFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    BalanceClientSocketSession session = (BalanceClientSocketSession) connector.connect();
    for (int i = 0; i < 100; i++) {
        int fid = Math.abs(new Random().nextInt());
        ProtobaseFuture future = new ProtobaseFutureImpl(context, "future-name");
        future.write("你好!");
        future.setHashCode(fid);
        session.flush(future);
    }
    ThreadUtil.sleep(300);
    System.out.println("==========" + res.get());
    ThreadUtil.sleep(500000000);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) BalanceClientSocketSession(com.generallycloud.baseio.balance.BalanceClientSocketSession) 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) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketSession(com.generallycloud.baseio.component.SocketSession) BalanceClientSocketSession(com.generallycloud.baseio.balance.BalanceClientSocketSession) BalanceClientSocketSessionFactory(com.generallycloud.baseio.balance.BalanceClientSocketSessionFactory) 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 5 with NioSocketChannelContext

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

the class TestBalanceLoad method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            if (BalanceContext.BALANCE_CHANNEL_LOST.equals(f.getFutureName())) {
                System.out.println("客户端已下线:" + f.getReadText());
            } else {
                System.out.println("收到报文:" + future.toString());
                String res = "_____________" + f.getReadText();
                System.out.println("处理报文:" + res);
                f.write(res);
                session.flush(future);
            }
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(8800);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    connector.connect();
    System.in.read();
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) 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

NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)56 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)54 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)54 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)51 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)40 SocketSession (com.generallycloud.baseio.component.SocketSession)33 Future (com.generallycloud.baseio.protocol.Future)31 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)27 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)26 FixedSession (com.generallycloud.baseio.container.FixedSession)21 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)21 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)16 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)11 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)11 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)7 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)7 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)6 MessageConsumer (com.generallycloud.baseio.container.jms.client.MessageConsumer)6 DefaultMessageConsumer (com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer)6 ParamedProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)5