use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.
the class TestLineBasedBroadcastServer method main.
public static void main(String[] args) throws Exception {
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
long old = System.currentTimeMillis();
String res = "hello world!";
future.write(res);
ChannelAcceptor acceptor = (ChannelAcceptor) session.getContext().getChannelService();
acceptor.broadcast(future);
long now = System.currentTimeMillis();
System.out.println("广播花费时间:" + (now - old) + ",连接数:" + session.getContext().getSessionManager().getManagedSessionSize());
}
};
ServerConfiguration configuration = new ServerConfiguration();
configuration.setSERVER_PORT(18300);
configuration.setSERVER_SESSION_IDLE_TIME(180000);
configuration.setSERVER_MEMORY_POOL_CAPACITY(1024 * 512);
configuration.setSERVER_MEMORY_POOL_UNIT(64);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.setProtocolFactory(new CharBasedProtocolFactory());
acceptor.bind();
}
use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.
the class TestLineBasedServer method main.
public static void main(String[] args) throws Exception {
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
String res = "yes server already accept your message:" + future.getReadText();
future.write(res);
session.flush(future);
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.setProtocolFactory(new CharBasedProtocolFactory());
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();
}
use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.
the class SimpleTestFIxedLengthBroadcastServer 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(8300));
SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
context.addSessionEventListener(new LoggerSocketSEListener());
context.addSessionEventListener(new SetOptionListener());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.setProtocolFactory(new FixedLengthProtocolFactory());
acceptor.bind();
ThreadUtil.execute(new Runnable() {
@Override
public void run() {
for (; ; ) {
ThreadUtil.sleep(1000);
FixedLengthFuture future = new FixedLengthFutureImpl(context);
future.write("broadcast msg .........................");
try {
acceptor.broadcast(future);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor 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