Search in sources :

Example 1 with OnFuture

use of com.generallycloud.baseio.container.OnFuture in project baseio by generallycloud.

the class TestSessionDisconnect method main.

public static void main(String[] args) throws Exception {
    String serviceName = "TestSessionDisconnectServlet";
    String param = "ttt";
    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");
    ProtobaseFuture future = session.request(serviceName, param);
    System.out.println(future.getReadText());
    session.listen(serviceName, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            ProtobaseFuture f = (ProtobaseFuture) future;
            System.out.println(f.getReadText());
        }
    });
    session.write(serviceName, param);
    ThreadUtil.sleep(9999);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnFuture(com.generallycloud.baseio.container.OnFuture) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) 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) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) OnFuture(com.generallycloud.baseio.container.OnFuture)

Example 2 with OnFuture

use of com.generallycloud.baseio.container.OnFuture in project baseio by generallycloud.

the class RTPClient method bindTCPSession.

public void bindTCPSession() throws IOException {
    if (connector == null) {
        throw new IllegalArgumentException("null udp connector");
    }
    Authority authority = session.getAuthority();
    if (authority == null) {
        throw new IllegalArgumentException("not login");
    }
    JSONObject json = new JSONObject();
    json.put("serviceName", RTPServerDPAcceptor.BIND_SESSION);
    json.put("username", authority.getUsername());
    json.put("password", authority.getPassword());
    final DatagramPacket packet = DatagramPacket.createSendPacket(json.toJSONString().getBytes(context.getEncoding()));
    final String BIND_SESSION_CALLBACK = RTPServerDPAcceptor.BIND_SESSION_CALLBACK;
    final Waiter<Integer> waiter = new Waiter<>();
    session.listen(BIND_SESSION_CALLBACK, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            waiter.setPayload(0);
        }
    });
    final byte[] shortWaiter = new byte[] {};
    ThreadUtil.execute(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                try {
                    connector.sendDatagramPacket(packet);
                } catch (IOException e) {
                    DebugUtil.debug(e);
                }
                if (waiter.isDnoe()) {
                    break;
                }
                synchronized (shortWaiter) {
                    try {
                        shortWaiter.wait(300);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }
    });
    if (waiter.await(3000)) {
        CloseUtil.close(connector);
        throw new ClosedChannelException("disconnected");
    }
}
Also used : ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) Authority(com.generallycloud.baseio.container.authority.Authority) OnFuture(com.generallycloud.baseio.container.OnFuture) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) DatagramPacket(com.generallycloud.baseio.protocol.DatagramPacket) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) OnFuture(com.generallycloud.baseio.container.OnFuture) Waiter(com.generallycloud.baseio.concurrent.Waiter)

Example 3 with OnFuture

use of com.generallycloud.baseio.container.OnFuture in project baseio by generallycloud.

the class TestDownload method main.

public static void main(String[] args) throws Exception {
    String serviceName = "TestDownloadServlet";
    String fileName = "upload-flashmail-2.4.exe";
    JSONObject j = new JSONObject();
    j.put(FileReceiveUtil.FILE_NAME, fileName);
    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());
    connector.getContext().setProtocolFactory(new ProtobaseProtocolFactory());
    FixedSession session = new FixedSession(connector.connect());
    final FileReceiveUtil fileReceiveUtil = new FileReceiveUtil("download-");
    session.listen(serviceName, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            try {
                fileReceiveUtil.accept(session, (ParamedProtobaseFuture) future, false);
            } catch (Exception e) {
                DebugUtil.debug(e);
            }
        }
    });
    long old = System.currentTimeMillis();
    session.write(serviceName, j.toJSONString());
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    ThreadUtil.sleep(5000);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnFuture(com.generallycloud.baseio.container.OnFuture) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) 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) JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) FileReceiveUtil(com.generallycloud.baseio.container.FileReceiveUtil) Future(com.generallycloud.baseio.protocol.Future) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture) OnFuture(com.generallycloud.baseio.container.OnFuture) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)

Example 4 with OnFuture

use of com.generallycloud.baseio.container.OnFuture in project baseio by generallycloud.

the class TestListenSimple method main.

public static void main(String[] args) throws Exception {
    String serviceKey = "TestListenSimpleServlet";
    String param = "ttt";
    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());
    ProtobaseFuture future = session.request(serviceKey, param);
    System.out.println(future.getReadText());
    session.listen(serviceKey, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            ProtobaseFuture f = (ProtobaseFuture) future;
            System.out.println(f.getReadText());
        }
    });
    session.write(serviceKey, param);
    ThreadUtil.sleep(1000);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnFuture(com.generallycloud.baseio.container.OnFuture) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) 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) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) OnFuture(com.generallycloud.baseio.container.OnFuture)

Aggregations

SocketSession (com.generallycloud.baseio.component.SocketSession)4 OnFuture (com.generallycloud.baseio.container.OnFuture)4 Future (com.generallycloud.baseio.protocol.Future)4 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)3 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)3 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)3 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)3 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)3 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)3 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)3 FixedSession (com.generallycloud.baseio.container.FixedSession)3 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ClosedChannelException (com.generallycloud.baseio.ClosedChannelException)1 ParamedProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)1 Waiter (com.generallycloud.baseio.concurrent.Waiter)1 FileReceiveUtil (com.generallycloud.baseio.container.FileReceiveUtil)1 Authority (com.generallycloud.baseio.container.authority.Authority)1 DatagramPacket (com.generallycloud.baseio.protocol.DatagramPacket)1 IOException (java.io.IOException)1