Search in sources :

Example 1 with ProtobaseFutureImpl

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

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

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class RTPServerDPAcceptor method execute.

@Override
protected void execute(DatagramSession dSession, DatagramRequest request) {
    String serviceName = request.getFutureName();
    if (BIND_SESSION.equals(serviceName)) {
        Parameters parameters = request.getParameters();
        ApplicationContext context = ApplicationContext.getInstance();
        LoginCenter loginCenter = AuthorityContext.getInstance().getLoginCenter();
        if (!loginCenter.isValidate(parameters)) {
            return;
        }
        // FIXME udp
        SocketChannelContext channelContext = context.getChannelContext();
        SocketSessionManager sessionManager = channelContext.getSessionManager();
        // Session session = factory.getSession(username);
        SocketSession session = null;
        if (session == null) {
            return;
        }
        // session.setDatagramChannel(channel); //FIXME udp
        ProtobaseFuture future = new ProtobaseFutureImpl(session.getContext(), BIND_SESSION_CALLBACK);
        logger.debug("___________________bind___session___{}", session);
        future.write("1");
        session.flush(future);
    } else {
        logger.debug(">>>> {}", request.getFutureName());
    }
}
Also used : ApplicationContext(com.generallycloud.baseio.container.ApplicationContext) Parameters(com.generallycloud.baseio.component.Parameters) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) SocketSessionManager(com.generallycloud.baseio.component.SocketSessionManager) LoginCenter(com.generallycloud.baseio.container.LoginCenter) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext)

Example 4 with ProtobaseFutureImpl

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class ProtobaseProtocolDecoder method decode.

@Override
public ChannelFuture decode(SocketChannel channel, ByteBuf buffer) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    ByteBuf buf = allocator.allocate(2);
    return new ProtobaseFutureImpl(channel, buf);
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf)

Example 5 with ProtobaseFutureImpl

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class TestProtobufClient method main.

public static void main(String[] args) throws Exception {
    ProtobufUtil protobufUtil = new ProtobufUtil();
    protobufUtil.regist(SearchRequest.getDefaultInstance());
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            SearchRequest res = (SearchRequest) protobufUtil.getMessage(f);
            System.out.println();
            System.out.println("________" + res);
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SessionActiveSEListener());
    // context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    SocketSession session = connector.connect();
    ProtobaseFuture f = new ProtobaseFutureImpl(context);
    ByteString byteString = ByteString.copyFrom("222".getBytes());
    SearchRequest request = SearchRequest.newBuilder().setCorpus(Corpus.IMAGES).setPageNumber(100).setQuery("test").setQueryBytes(byteString).setResultPerPage(-1).build();
    protobufUtil.writeProtobuf(request, f);
    session.flush(f);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : SearchRequest(com.generallycloud.test.io.protobuf.TestProtoBufBean.SearchRequest) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ByteString(com.google.protobuf.ByteString) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ProtobufUtil(com.generallycloud.baseio.codec.protobuf.ProtobufUtil) 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) 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)

Aggregations

ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)12 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)10 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)8 SocketSession (com.generallycloud.baseio.component.SocketSession)8 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)7 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)7 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)7 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)7 Future (com.generallycloud.baseio.protocol.Future)7 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)6 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)6 JSONObject (com.alibaba.fastjson.JSONObject)1 BalanceClientSocketSession (com.generallycloud.baseio.balance.BalanceClientSocketSession)1 BalanceClientSocketSessionFactory (com.generallycloud.baseio.balance.BalanceClientSocketSessionFactory)1 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)1 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 ParamedProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)1 ParamedProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)1 ProtobaseBeatFutureFactory (com.generallycloud.baseio.codec.protobase.future.ProtobaseBeatFutureFactory)1 ProtobufUtil (com.generallycloud.baseio.codec.protobuf.ProtobufUtil)1