Search in sources :

Example 1 with ProtobaseCodec

use of com.firenio.codec.protobase.ProtobaseCodec 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 2 with ProtobaseCodec

use of com.firenio.codec.protobase.ProtobaseCodec 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 3 with ProtobaseCodec

use of com.firenio.codec.protobase.ProtobaseCodec 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 4 with ProtobaseCodec

use of com.firenio.codec.protobase.ProtobaseCodec 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)

Example 5 with ProtobaseCodec

use of com.firenio.codec.protobase.ProtobaseCodec in project baseio by generallycloud.

the class TestProtobaseServer 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 ProtobaseCodec());
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ProtobaseCodec(com.firenio.codec.protobase.ProtobaseCodec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Aggregations

ProtobaseCodec (com.firenio.codec.protobase.ProtobaseCodec)5 Channel (com.firenio.component.Channel)5 Frame (com.firenio.component.Frame)5 IoEventHandle (com.firenio.component.IoEventHandle)5 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)5 ProtobaseFrame (com.firenio.codec.protobase.ProtobaseFrame)4 ChannelConnector (com.firenio.component.ChannelConnector)3 Waiter (com.firenio.concurrent.Waiter)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)1 ChannelActiveListener (com.firenio.component.ChannelActiveListener)1 ChannelAliveListener (com.firenio.component.ChannelAliveListener)1 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)1 Before (org.junit.Before)1