Search in sources :

Example 1 with Waiter

use of com.firenio.concurrent.Waiter in project baseio by generallycloud.

the class TestSimpleHttpClient method main.

@Test
public void main() throws Exception {
    Options.setEnableEpoll(true);
    Waiter<String> w = new Waiter<>();
    ChannelConnector context = new ChannelConnector("firenio.com", 443);
    SslContext sslContext = SslContextBuilder.forClient(true).build();
    context.addProtocolCodec(new ClientHttpCodec());
    context.addProtocolCodec(new WebSocketCodec());
    context.setIoEventHandle(new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) {
            ClientHttpFrame res = (ClientHttpFrame) frame;
            System.out.println();
            System.out.println(new String(res.getBytesContent()));
            System.out.println();
            Util.close(context);
            w.call(new String(res.getBytesContent()), null);
        }
    });
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setSslContext(sslContext);
    long start = Util.now();
    Channel ch = context.connect(3000);
    ch.writeAndFlush(new ClientHttpFrame("/test?p=2222"));
    w.await(3000);
    System.out.println(Util.past(start));
    Assert.assertEquals("yes server already accept your message :) {p=2222}", w.getResponse());
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) Channel(com.firenio.component.Channel) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) Waiter(com.firenio.concurrent.Waiter) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) SslContext(com.firenio.component.SslContext) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) Test(org.junit.Test)

Example 2 with Waiter

use of com.firenio.concurrent.Waiter 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 3 with Waiter

use of com.firenio.concurrent.Waiter in project baseio by generallycloud.

the class TestLengthValueServerJunit method testClientAsync.

public void testClientAsync() throws Exception {
    Waiter<String> w = new Waiter<>();
    ChannelConnector context = new ChannelConnector(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();
            w.call(f.getStringContent(), null);
        }
    };
    context.setIoEventHandle(eventHandle);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    context.connect((ch, ex) -> {
        LengthValueFrame f = new LengthValueFrame();
        f.setString(hello, ch);
        try {
            ch.writeAndFlush(f);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    w.await(1000);
    v(w.getResponse());
}
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) Waiter(com.firenio.concurrent.Waiter) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 4 with Waiter

use of com.firenio.concurrent.Waiter in project baseio by generallycloud.

the class TestLengthValueServerJunit method testClient.

public void testClient() throws Exception {
    Waiter<String> w = new Waiter<>();
    ChannelConnector context = new ChannelConnector(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();
            w.call(f.getStringContent(), null);
        }
    };
    context.setIoEventHandle(eventHandle);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    Channel ch = context.connect();
    LengthValueFrame f = new LengthValueFrame();
    f.setString(hello, ch);
    ch.writeAndFlush(f);
    w.await(1000);
    v(w.getResponse());
}
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) Waiter(com.firenio.concurrent.Waiter) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 5 with Waiter

use of com.firenio.concurrent.Waiter 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)

Aggregations

Channel (com.firenio.component.Channel)5 ChannelConnector (com.firenio.component.ChannelConnector)5 Frame (com.firenio.component.Frame)5 IoEventHandle (com.firenio.component.IoEventHandle)5 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)5 Waiter (com.firenio.concurrent.Waiter)5 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)2 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)2 ProtobaseCodec (com.firenio.codec.protobase.ProtobaseCodec)2 ProtobaseFrame (com.firenio.codec.protobase.ProtobaseFrame)2 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)1 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)1 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)1 SslContext (com.firenio.component.SslContext)1 Test (org.junit.Test)1