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);
}
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);
}
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());
}
}
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);
}
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);
}
Aggregations