Search in sources :

Example 16 with Future

use of com.generallycloud.baseio.protocol.Future 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 17 with Future

use of com.generallycloud.baseio.protocol.Future 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 18 with Future

use of com.generallycloud.baseio.protocol.Future 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 19 with Future

use of com.generallycloud.baseio.protocol.Future 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)

Example 20 with Future

use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.

the class TestFIxedLengthServer method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            future.write("yes server already accept your message:");
            future.write(future.getReadText());
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SocketSessionAliveSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    File certificate = FileUtil.readFileByCls("generallycloud.com.crt");
    File privateKey = FileUtil.readFileByCls("generallycloud.com.key");
    SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
    context.setSslContext(sslContext);
    acceptor.bind();
}
Also used : FLBeatFutureFactory(com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) File(java.io.File) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Aggregations

Future (com.generallycloud.baseio.protocol.Future)36 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)33 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)33 SocketSession (com.generallycloud.baseio.component.SocketSession)33 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)33 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)31 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)29 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)21 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)15 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)12 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)12 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)11 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)8 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)7 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)4 FixedSession (com.generallycloud.baseio.container.FixedSession)4 OnFuture (com.generallycloud.baseio.container.OnFuture)4 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)4