use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.
the class SimpleTestProtobaseServer method main.
public static void main(String[] args) throws Exception {
DebugUtil.setEnableDebug(true);
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
DebugUtil.debug("receive:" + future.getReadText());
future.write("yes server already accept your message:");
future.write(future.getReadText());
session.flush(future);
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
context.getServerConfiguration().setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
context.getServerConfiguration().setSERVER_SESSION_IDLE_TIME(60 * 60 * 1000);
SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setBeatFutureFactory(new ProtobaseBeatFutureFactory());
context.addSessionIdleEventListener(new SocketSessionAliveSEListener());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.setProtocolFactory(new ProtobaseProtocolFactory());
acceptor.bind();
}
use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.
the class TestSessionDisconnect method main.
public static void main(String[] args) throws Exception {
String serviceName = "TestSessionDisconnectServlet";
String param = "ttt";
LoggerFactory.configure();
SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
ServerConfiguration configuration = new ServerConfiguration(8300);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandle);
context.setProtocolFactory(new ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
FixedSession session = new FixedSession(connector.connect());
session.login("admin", "admin100");
ProtobaseFuture future = session.request(serviceName, param);
System.out.println(future.getReadText());
session.listen(serviceName, new OnFuture() {
@Override
public void onResponse(SocketSession session, Future future) {
ProtobaseFuture f = (ProtobaseFuture) future;
System.out.println(f.getReadText());
}
});
session.write(serviceName, param);
ThreadUtil.sleep(9999);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.
the class TestSimple method main.
public static void main(String[] args) throws Exception {
String serviceKey = "/test-simple";
String param = "ttt";
IoEventHandleAdaptor eventHandle = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
System.out.println("________________________" + future.getReadText());
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setProtocolFactory(new ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
context.setIoEventHandleAdaptor(eventHandle);
SocketSession session = connector.connect();
ProtobaseFuture f = new ProtobaseFutureImpl(connector.getContext(), serviceKey);
f.write(param);
session.flush(f);
ThreadUtil.sleep(500);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.
the class AbstractFutureAcceptor method acceptHeartBeat.
private void acceptHeartBeat(final SocketSession session, final ChannelFuture future) {
if (future.isPING()) {
heartBeatLogger.logRequest(session);
SocketChannelContext context = session.getContext();
BeatFutureFactory factory = context.getBeatFutureFactory();
if (factory == null) {
RuntimeException e = new RuntimeException("none factory of BeatFuture");
CloseUtil.close(session);
logger.error(e.getMessage(), e);
return;
}
Future f = factory.createPONGPacket(session);
session.flush(f);
} else {
heartBeatLogger.logResponse(session);
}
}
use of com.generallycloud.baseio.protocol.Future in project baseio by generallycloud.
the class SocketSessionActiveSEListener method sessionIdled.
@Override
public void sessionIdled(SocketSession session, long lastIdleTime, long currentTime) {
if (session.isClosed()) {
logger.info("closed session");
return;
}
if (session.getLastAccessTime() < lastIdleTime) {
logger.info("Did not detect heartbeat messages in heartbeat cycle, prepare to disconnect {}", session);
CloseUtil.close(session);
} else {
SocketChannelContext context = session.getContext();
BeatFutureFactory factory = context.getBeatFutureFactory();
if (factory == null) {
RuntimeException e = new RuntimeException("none factory of BeatFuture");
CloseUtil.close(session);
logger.error(e.getMessage(), e);
return;
}
Future future = factory.createPINGPacket(session);
if (future == null) {
// 该session无需心跳,比如HTTP协议
return;
}
session.flush(future);
}
}
Aggregations