Search in sources :

Example 1 with SslContext

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

use of com.generallycloud.baseio.component.ssl.SslContext in project baseio by generallycloud.

the class TestFIxedLengthClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SslContext sslContext = SSLUtil.initClient(true);
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SessionActiveSEListener());
    context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    context.setSslContext(sslContext);
    SocketSession session = connector.connect();
    FixedLengthFuture future = new FixedLengthFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
    DebugUtil.debug("连接已关闭。。。");
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) FLBeatFutureFactory(com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Example 3 with SslContext

use of com.generallycloud.baseio.component.ssl.SslContext in project baseio by generallycloud.

the class TestFIxedLengthServer 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));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SocketSessionAliveSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    File certificate = FileUtil.readFileByCls("generallycloud.com.crt");
    File privateKey = FileUtil.readFileByCls("generallycloud.com.key");
    SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
    context.setSslContext(sslContext);
    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) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) File(java.io.File) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Example 4 with SslContext

use of com.generallycloud.baseio.component.ssl.SslContext in project baseio by generallycloud.

the class TestSimpleHttpClient method main.

public static void main(String[] args) throws Exception {
    HttpIOEventHandle eventHandleAdaptor = new HttpIOEventHandle();
    // ServerConfiguration c = new ServerConfiguration("localhost",80);
    ServerConfiguration c = new ServerConfiguration("generallycloud.com", 443);
    SocketChannelContext context = new NioSocketChannelContext(c);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    SslContext sslContext = SSLUtil.initClient(true);
    context.setProtocolFactory(new ClientHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setSslContext(sslContext);
    SocketSession session = connector.connect();
    HttpClient client = new HttpClient(session);
    HttpFuture future = new ClientHttpFuture(context, "/test-show-memory");
    HttpFuture res = client.request(future, 10000);
    System.out.println();
    System.out.println(new String(res.getBodyContent()));
    System.out.println();
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ClientHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) HttpIOEventHandle(com.generallycloud.baseio.codec.http11.HttpIOEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) HttpClient(com.generallycloud.baseio.codec.http11.HttpClient) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Example 5 with SslContext

use of com.generallycloud.baseio.component.ssl.SslContext in project baseio by generallycloud.

the class TestLineBasedServer method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            String res = "yes server already accept your message:" + future.getReadText();
            future.write(res);
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new CharBasedProtocolFactory());
    File certificate = FileUtil.readFileByCls("generallycloud.com.crt");
    File privateKey = FileUtil.readFileByCls("generallycloud.com.key");
    SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
    context.setSslContext(sslContext);
    acceptor.bind();
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) File(java.io.File) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Aggregations

NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)5 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)5 SslContext (com.generallycloud.baseio.component.ssl.SslContext)5 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)5 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)4 SocketSession (com.generallycloud.baseio.component.SocketSession)4 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)3 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)3 Future (com.generallycloud.baseio.protocol.Future)3 File (java.io.File)3 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)2 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)2 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)2 CharBasedProtocolFactory (com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)1 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)1 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)1 ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)1 HttpClient (com.generallycloud.baseio.codec.http11.HttpClient)1 HttpIOEventHandle (com.generallycloud.baseio.codec.http11.HttpIOEventHandle)1 ClientHttpFuture (com.generallycloud.baseio.codec.http11.future.ClientHttpFuture)1