Search in sources :

Example 6 with ProtobaseProtocolFactory

use of com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory 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 7 with ProtobaseProtocolFactory

use of com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory in project baseio by generallycloud.

the class TestSimpleWebSocketClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            if (future instanceof ClientHttpFuture) {
                ClientHttpFuture f = (ClientHttpFuture) future;
                if (f.getRequestHeader("Sec-WebSocket-Accept") != null) {
                    f.updateWebSocketProtocol();
                    WebSocketFuture f2 = new WebSocketFutureImpl(session.getContext());
                    f2.write("{action: \"add-user\", username: \"火星人\"}");
                    // f2.write("{\"action\":999}");
                    session.flush(f2);
                }
                System.out.println(f.getRequestHeaders());
            } else {
                WebSocketFuture f = (WebSocketFuture) future;
                System.out.println(f.getReadText());
            }
        }
    };
    ServerConfiguration configuration = new ServerConfiguration();
    configuration.setSERVER_HOST("47.89.30.77");
    // configuration.setSERVER_HOST("120.76.222.210");
    // configuration.setSERVER_HOST("115.29.193.48");
    // configuration.setSERVER_HOST("workerman.net");
    configuration.setSERVER_PORT(7680);
    // configuration.setSERVER_PORT(30005);
    // configuration.setSERVER_PORT(29000);
    // configuration.setSERVER_PORT(8280);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    connector.getContext().setBeatFutureFactory(new WebSocketBeatFutureFactory());
    connector.getContext().setProtocolFactory(new ClientHTTPProtocolFactory());
    connector.getContext().setSslContext(SSLUtil.initClient(true));
    SocketSession session = connector.connect();
    String url = "/web-socket-chat";
    url = "/c1020";
    HttpFuture future = new WebSocketUpgradeRequestFuture(session.getContext(), url);
    // future.setRequestURL("ws://120.76.222.210:30005/");
    // future.setResponseHeader("Host", "120.76.222.210:30005");
    // future.setResponseHeader("Pragma", "no-cache");
    // future.setResponseHeader("Cache-Control", "no-cache");
    // future.setResponseHeader("User-Agent",
    // "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
    // future.setResponseHeader("Accept-Encoding", "gzip, deflate, sdch");
    // future.setResponseHeader("Accept-Language", "zh-CN,zh;q=0.8");
    // future.setRequestHeader("", "");
    session.flush(future);
    // ThreadUtil.sleep(1000);
    // WebSocketReadFuture f2 = new WebSocketReadFutureImpl();
    // f2.write("test");
    // session.flush(f2);
    ThreadUtil.sleep(999999999);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) WebSocketFutureImpl(com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl) WebSocketUpgradeRequestFuture(com.generallycloud.baseio.codec.http11.future.WebSocketUpgradeRequestFuture) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) ClientHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) WebSocketUpgradeRequestFuture(com.generallycloud.baseio.codec.http11.future.WebSocketUpgradeRequestFuture) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) WebSocketBeatFutureFactory(com.generallycloud.baseio.codec.http11.future.WebSocketBeatFutureFactory)

Example 8 with ProtobaseProtocolFactory

use of com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory in project baseio by generallycloud.

the class TestListenerByteMessage method main.

public static void main(String[] args) throws Exception {
    SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(8300));
    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");
    MessageConsumer consumer = new DefaultMessageConsumer(session);
    final long old = System.currentTimeMillis();
    consumer.receive(new OnMessage() {

        @Override
        public void onReceive(Message message) {
            System.out.println(message);
            if (message.getMsgType() == Message.TYPE_TEXT_BYTE) {
                TextByteMessage _Message = (TextByteMessage) message;
                System.out.println(new String(_Message.getByteArray(), Encoding.UTF8));
            }
            System.out.println("Time:" + (System.currentTimeMillis() - old));
        }
    });
    ThreadUtil.sleep(30000000);
    connector.close();
}
Also used : TextByteMessage(com.generallycloud.baseio.container.jms.TextByteMessage) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) DefaultMessageConsumer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer) MessageConsumer(com.generallycloud.baseio.container.jms.client.MessageConsumer) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnMessage(com.generallycloud.baseio.container.jms.client.OnMessage) TextByteMessage(com.generallycloud.baseio.container.jms.TextByteMessage) Message(com.generallycloud.baseio.container.jms.Message) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) DefaultMessageConsumer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer) OnMessage(com.generallycloud.baseio.container.jms.client.OnMessage) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) 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)

Example 9 with ProtobaseProtocolFactory

use of com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory in project baseio by generallycloud.

the class TestListenerCallBack 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");
    MessageConsumer consumer = new DefaultMessageConsumer(session);
    consumer.receive(new OnMessage() {

        @Override
        public void onReceive(Message message) {
            System.out.println(message);
        }
    });
    ThreadUtil.sleep(1000);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) DefaultMessageConsumer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer) MessageConsumer(com.generallycloud.baseio.container.jms.client.MessageConsumer) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnMessage(com.generallycloud.baseio.container.jms.client.OnMessage) Message(com.generallycloud.baseio.container.jms.Message) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) DefaultMessageConsumer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer) OnMessage(com.generallycloud.baseio.container.jms.client.OnMessage) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) 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)

Example 10 with ProtobaseProtocolFactory

use of com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory in project baseio by generallycloud.

the class TestListenerPower 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");
    MessageConsumer consumer = new DefaultMessageConsumer(session);
    long old = System.currentTimeMillis();
    OnMessage onMessage = new OnMessage() {

        @Override
        public void onReceive(Message message) {
            System.out.println(message);
        }
    };
    for (int i = 0; i < 10000; i++) {
        consumer.receive(onMessage);
    }
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    connector.close();
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) DefaultMessageConsumer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer) MessageConsumer(com.generallycloud.baseio.container.jms.client.MessageConsumer) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnMessage(com.generallycloud.baseio.container.jms.client.OnMessage) Message(com.generallycloud.baseio.container.jms.Message) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) DefaultMessageConsumer(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer) OnMessage(com.generallycloud.baseio.container.jms.client.OnMessage) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) 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)

Aggregations

ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)28 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)28 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)27 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)27 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)26 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)25 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)15 SocketSession (com.generallycloud.baseio.component.SocketSession)15 FixedSession (com.generallycloud.baseio.container.FixedSession)15 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)15 Future (com.generallycloud.baseio.protocol.Future)15 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)12 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)6 MessageConsumer (com.generallycloud.baseio.container.jms.client.MessageConsumer)5 DefaultMessageConsumer (com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer)5 Message (com.generallycloud.baseio.container.jms.Message)4 OnMessage (com.generallycloud.baseio.container.jms.client.OnMessage)4 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)2 ProtobaseBeatFutureFactory (com.generallycloud.baseio.codec.protobase.future.ProtobaseBeatFutureFactory)2 ProtobufUtil (com.generallycloud.baseio.codec.protobuf.ProtobufUtil)2