Search in sources :

Example 11 with LoggerChannelOpenListener

use of com.firenio.component.LoggerChannelOpenListener 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 12 with LoggerChannelOpenListener

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

the class HttpProxyServer method strtup.

public synchronized void strtup(NioEventLoopGroup group, int port) throws Exception {
    if (context != null && context.isActive()) {
        return;
    }
    IoEventHandle eventHandle = new IoEventHandle() {

        @Override
        public void accept(Channel ch_src, Frame frame) throws Exception {
            final HttpFrame f = (HttpFrame) frame;
            if (f.getMethod() == HttpMethod.CONNECT) {
                ch_src.writeAndFlush(CONNECT_RES_BUF.duplicate());
                HttpProxyAttr s = HttpProxyAttr.get(ch_src);
                String[] arr = f.getHost().split(":");
                s.host = arr[0];
                s.port = Integer.parseInt(arr[1]);
                s.handshakeFinished = true;
            } else {
                String host = f.getHost();
                String[] arr = host.split(":");
                int port = 80;
                if (arr.length == 2) {
                    port = Integer.parseInt(arr[1]);
                }
                if (f.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId()) == null) {
                    return;
                }
                NioEventLoop el = ch_src.getEventLoop();
                ChannelConnector context = new ChannelConnector(el, arr[0], port);
                context.addProtocolCodec(new ClientHttpCodec());
                context.setIoEventHandle(new IoEventHandle() {

                    @Override
                    public void accept(Channel ch, Frame frame) throws Exception {
                        ClientHttpFrame res = (ClientHttpFrame) frame;
                        IntObjectMap<String> hs = res.getResponse_headers();
                        for (hs.scan(); hs.hasNext(); ) {
                            String v = hs.getValue();
                            if (v == null) {
                                continue;
                            }
                            if (hs.getKey() == HttpHeader.Content_Length.getId() || hs.getKey() == HttpHeader.Connection.getId() || hs.getKey() == HttpHeader.Transfer_Encoding.getId() || hs.getKey() == HttpHeader.Content_Encoding.getId()) {
                                continue;
                            }
                            f.setResponseHeader(hs.getKey(), v.getBytes());
                        }
                        if (res.getContent() != null) {
                            f.setContent(res.getContent());
                        } else if (res.isChunked()) {
                            f.setString("not support chunked now.", ch);
                        }
                        ch_src.writeAndFlush(f);
                        ch.close();
                    }
                });
                String url = parseRequestURL(f.getRequestURL());
                context.setPrintConfig(false);
                context.addChannelEventListener(new LoggerChannelOpenListener());
                context.connect((ch, ex) -> {
                    if (ex == null) {
                        ClientHttpFrame req = new ClientHttpFrame(url, f.getMethod());
                        req.setRequestHeaders(f.getRequestHeaders());
                        req.getRequestHeaders().remove(HttpHeader.Proxy_Connection.getId());
                        if (f.getMethod() == HttpMethod.POST) {
                            req.setContent(f.getContent());
                        }
                        try {
                            ch.writeAndFlush(req);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    };
    context = new ChannelAcceptor(group, 8088);
    context.addProtocolCodec(new HttpProxyCodec());
    context.setIoEventHandle(eventHandle);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) IOException(java.io.IOException) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) IntObjectMap(com.firenio.collection.IntObjectMap) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) NioEventLoop(com.firenio.component.NioEventLoop) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame)

Example 13 with LoggerChannelOpenListener

use of com.firenio.component.LoggerChannelOpenListener 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)

Example 14 with LoggerChannelOpenListener

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

the class TestSimpleWebSocketClient method main.

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

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            if (frame instanceof ClientHttpFrame) {
                ClientHttpFrame f = (ClientHttpFrame) frame;
                if (f.updateWebSocketProtocol(ch)) {
                    WebSocketFrame f2 = new WebSocketFrame();
                    Map<String, String> map = new HashMap<>();
                    map.put("action", "add-user");
                    map.put("username", "火星人" + Util.randomUUID());
                    f2.setString(JSON.toJSONString(map), ch);
                    ch.writeAndFlush(f2);
                }
                System.out.println(f.getResponse_headers());
            } else {
                WebSocketFrame f = (WebSocketFrame) frame;
                System.out.println(f.getStringContent());
            }
        }
    };
    String host = "firenio.com";
    int port = 443;
    NioEventLoopGroup g = new NioEventLoopGroup();
    g.setEnableMemoryPool(false);
    ChannelConnector context = new ChannelConnector(g, host, 443);
    // context.setExecutorGroup(new ExecutorEventLoopGroup());
    context.setIoEventHandle(eventHandleAdaptor);
    context.addProtocolCodec(new ClientHttpCodec());
    context.addProtocolCodec(new WebSocketCodec());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setSslContext(SslContextBuilder.forClient(true).build());
    Channel ch = context.connect();
    String url = "/web-socket-chat";
    HttpFrame frame = new WsUpgradeRequestFrame(url);
    frame.setRequestHeader(HttpHeader.Host, host + port);
    frame.setRequestHeader(HttpHeader.Pragma, "no-cache");
    frame.setRequestHeader(HttpHeader.Cache_Control, "no-cache");
    frame.setRequestHeader(HttpHeader.User_Agent, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
    frame.setRequestHeader(HttpHeader.Accept_Encoding, "gzip, deflate, sdch");
    frame.setRequestHeader(HttpHeader.Accept_Language, "zh-CN,zh;q=0.8");
    ch.writeAndFlush(frame);
    Util.sleep(100);
    WebSocketFrame f2 = new WebSocketFrame();
    Map<String, String> map = new HashMap<>();
    map.put("action", "new-message");
    map.put("message", TestUtil.newString(1024 * 8));
    f2.setString(JSON.toJSONString(map), ch);
    ch.writeAndFlush(f2);
    Util.sleep(999999999);
    Util.close(context);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) WebSocketFrame(com.firenio.codec.http11.WebSocketFrame) WsUpgradeRequestFrame(com.firenio.codec.http11.WsUpgradeRequestFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) HashMap(java.util.HashMap) Channel(com.firenio.component.Channel) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) WsUpgradeRequestFrame(com.firenio.codec.http11.WsUpgradeRequestFrame) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) WebSocketFrame(com.firenio.codec.http11.WebSocketFrame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec)

Example 15 with LoggerChannelOpenListener

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

the class HttpProxy4CloudServer method strtup.

public synchronized void strtup(NioEventLoopGroup serverG, int port) throws Exception {
    if (context != null && context.isActive()) {
        return;
    }
    context = new ChannelAcceptor(serverG, 8088);
    context.addProtocolCodec(new HttpProxy4CloudCodec());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new ChannelEventListenerAdapter() {

        @Override
        public void channelClosed(Channel ch) {
            Util.close(((HttpProxy4CloudAttr) ch.getAttachment()).target);
        }
    });
    context.bind();
}
Also used : Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Aggregations

LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)28 Channel (com.firenio.component.Channel)26 Frame (com.firenio.component.Frame)24 IoEventHandle (com.firenio.component.IoEventHandle)24 ChannelConnector (com.firenio.component.ChannelConnector)17 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)14 ChannelAcceptor (com.firenio.component.ChannelAcceptor)10 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)9 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)7 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)5 ProtobaseCodec (com.firenio.codec.protobase.ProtobaseCodec)5 Waiter (com.firenio.concurrent.Waiter)5 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)4 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)4 ProtobaseFrame (com.firenio.codec.protobase.ProtobaseFrame)4 HttpFrame (com.firenio.codec.http11.HttpFrame)3 ChannelAliveListener (com.firenio.component.ChannelAliveListener)3 ChannelEventListenerAdapter (com.firenio.component.ChannelEventListenerAdapter)3 ByteBuf (com.firenio.buffer.ByteBuf)2 HttpCodec (com.firenio.codec.http11.HttpCodec)2