Search in sources :

Example 1 with SocketChannelConnector

use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.

the class TestBalanceBroadcast method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            if (BalanceContext.BALANCE_CHANNEL_LOST.equals(f.getFutureName())) {
                System.out.println("客户端已下线:" + f.getReadText());
            } else {
                System.out.println("~~~~~~收到报文:" + future.toString());
                String res = "(***" + f.getReadText() + "***)";
                System.out.println("~~~~~~处理报文:" + res);
                f.write(res);
                session.flush(future);
            }
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(8800);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    SocketSession session = connector.connect();
    for (; session.isOpened(); ) {
        ProtobaseFuture future = new ProtobaseFutureImpl(context, "broadcast");
        future.setBroadcast(true);
        String msg = "broadcast msg___S:" + System.currentTimeMillis();
        future.write(msg);
        future.writeBinary("__^^^binary^^^__".getBytes());
        session.flush(future);
        ThreadUtil.sleep(10);
    }
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 2 with SocketChannelConnector

use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.

the class TestBalanceClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            if (f.hasReadBinary()) {
                System.out.println(f.getReadText() + new String(f.getReadBinary()) + "______R:" + System.currentTimeMillis());
            } else {
                System.out.println(f.getReadText() + "______R:" + System.currentTimeMillis());
            }
            res.incrementAndGet();
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(8600);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.setSocketSessionFactory(new BalanceClientSocketSessionFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    BalanceClientSocketSession session = (BalanceClientSocketSession) connector.connect();
    for (int i = 0; i < 100; i++) {
        int fid = Math.abs(new Random().nextInt());
        ProtobaseFuture future = new ProtobaseFutureImpl(context, "future-name");
        future.write("你好!");
        future.setHashCode(fid);
        session.flush(future);
    }
    ThreadUtil.sleep(300);
    System.out.println("==========" + res.get());
    ThreadUtil.sleep(500000000);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) BalanceClientSocketSession(com.generallycloud.baseio.balance.BalanceClientSocketSession) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketSession(com.generallycloud.baseio.component.SocketSession) BalanceClientSocketSession(com.generallycloud.baseio.balance.BalanceClientSocketSession) BalanceClientSocketSessionFactory(com.generallycloud.baseio.balance.BalanceClientSocketSessionFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future)

Example 3 with SocketChannelConnector

use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.

the class TestBalanceLoad method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            if (BalanceContext.BALANCE_CHANNEL_LOST.equals(f.getFutureName())) {
                System.out.println("客户端已下线:" + f.getReadText());
            } else {
                System.out.println("收到报文:" + future.toString());
                String res = "_____________" + f.getReadText();
                System.out.println("处理报文:" + res);
                f.write(res);
                session.flush(future);
            }
        }
    };
    ServerConfiguration configuration = new ServerConfiguration(8800);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    connector.connect();
    System.in.read();
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 4 with SocketChannelConnector

use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.

the class TestTellerPower method main.

public static void main(String[] args) throws Exception {
    LoggerFactory.configure();
    SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
    ServerConfiguration configuration = new ServerConfiguration(8300);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandle);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    FixedSession session = new FixedSession(connector.connect());
    session.login("admin", "admin100");
    MessageProducer producer = new DefaultMessageProducer(session);
    TextMessage message = new TextMessage("msgId", "qName", "你好!");
    long old = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
        producer.offer(message);
    }
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    connector.close();
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) DefaultMessageProducer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageProducer) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) DefaultMessageProducer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageProducer) MessageProducer(com.generallycloud.baseio.container.jms.client.MessageProducer) TextMessage(com.generallycloud.baseio.container.jms.TextMessage)

Example 5 with SocketChannelConnector

use of com.generallycloud.baseio.connector.SocketChannelConnector 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)

Aggregations

LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)41 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)41 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)41 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)40 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)40 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)25 SocketSession (com.generallycloud.baseio.component.SocketSession)23 FixedSession (com.generallycloud.baseio.container.FixedSession)21 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)21 Future (com.generallycloud.baseio.protocol.Future)21 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)17 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)15 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)7 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)6 MessageConsumer (com.generallycloud.baseio.container.jms.client.MessageConsumer)6 DefaultMessageConsumer (com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer)6 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)5 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)5 ParamedProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)5 Message (com.generallycloud.baseio.container.jms.Message)5