use of com.generallycloud.baseio.component.SocketChannelContext in project baseio by generallycloud.
the class ApplicationBootstrapEngine method bootstrap.
@Override
public void bootstrap(String rootPath, boolean deployModel) throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
Properties serverProperties = FileUtil.readPropertiesByCls("server.properties");
ServerConfiguration sc = new ServerConfiguration();
new PropertiesSCLoader("SERVER").loadConfiguration(sc, serverProperties);
ApplicationContext applicationContext = new ApplicationContext(rootPath);
applicationContext.setDeployModel(deployModel);
SocketChannelContext channelContext = new NioSocketChannelContext(sc);
// SocketChannelContext channelContext = new AioSocketChannelContext(sc);
SocketChannelAcceptor acceptor = new SocketChannelAcceptor(channelContext);
try {
Properties intfProperties = FileUtil.readPropertiesByCls("intf.properties");
applicationContext.setBlackIPs(loadBlackIPs());
applicationContext.setChannelContext(channelContext);
ApplicationConfigurationLoader acLoader = loadConfigurationLoader(intfProperties.getProperty("intf.ApplicationConfigurationLoader"));
ApplicationExtLoader applicationExtLoader = loadApplicationExtLoader(intfProperties.getProperty("intf.ApplicationExtLoader"));
ApplicationContextEnricher enricher = loadApplicationContextEnricher(intfProperties.getProperty("intf.ApplicationContextEnricher"));
applicationContext.setApplicationExtLoader(applicationExtLoader);
applicationContext.setApplicationConfigurationLoader(acLoader);
enricher.enrich(applicationContext);
channelContext.setIoEventHandleAdaptor(new ApplicationIoEventHandle(applicationContext));
if (sc.isSERVER_ENABLE_SSL()) {
if (!StringUtil.isNullOrBlank(sc.getSERVER_CERT_KEY())) {
File certificate = FileUtil.readFileByCls(sc.getSERVER_CERT_CRT(), classLoader);
File privateKey = FileUtil.readFileByCls(sc.getSERVER_CERT_KEY(), classLoader);
SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
channelContext.setSslContext(sslContext);
} else {
String keystoreInfo = sc.getSERVER_SSL_KEYSTORE();
if (StringUtil.isNullOrBlank(keystoreInfo)) {
throw new IllegalArgumentException("ssl enabled,but no config for");
}
String[] params = keystoreInfo.split(";");
if (params.length != 4) {
throw new IllegalArgumentException("SERVER_SSL_KEYSTORE config error");
}
File storeFile = FileUtil.readFileByCls(params[0], classLoader);
SslContext sslContext = SSLUtil.initServer(storeFile, params[1], params[2], params[3]);
channelContext.setSslContext(sslContext);
}
}
sc.setSERVER_PORT(getServerPort(sc.getSERVER_PORT(), sc.isSERVER_ENABLE_SSL()));
acceptor.bind();
} catch (Throwable e) {
Logger logger = LoggerFactory.getLogger(getClass());
logger.error(e.getMessage(), e);
CloseUtil.unbind(acceptor);
}
}
use of com.generallycloud.baseio.component.SocketChannelContext 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.component.SocketChannelContext 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.component.SocketChannelContext in project baseio by generallycloud.
the class TestBalanceLoad 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());
connector.connect();
System.in.read();
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.component.SocketChannelContext in project baseio by generallycloud.
the class TestBytebufAllocator method test.
static void test() throws Exception {
ServerConfiguration configuration = new ServerConfiguration();
configuration.setSERVER_MEMORY_POOL_CAPACITY(10);
configuration.setSERVER_MEMORY_POOL_UNIT(1);
SocketChannelContext context = new NioSocketChannelContext(configuration);
PooledByteBufAllocatorManager allocator = new PooledByteBufAllocatorManager(context);
allocator.start();
ByteBufAllocator allocator2 = allocator.getNextBufAllocator();
ByteBuf buf = allocator2.allocate(15);
System.out.println(buf);
}
Aggregations