Search in sources :

Example 11 with Channel

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

the class TestSimpleHttpClient2 method main.

// telnet 192.168.133.134 8080
public static void main(String[] args) throws Exception {
    String host = "www.baidu.com";
    String url = "/plaintext";
    host = "firenio.com";
    host = "127.0.0.1";
    // host = "fe80::a793:9577:4396:8ca6";
    // host = "www.baidu.com";
    // host = "api.weixin.qq.com";
    // host = "192.168.1.103";
    int port = 1443;
    port = 8300;
    // port = 443;
    ChannelConnector context = new ChannelConnector(host, port);
    context.addProtocolCodec(new ClientHttpCodec());
    context.addProtocolCodec(new WebSocketCodec());
    context.setIoEventHandle(new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            ClientHttpFrame res = (ClientHttpFrame) frame;
            System.out.println();
            System.out.println(new String(res.getBytesContent()));
            System.out.println();
        // Util.close(context);
        }
    });
    context.addChannelEventListener(new ChannelEventListenerAdapter() {

        @Override
        public void channelOpened(Channel ch) throws Exception {
            throw new IOException();
        }
    });
    context.addChannelEventListener(new LoggerChannelOpenListener());
    if (port == 1443 || port == 443) {
        context.setSslContext(SslContextBuilder.forClient(true).build());
    }
    Channel ch = context.connect(999999);
    ClientHttpFrame f = new ClientHttpFrame(url, HttpMethod.GET);
    // f.setRequestHeader(HttpHeader.Host, host);
    // f.setContent("abc123".readBytes());
    ch.write(f);
    ch.writeAndFlush(f);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) Channel(com.firenio.component.Channel) IOException(java.io.IOException) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) IOException(java.io.IOException) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec)

Example 12 with Channel

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

the class TestHttp2Server method main.

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

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            frame.write("Hello World", ch);
            ch.writeAndFlush(frame);
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setMemoryCapacity(1024 * 1024 * 4);
    group.setMemoryUnit(512);
    group.setEnableMemoryPool(true);
    ChannelAcceptor context = new ChannelAcceptor(group, 443);
    context.addProtocolCodec(new Http2Codec());
    context.setIoEventHandle(eventHandleAdaptor);
    // context.setApplicationProtocols(new String[]{"h2", "http/1.1"});
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) Http2Codec(com.firenio.codec.http2.Http2Codec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 13 with Channel

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

the class TestLengthValueServer method main.

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

        @Override
        public void accept(Channel ch, Frame f) throws Exception {
            String text = f.getStringContent();
            f.setContent(ch.allocateWithSkipHeader(1));
            f.write("yes server already accept your message:", ch);
            f.write(text, ch);
            ch.writeAndFlush(f);
        }
    };
    ChannelAcceptor context = new ChannelAcceptor(8300);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setIoEventHandle(eventHandleAdaptor);
    context.addProtocolCodec(new LengthValueCodec());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 14 with Channel

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

the class WebSocketMsgAdapter method doLoop.

@Override
protected void doLoop() throws InterruptedException {
    Msg msg = (Msg) msgs.poll(16, TimeUnit.MILLISECONDS);
    if (msg == null) {
        return;
    }
    if (msg.ch != null) {
        WebSocketFrame f = new WebSocketFrame();
        f.setString(msg.msg, msg.ch);
        try {
            msg.ch.writeAndFlush(f);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    } else {
        if (!clientMap.isEmpty()) {
            synchronized (this) {
                Client client = clientMap.values().iterator().next();
                Channel ch = client.channel;
                WebSocketFrame f = new WebSocketFrame();
                byte[] data = msg.msg.getBytes();
                f.setContent(data);
                try {
                    ByteBuf buf = ch.getCodec().encode(ch, f);
                    ChannelManagerListener.broadcast(buf, channelMap.values());
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }
}
Also used : Channel(com.firenio.component.Channel) WebSocketFrame(com.firenio.codec.http11.WebSocketFrame) ByteBuf(com.firenio.buffer.ByteBuf)

Example 15 with Channel

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

Aggregations

Channel (com.firenio.component.Channel)33 Frame (com.firenio.component.Frame)28 IoEventHandle (com.firenio.component.IoEventHandle)27 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)26 ChannelConnector (com.firenio.component.ChannelConnector)19 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)15 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)11 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)10 ChannelAcceptor (com.firenio.component.ChannelAcceptor)9 ByteBuf (com.firenio.buffer.ByteBuf)5 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)5 HttpFrame (com.firenio.codec.http11.HttpFrame)5 ProtobaseCodec (com.firenio.codec.protobase.ProtobaseCodec)5 Waiter (com.firenio.concurrent.Waiter)5 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)4 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)4 ProtobaseFrame (com.firenio.codec.protobase.ProtobaseFrame)4 ChannelEventListenerAdapter (com.firenio.component.ChannelEventListenerAdapter)4 WebSocketFrame (com.firenio.codec.http11.WebSocketFrame)3 HttpCodec (com.firenio.codec.http11.HttpCodec)2