use of com.generallycloud.baseio.codec.protobuf.ProtobufUtil 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);
}
use of com.generallycloud.baseio.codec.protobuf.ProtobufUtil in project baseio by generallycloud.
the class TestProtobufServer 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 req = (SearchRequest) protobufUtil.getMessage(f);
String message = "yes server already accept your message:\n" + req;
System.out.println(message);
SearchRequest res = SearchRequest.newBuilder().mergeFrom(req).setQuery("query_______").build();
protobufUtil.writeProtobuf(res.getClass().getName(), res, f);
session.flush(future);
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
context.addSessionEventListener(new LoggerSocketSEListener());
// context.addSessionEventListener(new SessionAliveSEListener());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
// context.setBeatFutureFactory(new NIOBeatFutureFactory());
context.setProtocolFactory(new ProtobaseProtocolFactory());
acceptor.bind();
}
Aggregations