Search in sources :

Example 1 with LengthValueFrame

use of com.firenio.codec.lengthvalue.LengthValueFrame 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 LengthValueFrame

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

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

use of com.firenio.codec.lengthvalue.LengthValueFrame 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 5 with LengthValueFrame

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

the class TestLoadClient1 method run.

@Override
public void run() {
    int time1 = getTime();
    Channel ch = context.getChannel();
    try {
        for (int i = 0; i < time1; i++) {
            Frame frame = new LengthValueFrame();
            frame.setContent(ch.allocateWithSkipHeader(1));
            if (debug) {
                byte[] bb = new byte[4];
                ByteUtil.putInt(bb, i, 0);
                frame.write(bb);
            }
            frame.write(req);
            if (debug) {
                frame.write(String.valueOf(i).getBytes());
            }
            ch.writeAndFlush(frame);
            if (debug) {
                count.incrementAndGet();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame)

Aggregations

LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)9 Channel (com.firenio.component.Channel)9 Frame (com.firenio.component.Frame)9 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)8 IoEventHandle (com.firenio.component.IoEventHandle)8 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)8 ChannelConnector (com.firenio.component.ChannelConnector)7 Waiter (com.firenio.concurrent.Waiter)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)1 ChannelActiveListener (com.firenio.component.ChannelActiveListener)1 ChannelManagerListener (com.firenio.component.ChannelManagerListener)1 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)1 Logger (com.firenio.log.Logger)1 BigDecimal (java.math.BigDecimal)1 Scanner (java.util.Scanner)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1