Search in sources :

Example 6 with ChannelConnector

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

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

the class TestProtobase method testBinary.

public void testBinary() throws Exception {
    Waiter<String> w = new Waiter<>();
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame f) {
            String text = new String(f.getBytesContent(), ch.getCharset());
            System.out.println();
            System.out.println("____________________" + text);
            System.out.println();
            Util.close(ch);
            w.call(text, null);
        }
    };
    ChannelConnector context = new ChannelConnector(8300);
    context.setIoEventHandle(eventHandleAdaptor);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new ProtobaseCodec());
    Channel ch = context.connect();
    ProtobaseFrame f = new ProtobaseFrame();
    f.setBinary();
    f.setString(hello, ch);
    ch.writeAndFlush(f);
    w.await(3000);
    Assert.assertEquals(w.getResponse(), res + hello);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) ProtobaseFrame(com.firenio.codec.protobase.ProtobaseFrame) Frame(com.firenio.component.Frame) ProtobaseFrame(com.firenio.codec.protobase.ProtobaseFrame) Channel(com.firenio.component.Channel) ChannelConnector(com.firenio.component.ChannelConnector) ProtobaseCodec(com.firenio.codec.protobase.ProtobaseCodec) Waiter(com.firenio.concurrent.Waiter) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 8 with ChannelConnector

use of com.firenio.component.ChannelConnector 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 9 with ChannelConnector

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

the class TestHeartBeat method main.

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

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            DebugUtil.debug("______________" + frame);
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setIdleTime(20);
    ChannelConnector context = new ChannelConnector(group, "127.0.0.1", 8300);
    context.addChannelIdleEventListener(new ChannelActiveListener());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    context.setIoEventHandle(eventHandleAdaptor);
    Channel ch = context.connect();
    String param = "tttt";
    long old = Util.now();
    for (int i = 0; i < 5; i++) {
        Frame frame = new LengthValueFrame();
        frame.setString(param, ch);
        ch.writeAndFlush(frame);
        Util.sleep(300);
    }
    System.out.println("Time:" + (Util.past(old)));
    Thread.sleep(2000);
    Util.close(context);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) ChannelActiveListener(com.firenio.component.ChannelActiveListener) Channel(com.firenio.component.Channel) ChannelConnector(com.firenio.component.ChannelConnector) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 10 with ChannelConnector

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

the class TestLengthValueClient method main.

public static void main(String[] args) throws Exception {
    ChannelConnector context = new ChannelConnector("127.0.0.1", 8300);
    IoEventHandle eventHandle = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame f) throws Exception {
            System.out.println();
            System.out.println("____________________" + f.getStringContent());
            System.out.println();
            context.close();
        }
    };
    context.setIoEventHandle(eventHandle);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    Channel ch = context.connect(3000);
    LengthValueFrame frame = new LengthValueFrame();
    frame.setString("hello server!", ch);
    ch.writeAndFlush(frame);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) ChannelConnector(com.firenio.component.ChannelConnector) Channel(com.firenio.component.Channel) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Aggregations

Channel (com.firenio.component.Channel)19 ChannelConnector (com.firenio.component.ChannelConnector)19 Frame (com.firenio.component.Frame)18 IoEventHandle (com.firenio.component.IoEventHandle)18 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)17 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)11 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)8 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)5 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)5 Waiter (com.firenio.concurrent.Waiter)5 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)4 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)3 ProtobaseCodec (com.firenio.codec.protobase.ProtobaseCodec)3 ProtobaseFrame (com.firenio.codec.protobase.ProtobaseFrame)3 ByteBuf (com.firenio.buffer.ByteBuf)2 HttpFrame (com.firenio.codec.http11.HttpFrame)2 ChannelActiveListener (com.firenio.component.ChannelActiveListener)2 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2