Search in sources :

Example 1 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl 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();
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
        // latch.countDown();
        // long count = latch.getCount();
        // if (count % 10 == 0) {
        // if (count < 50) {
        // logger.info("************************================" + count);
        // }
        // }
        // logger.info("res==========={}",res.getAndIncrement());
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(8300);
    // SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelContext context = new AioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    connector.getContext().setProtocolFactory(new FixedLengthProtocolFactory());
    connector.getContext().getServerConfiguration().setSERVER_CORE_SIZE(1);
    SocketSession session = connector.connect();
    System.out.println("################## Test start ####################");
    long old = System.currentTimeMillis();
    for (int i = 0; i < time; i++) {
        FixedLengthFuture future = new FixedLengthFutureImpl(session.getContext());
        future.write("hello server!");
        session.flush(future);
    }
    latch.await();
    long spend = (System.currentTimeMillis() - 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);
    CloseUtil.close(connector);
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) Logger(com.generallycloud.baseio.log.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) BigDecimal(java.math.BigDecimal) AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)

Example 2 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl in project baseio by generallycloud.

the class TestLoadClient1 method run.

@Override
public void run() {
    int time1 = getTime();
    SocketSession session = connector.getSession();
    for (int i = 0; i < time1; i++) {
        FixedLengthFuture future = new FixedLengthFutureImpl(session.getContext());
        future.write("hello server!");
        session.flush(future);
    }
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession)

Example 3 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl in project baseio by generallycloud.

the class SimpleTestFIxedLengthClientPush method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println(">msg from server: " + future.getReadText());
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    SocketSession session = connector.connect();
    ThreadUtil.execute(new Runnable() {

        @Override
        public void run() {
            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)) {
                    CloseUtil.close(session);
                    break;
                }
                FixedLengthFuture future = new FixedLengthFutureImpl(context);
                future.write(line);
                session.flush(future);
            }
        }
    });
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) Scanner(java.util.Scanner) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)

Example 4 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl in project baseio by generallycloud.

the class SimpleTestFIxedLengthServerPush method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            SocketSessionManager sessionManager = session.getContext().getSessionManager();
            Map<Integer, SocketSession> sessions = sessionManager.getManagedSessions();
            String msg = future.getReadText();
            String[] arr = msg.split(" ");
            String cmd = arr[0];
            logger.info("msg received: {}", msg);
            if ("list".equals(cmd)) {
                String keys = sessions.keySet().toString();
                future.write(keys);
            } else if ("id".equals(cmd)) {
                future.write(String.valueOf(session.getSessionId()));
            } else if ("push".equals(cmd)) {
                Integer id = Integer.valueOf(arr[1]);
                SocketSession target = sessions.get(id);
                if (target == null) {
                    future.write("offline id: " + id);
                } else {
                    future.write("from [");
                    future.write(String.valueOf(session.getSessionId()));
                    future.write("] push msg>");
                    future.write(arr[2]);
                    target.flush(future);
                    return;
                }
            } else if ("broadcast".equals(cmd)) {
                future.write("from [");
                future.write(String.valueOf(session.getSessionId()));
                future.write("] broadcast msg>");
                future.write(arr[1]);
                sessionManager.broadcast(future);
                return;
            } else {
                future.write("no cmd: " + cmd);
            }
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.addSessionEventListener(new SocketSessionEventListener() {

        @Override
        public void sessionOpened(SocketSession session) throws Exception {
        }

        @Override
        public void sessionClosed(SocketSession session) {
            SocketSessionManager sessionManager = session.getContext().getSessionManager();
            FixedLengthFuture future = new FixedLengthFutureImpl(session.getContext());
            future.write("client left: " + session.getSessionId());
            try {
                sessionManager.broadcast(future);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    });
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    acceptor.bind();
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSessionManager(com.generallycloud.baseio.component.SocketSessionManager) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSessionEventListener(com.generallycloud.baseio.component.SocketSessionEventListener) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Example 5 with FixedLengthFutureImpl

use of com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl in project baseio by generallycloud.

the class TestFIxedLengthClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SslContext sslContext = SSLUtil.initClient(true);
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SessionActiveSEListener());
    context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    context.setSslContext(sslContext);
    SocketSession session = connector.connect();
    FixedLengthFuture future = new FixedLengthFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
    DebugUtil.debug("连接已关闭。。。");
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) FLBeatFutureFactory(com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Aggregations

FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)8 SocketSession (com.generallycloud.baseio.component.SocketSession)8 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)7 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)7 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)7 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)7 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)7 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)7 Future (com.generallycloud.baseio.protocol.Future)7 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)6 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)5 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)2 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)2 IOException (java.io.IOException)2 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)1 AioSocketChannelContext (com.generallycloud.baseio.component.AioSocketChannelContext)1 SocketSessionActiveSEListener (com.generallycloud.baseio.component.SocketSessionActiveSEListener)1 SocketSessionEventListener (com.generallycloud.baseio.component.SocketSessionEventListener)1 SocketSessionManager (com.generallycloud.baseio.component.SocketSessionManager)1 SslContext (com.generallycloud.baseio.component.ssl.SslContext)1