Search in sources :

Example 6 with SocketChannelConnector

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

the class TestHttpLoadClient method prepare.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            addCount(1000);
        }
    };
    ServerConfiguration c = new ServerConfiguration("localhost", 80);
    c.setSERVER_MEMORY_POOL_CAPACITY(1280000);
    c.setSERVER_MEMORY_POOL_UNIT(128);
    c.setSERVER_CORE_SIZE(1);
    c.setSERVER_HOST("192.168.0.180");
    SocketChannelContext context = new NioSocketChannelContext(c);
    connector = new SocketChannelConnector(context);
    context.setProtocolFactory(new ClientHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    session = connector.connect();
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) ClientHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 7 with SocketChannelConnector

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

the class TestSimpleHttpClient2 method main.

public static void main(String[] args) throws IOException {
    String host = "www.baidu.com";
    host = "generallycloud.com";
    host = "127.0.0.1";
    int port = 443;
    port = 8080;
    HttpIOEventHandle eventHandleAdaptor = new HttpIOEventHandle();
    ServerConfiguration c = new ServerConfiguration(host, port);
    SocketChannelContext context = new NioSocketChannelContext(c);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setProtocolFactory(new ClientHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    if (port == 443) {
        context.setSslContext(SSLUtil.initClient(true));
    }
    SocketSession session = connector.connect();
    HttpClient client = new HttpClient(session);
    HttpFuture future = new ClientHttpFuture(context, "/");
    HttpFuture res = client.request(future, 99990000);
    System.out.println();
    System.out.println(new String(res.getBodyContent()));
    System.out.println();
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) 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) HttpIOEventHandle(com.generallycloud.baseio.codec.http11.HttpIOEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) HttpClient(com.generallycloud.baseio.codec.http11.HttpClient)

Example 8 with SocketChannelConnector

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

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

the class TestBrowser method main.

public static void main(String[] args) throws Exception {
    String queueName = "qName";
    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 ParamedProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    FixedSession session = new FixedSession(connector.connect());
    session.login("admin", "admin100");
    MessageBrowser browser = new DefaultMessageBrowser(session);
    Message message = browser.browser(queueName);
    System.out.println("message:" + message);
    int size = browser.size();
    System.out.println("size:" + size);
    boolean isOnline = browser.isOnline(queueName);
    System.out.println("isOnline:" + isOnline);
    connector.close();
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) Message(com.generallycloud.baseio.container.jms.Message) 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) DefaultMessageBrowser(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageBrowser) MessageBrowser(com.generallycloud.baseio.container.jms.client.MessageBrowser) DefaultMessageBrowser(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageBrowser) ParamedProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)

Example 10 with SocketChannelConnector

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

the class TestListener method main.

public static void main(String[] args) throws Exception {
    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 ParamedProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    FixedSession session = new FixedSession(connector.connect());
    session.login("admin", "admin100");
    MessageConsumer consumer = new DefaultMessageConsumer(session);
    long old = System.currentTimeMillis();
    consumer.receive(new OnMessage() {

        @Override
        public void onReceive(Message message) {
            System.out.println(message);
        }
    });
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    ThreadUtil.sleep(1500000);
    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) ParamedProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)

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