Search in sources :

Example 1 with ChannelAliveListener

use of com.firenio.component.ChannelAliveListener in project baseio by generallycloud.

the class TestProtobase method main.

@Before
public void main() throws Exception {
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame f) throws Exception {
            if (f.isText()) {
                String text = f.getStringContent();
                DebugUtil.debug("receive text:" + text);
                f.setContent(ch.allocateWithSkipHeader(1));
                f.write(res, ch);
                f.write(text, ch);
                ch.writeAndFlush(f);
            } else {
                byte[] text = f.getBytesContent();
                f.setContent(ch.allocateWithSkipHeader(text.length));
                f.write(res, ch);
                f.write(text);
                ch.writeAndFlush(f);
            }
        }
    };
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelIdleEventListener(new ChannelAliveListener());
    context.setIoEventHandle(eventHandleAdaptor);
    context.addProtocolCodec(new ProtobaseCodec());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) ProtobaseFrame(com.firenio.codec.protobase.ProtobaseFrame) Frame(com.firenio.component.Frame) ChannelAliveListener(com.firenio.component.ChannelAliveListener) Channel(com.firenio.component.Channel) ProtobaseCodec(com.firenio.codec.protobase.ProtobaseCodec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) Before(org.junit.Before)

Example 2 with ChannelAliveListener

use of com.firenio.component.ChannelAliveListener in project baseio by generallycloud.

the class NetDataTransferServer method startup.

public synchronized void startup(NioEventLoopGroup group, int port) throws Exception {
    ChannelAcceptor context = new ChannelAcceptor(group, port);
    context.addProtocolCodec(new NetDataTransfer());
    context.addChannelIdleEventListener(new ChannelAliveListener());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new CountChannelListener());
    context.addChannelEventListener(CLOSE_TARGET);
    context.bind();
}
Also used : ChannelAliveListener(com.firenio.component.ChannelAliveListener) ChannelAcceptor(com.firenio.component.ChannelAcceptor) CountChannelListener(sample.http11.service.CountChannelListener) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 3 with ChannelAliveListener

use of com.firenio.component.ChannelAliveListener in project baseio by generallycloud.

the class TestHttpBootstrapEngine method bootstrap.

@Override
public void bootstrap(String rootPath, boolean prodMode) throws Exception {
    ClassLoader cl = this.getClass().getClassLoader();
    boolean debug = Util.isTrueValue(System.getProperty("http.debug"));
    if (debug) {
        for (; debug; ) {
            Util.sleep(100);
        }
    }
    DevelopConfig.NATIVE_DEBUG = true;
    DevelopConfig.BUF_DEBUG = true;
    DevelopConfig.BUF_PATH_DEBUG = true;
    DevelopConfig.SSL_DEBUG = true;
    DevelopConfig.CHANNEL_DEBUG = true;
    DevelopConfig.DEBUG_ERROR = true;
    Options.setEnableEpoll(true);
    Options.setEnableUnsafe(true);
    Options.setEnableOpenssl(true);
    Options.setBufThreadYield(true);
    // Options.setEnableUnsafeBuf(true);
    HttpDateUtil.start();
    final SpringHttpFrameHandle handle = new SpringHttpFrameHandle();
    Properties properties = FileUtil.readPropertiesByCls("server.properties", cl);
    NioEventLoopGroup group = new NioEventLoopGroup(true);
    ChannelAcceptor context = new ChannelAcceptor(group);
    ConfigurationParser.parseConfiguration("server.", context, properties);
    ConfigurationParser.parseConfiguration("server.", group, properties);
    context.setIoEventHandle(handle);
    context.addChannelIdleEventListener(new ChannelAliveListener());
    context.addChannelEventListener(new WebSocketChannelListener());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new CountChannelListener());
    context.setExecutorGroup(new ThreadEventLoopGroup());
    context.addLifeCycleListener(new LifeCycleListener() {

        @Override
        public void lifeCycleStarting(LifeCycle lifeCycle) {
            try {
                handle.initialize(context, rootPath, prodMode);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

        @Override
        public void lifeCycleStopped(LifeCycle lifeCycle) {
            handle.destroy(context);
        }
    });
    String[] applicationProtocols = null;
    if (properties.getBooleanProperty("app.enableHttp2")) {
        context.addProtocolCodec(new Http2Codec());
        applicationProtocols = new String[] { "h2", "http/1.1" };
    } else {
        context.addProtocolCodec(new HttpCodec(4));
        context.addProtocolCodec(new WebSocketCodec());
    }
    int defaultPort = 80;
    String pem = properties.getProperty("server.sslPem");
    if (!Util.isNullOrBlank(pem)) {
        defaultPort = 443;
        SslContext sslContext = loadSslContextFromPem(pem, applicationProtocols, cl);
        context.setSslContext(sslContext);
    }
    int port = properties.getIntegerProperty("server.port", defaultPort);
    context.setPort(port);
    try {
        context.bind();
    } catch (Exception e) {
        HttpDateUtil.stop();
        group.stop();
        throw e;
    }
    this.group = group;
    this.context = context;
    if (properties.getBooleanProperty("app.proxy")) {
        NetDataTransferServer.get().startup(group, 18088);
    }
}
Also used : LifeCycle(com.firenio.LifeCycle) ChannelAliveListener(com.firenio.component.ChannelAliveListener) ChannelAcceptor(com.firenio.component.ChannelAcceptor) LifeCycleListener(com.firenio.LifeCycleListener) Properties(com.firenio.common.Properties) WebSocketChannelListener(com.firenio.codec.http11.WebSocketChannelListener) SSLException(javax.net.ssl.SSLException) Http2Codec(com.firenio.codec.http2.Http2Codec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) HttpCodec(com.firenio.codec.http11.HttpCodec) CountChannelListener(sample.http11.service.CountChannelListener) SpringHttpFrameHandle(sample.http11.SpringHttpFrameHandle) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) SslContext(com.firenio.component.SslContext)

Aggregations

ChannelAliveListener (com.firenio.component.ChannelAliveListener)3 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)3 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 CountChannelListener (sample.http11.service.CountChannelListener)2 LifeCycle (com.firenio.LifeCycle)1 LifeCycleListener (com.firenio.LifeCycleListener)1 HttpCodec (com.firenio.codec.http11.HttpCodec)1 WebSocketChannelListener (com.firenio.codec.http11.WebSocketChannelListener)1 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)1 Http2Codec (com.firenio.codec.http2.Http2Codec)1 ProtobaseCodec (com.firenio.codec.protobase.ProtobaseCodec)1 ProtobaseFrame (com.firenio.codec.protobase.ProtobaseFrame)1 Properties (com.firenio.common.Properties)1 Channel (com.firenio.component.Channel)1 Frame (com.firenio.component.Frame)1 IoEventHandle (com.firenio.component.IoEventHandle)1 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)1 SslContext (com.firenio.component.SslContext)1 ThreadEventLoopGroup (com.firenio.concurrent.ThreadEventLoopGroup)1 SSLException (javax.net.ssl.SSLException)1