Search in sources :

Example 16 with IoEventHandle

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

the class TestLengthValueBroadcastServer method main.

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

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            LengthValueFrame f = (LengthValueFrame) frame;
            frame.write("yes server already accept your message:", ch);
            frame.write(f.getStringContent(), ch);
            ch.writeAndFlush(f);
        }
    };
    ChannelAcceptor context = new ChannelAcceptor(8300);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new SetOptionListener());
    context.addChannelEventListener(channelManagerListener);
    context.setIoEventHandle(eventHandleAdaptor);
    context.addProtocolCodec(new LengthValueCodec());
    context.bind();
    Util.exec(() -> {
        for (; ; ) {
            Util.sleep(1000);
            LengthValueFrame frame = new LengthValueFrame();
            frame.setContent("broadcast msg ........".getBytes());
            try {
                channelManagerListener.broadcast(frame);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) ChannelManagerListener(com.firenio.component.ChannelManagerListener) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) ChannelAcceptor(com.firenio.component.ChannelAcceptor) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 17 with IoEventHandle

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

the class TestLengthValueClient1 method main.

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

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            LengthValueFrame f = (LengthValueFrame) frame;
            System.out.println();
            System.out.println("____________________" + f.getStringContent());
            System.out.println();
        }
    };
    ChannelConnector context = new ChannelConnector(8300);
    context.setIoEventHandle(eventHandleAdaptor);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    Channel ch = context.connect();
    StringBuilder sb = new StringBuilder(1024 * 6);
    for (int i = 0; i < 1; i++) {
        sb.append("hello!");
    }
    for (int i = 0; i < 20; i++) {
        LengthValueFrame frame = new LengthValueFrame();
        frame.write(sb.toString(), ch);
        ch.writeAndFlush(frame);
    }
    Util.sleep(100);
    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) Channel(com.firenio.component.Channel) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) ChannelConnector(com.firenio.component.ChannelConnector) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 18 with IoEventHandle

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

the class TestHttpLoadServer method main.

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

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            HttpFrame f = (HttpFrame) frame;
            f.setConnection(HttpConnection.KEEP_ALIVE);
            f.setContentType(HttpContentType.text_plain);
            f.setString("Hello World", ch);
            ch.writeAndFlush(f);
            ch.release(f);
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setMemoryCapacity(1024 * 1024 * 64);
    group.setMemoryUnit(512);
    group.setEventLoopSize(2);
    ChannelAcceptor context = new ChannelAcceptor(group, 8080);
    context.addProtocolCodec(new HttpCodec(8));
    context.addProtocolCodec(new WebSocketCodec());
    context.setIoEventHandle(eventHandle);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) HttpFrame(com.firenio.codec.http11.HttpFrame) HttpCodec(com.firenio.codec.http11.HttpCodec) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) HttpFrame(com.firenio.codec.http11.HttpFrame) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 19 with IoEventHandle

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

the class TestProtobase method testText.

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

        @Override
        public void accept(Channel ch, Frame frame) {
            String text = frame.getStringContent();
            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.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 20 with IoEventHandle

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

the class TestProtobaseHeartBeat 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 ProtobaseCodec());
    context.setIoEventHandle(eventHandleAdaptor);
    Channel ch = context.connect();
    String param = "tttt";
    long old = Util.now_f();
    for (int i = 0; i < 5; i++) {
        Frame f = new ProtobaseFrame();
        f.setString(param, ch);
        ch.writeAndFlush(f);
        Util.sleep(300);
    }
    System.out.println("Time:" + (Util.past(old)));
    Thread.sleep(2000);
    Util.close(context);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) ProtobaseFrame(com.firenio.codec.protobase.ProtobaseFrame) Frame(com.firenio.component.Frame) ChannelActiveListener(com.firenio.component.ChannelActiveListener) ProtobaseFrame(com.firenio.codec.protobase.ProtobaseFrame) Channel(com.firenio.component.Channel) ChannelConnector(com.firenio.component.ChannelConnector) ProtobaseCodec(com.firenio.codec.protobase.ProtobaseCodec) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Aggregations

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