Search in sources :

Example 1 with LengthValueCodec

use of com.firenio.codec.lengthvalue.LengthValueCodec in project baseio by generallycloud.

the class TestLengthValueClientPush method main.

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

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            System.out.println(">msg from server: " + frame);
        }
    };
    ChannelConnector context = new ChannelConnector(8300);
    context.setIoEventHandle(eventHandleAdaptor);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    Channel ch = context.connect();
    Util.exec(() -> {
        System.out.println("************************************************");
        System.out.println("提示:");
        System.out.println("list(获取所有客户端id)");
        System.out.println("id(获取当前客户端id)");
        System.out.println("push id msg(推送消息到)");
        System.out.println("broadcast msg(广播消息)");
        System.out.println("exit(退出客户端)");
        System.out.println("仅用于演示,msg请勿包含空格");
        System.out.println("************************************************");
        Scanner scanner = new Scanner(System.in);
        for (; ; ) {
            System.out.println(">");
            String line = scanner.nextLine();
            if ("exit".equals(line)) {
                Util.close(ch);
                break;
            }
            LengthValueFrame frame = new LengthValueFrame();
            frame.write(line, ch);
            try {
                ch.writeAndFlush(frame);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) Scanner(java.util.Scanner) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelConnector(com.firenio.component.ChannelConnector) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Example 2 with LengthValueCodec

use of com.firenio.codec.lengthvalue.LengthValueCodec in project baseio by generallycloud.

the class TestLengthValueServerJunit method server.

@Before
public void server() throws Exception {
    IoEventHandle eventHandle = 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);
        }
    };
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setIoEventHandle(eventHandle);
    context.addProtocolCodec(new LengthValueCodec());
    context.addChannelEventListener(new ChannelEventListenerAdapter() {

        @Override
        public void channelOpened(Channel ch) throws Exception {
            System.out.println(ch.getOption(SocketOptions.TCP_NODELAY));
            System.out.println(ch.getOption(SocketOptions.SO_RCVBUF));
            ch.setOption(SocketOptions.TCP_NODELAY, 1);
            ch.setOption(SocketOptions.SO_RCVBUF, 1028);
            System.out.println(ch.getOption(SocketOptions.TCP_NODELAY));
            System.out.println(ch.getOption(SocketOptions.SO_RCVBUF));
        }
    });
    context.bind();
}
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) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) Before(org.junit.Before)

Example 3 with LengthValueCodec

use of com.firenio.codec.lengthvalue.LengthValueCodec in project baseio by generallycloud.

the class TestLoadClient1 method prepare.

@Override
public void prepare() throws Exception {
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) {
            addCount(80000);
            if (debug) {
                count.decrementAndGet();
            }
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup();
    group.setMemoryCapacity(5120000 * 256 * CLIENT_CORE_SIZE);
    group.setMemoryUnit(256);
    group.setWriteBuffers(TestLoadServer.WRITE_BUFFERS);
    group.setEnableMemoryPool(TestLoadServer.ENABLE_POOL);
    context = new ChannelConnector(group, "127.0.0.1", 8300);
    context.setIoEventHandle(eventHandleAdaptor);
    if (TestLoadServer.ENABLE_SSL) {
        context.setSslContext(SslContextBuilder.forClient(true).build());
    }
    context.setPrintConfig(false);
    context.addProtocolCodec(new LengthValueCodec());
    if (TestLoadServer.ENABLE_WORK_EVENT_LOOP) {
        context.setExecutorGroup(new ThreadEventLoopGroup("ep", 1024 * 256));
    }
    context.connect(6000);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelConnector(com.firenio.component.ChannelConnector) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup)

Example 4 with LengthValueCodec

use of com.firenio.codec.lengthvalue.LengthValueCodec in project baseio by generallycloud.

the class TestLoadClient method main.

public static void main(String[] args) throws Exception {
    final Logger logger = LoggerFactory.getLogger(TestLoadClient.class);
    final CountDownLatch latch = new CountDownLatch(time);
    final AtomicInteger res = new AtomicInteger();
    final AtomicInteger req = new AtomicInteger();
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
        // latch.countDown();
        // long count = latch.getCount();
        // if (count % 10 == 0) {
        // if (count < 50) {
        // logger.info("************************================" + count);
        // }
        // }
        // logger.info("res==========={}",res.getAndIncrement());
        }
    };
    ChannelConnector context = new ChannelConnector(8300);
    context.setIoEventHandle(eventHandleAdaptor);
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addProtocolCodec(new LengthValueCodec());
    Channel ch = context.connect();
    System.out.println("################## Test start ####################");
    long old = Util.now();
    for (int i = 0; i < time; i++) {
        LengthValueFrame frame = new LengthValueFrame();
        frame.write("hello server!", ch);
        ch.writeAndFlush(frame);
    }
    latch.await();
    long spend = (Util.past(old));
    System.out.println("## Execute Time:" + time);
    System.out.println("## OP/S:" + new BigDecimal(time * 1000).divide(new BigDecimal(spend), 2, BigDecimal.ROUND_HALF_UP));
    System.out.println("## Expend Time:" + spend);
    Util.close(context);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Logger(com.firenio.log.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) BigDecimal(java.math.BigDecimal) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelConnector(com.firenio.component.ChannelConnector)

Example 5 with LengthValueCodec

use of com.firenio.codec.lengthvalue.LengthValueCodec in project baseio by generallycloud.

the class TestLoadServer method main.

public static void main(String[] args) throws Exception {
    Options.setBufAutoExpansion(AUTO_EXPANSION);
    Options.setEnableEpoll(ENABLE_EPOLL);
    Options.setEnableUnsafeBuf(ENABLE_UNSAFE_BUF);
    IoEventHandle eventHandle = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame f) throws Exception {
            String text = f.getStringContent();
            if (BUFFERED_WRITE) {
                ByteBuf buf = ch.getAttribute(WRITE_BUF);
                if (buf == null) {
                    buf = ch.allocate();
                    ByteBuf temp = buf;
                    ch.setAttribute(WRITE_BUF, buf);
                    ch.getEventLoop().submit(() -> {
                        ch.writeAndFlush(temp);
                        ch.setAttribute(WRITE_BUF, null);
                    });
                }
                byte[] data = text.getBytes(ch.getCharset());
                buf.writeInt(data.length);
                buf.writeBytes(data);
            } else {
                f.setString(text, ch);
                ch.writeAndFlush(f);
            }
        }
    };
    NioEventLoopGroup group = new NioEventLoopGroup(SERVER_CORE_SIZE);
    group.setMemoryCapacity(1024 * 512 * MEM_UNIT * SERVER_CORE_SIZE);
    group.setWriteBuffers(WRITE_BUFFERS);
    group.setMemoryUnit(MEM_UNIT);
    group.setEnableMemoryPool(ENABLE_POOL);
    ChannelAcceptor context = new ChannelAcceptor(group, 8300);
    context.addProtocolCodec(new LengthValueCodec());
    context.setIoEventHandle(eventHandle);
    if (ENABLE_SSL) {
    // context.setSslPem("localhost.key;localhost.crt");
    }
    context.addChannelEventListener(new LoggerChannelOpenListener());
    if (ENABLE_WORK_EVENT_LOOP) {
        context.setExecutorGroup(new ThreadEventLoopGroup("ep", 1024 * 256 * CLIENT_CORE_SIZE));
    }
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ByteBuf(com.firenio.buffer.ByteBuf) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Aggregations

LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)15 Channel (com.firenio.component.Channel)15 Frame (com.firenio.component.Frame)14 IoEventHandle (com.firenio.component.IoEventHandle)14 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)14 ChannelConnector (com.firenio.component.ChannelConnector)11 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)10 ChannelAcceptor (com.firenio.component.ChannelAcceptor)3 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)3 ByteBuf (com.firenio.buffer.ByteBuf)2 ThreadEventLoopGroup (com.firenio.concurrent.ThreadEventLoopGroup)2 Waiter (com.firenio.concurrent.Waiter)2 ChannelActiveListener (com.firenio.component.ChannelActiveListener)1 ChannelEventListenerAdapter (com.firenio.component.ChannelEventListenerAdapter)1 ChannelManagerListener (com.firenio.component.ChannelManagerListener)1 ReConnector (com.firenio.component.ReConnector)1 Logger (com.firenio.log.Logger)1 BigDecimal (java.math.BigDecimal)1 Scanner (java.util.Scanner)1 CountDownLatch (java.util.concurrent.CountDownLatch)1