Search in sources :

Example 1 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor 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 SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.

the class SimpleTestFIxedLengthServer method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            future.write("yes server already accept your message:");
            future.write(future.getReadText());
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    // use java aio
    // SocketChannelContext context = new AioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    acceptor.bind();
}
Also used : FLBeatFutureFactory(com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory) 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) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 3 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.

the class BalanceFacadeAcceptor method start.

public void start(BalanceContext balanceContext, SocketChannelContext facadeContext, SocketChannelContext reverseContext) throws IOException {
    if (balanceContext == null) {
        throw new IllegalArgumentException("null configuration");
    }
    synchronized (this) {
        if (running) {
            return;
        }
        this.balanceContext = balanceContext;
        this.balanceContext.getBalanceReverseAcceptor().start(reverseContext);
        this.channelAcceptor = new SocketChannelAcceptor(facadeContext);
        this.channelAcceptor.bind();
        LoggerUtil.prettyLog(LoggerFactory.getLogger(BalanceFacadeAcceptor.class), "Balance Facade Acceptor startup completed ...");
    }
}
Also used : SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Example 4 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.

the class BalanceReverseAcceptor method start.

public void start(SocketChannelContext context) throws IOException {
    this.acceptor = new SocketChannelAcceptor(context);
    this.acceptor.bind();
    LoggerUtil.prettyLog(logger, "Balance Reverse Acceptor startup completed ...");
}
Also used : SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Example 5 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor 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

SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)14 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)12 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)12 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)11 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)11 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)11 SocketSession (com.generallycloud.baseio.component.SocketSession)11 Future (com.generallycloud.baseio.protocol.Future)11 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)5 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)3 SslContext (com.generallycloud.baseio.component.ssl.SslContext)3 File (java.io.File)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CharBasedProtocolFactory (com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)2 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)2 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)2 ServerHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ServerHTTPProtocolFactory)2 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)2 IOException (java.io.IOException)2 ChannelAcceptor (com.generallycloud.baseio.acceptor.ChannelAcceptor)1