use of com.firenio.component.LoggerChannelOpenListener in project baseio by generallycloud.
the class TestLoadServer method main.
public static void main(String[] args) throws Exception {
Options.setBufAutoExpansion(AUTO_EXPANSION);
Options.setEnableEpoll(ENABLE_EPOLL);
Options.setEnableUnsafeBuf(ENABLE_UNSAFE_BUF);
IoEventHandle eventHandle = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame f) throws Exception {
String text = f.getStringContent();
if (BUFFERED_WRITE) {
ByteBuf buf = ch.getAttribute(WRITE_BUF);
if (buf == null) {
buf = ch.allocate();
ByteBuf temp = buf;
ch.setAttribute(WRITE_BUF, buf);
ch.getEventLoop().submit(() -> {
ch.writeAndFlush(temp);
ch.setAttribute(WRITE_BUF, null);
});
}
byte[] data = text.getBytes(ch.getCharset());
buf.writeInt(data.length);
buf.writeBytes(data);
} else {
f.setString(text, ch);
ch.writeAndFlush(f);
}
}
};
NioEventLoopGroup group = new NioEventLoopGroup(SERVER_CORE_SIZE);
group.setMemoryCapacity(1024 * 512 * MEM_UNIT * SERVER_CORE_SIZE);
group.setWriteBuffers(WRITE_BUFFERS);
group.setMemoryUnit(MEM_UNIT);
group.setEnableMemoryPool(ENABLE_POOL);
ChannelAcceptor context = new ChannelAcceptor(group, 8300);
context.addProtocolCodec(new LengthValueCodec());
context.setIoEventHandle(eventHandle);
if (ENABLE_SSL) {
// context.setSslPem("localhost.key;localhost.crt");
}
context.addChannelEventListener(new LoggerChannelOpenListener());
if (ENABLE_WORK_EVENT_LOOP) {
context.setExecutorGroup(new ThreadEventLoopGroup("ep", 1024 * 256 * CLIENT_CORE_SIZE));
}
context.bind();
}
use of com.firenio.component.LoggerChannelOpenListener in project baseio by generallycloud.
the class TestSimpleHttpClient method main.
@Test
public void main() throws Exception {
Options.setEnableEpoll(true);
Waiter<String> w = new Waiter<>();
ChannelConnector context = new ChannelConnector("firenio.com", 443);
SslContext sslContext = SslContextBuilder.forClient(true).build();
context.addProtocolCodec(new ClientHttpCodec());
context.addProtocolCodec(new WebSocketCodec());
context.setIoEventHandle(new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) {
ClientHttpFrame res = (ClientHttpFrame) frame;
System.out.println();
System.out.println(new String(res.getBytesContent()));
System.out.println();
Util.close(context);
w.call(new String(res.getBytesContent()), null);
}
});
context.addChannelEventListener(new LoggerChannelOpenListener());
context.setSslContext(sslContext);
long start = Util.now();
Channel ch = context.connect(3000);
ch.writeAndFlush(new ClientHttpFrame("/test?p=2222"));
w.await(3000);
System.out.println(Util.past(start));
Assert.assertEquals("yes server already accept your message :) {p=2222}", w.getResponse());
}
use of com.firenio.component.LoggerChannelOpenListener in project baseio by generallycloud.
the class TestSimpleHttpClient2 method main.
// telnet 192.168.133.134 8080
public static void main(String[] args) throws Exception {
String host = "www.baidu.com";
String url = "/plaintext";
host = "firenio.com";
host = "127.0.0.1";
// host = "fe80::a793:9577:4396:8ca6";
// host = "www.baidu.com";
// host = "api.weixin.qq.com";
// host = "192.168.1.103";
int port = 1443;
port = 8300;
// port = 443;
ChannelConnector context = new ChannelConnector(host, port);
context.addProtocolCodec(new ClientHttpCodec());
context.addProtocolCodec(new WebSocketCodec());
context.setIoEventHandle(new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
ClientHttpFrame res = (ClientHttpFrame) frame;
System.out.println();
System.out.println(new String(res.getBytesContent()));
System.out.println();
// Util.close(context);
}
});
context.addChannelEventListener(new ChannelEventListenerAdapter() {
@Override
public void channelOpened(Channel ch) throws Exception {
throw new IOException();
}
});
context.addChannelEventListener(new LoggerChannelOpenListener());
if (port == 1443 || port == 443) {
context.setSslContext(SslContextBuilder.forClient(true).build());
}
Channel ch = context.connect(999999);
ClientHttpFrame f = new ClientHttpFrame(url, HttpMethod.GET);
// f.setRequestHeader(HttpHeader.Host, host);
// f.setContent("abc123".readBytes());
ch.write(f);
ch.writeAndFlush(f);
}
use of com.firenio.component.LoggerChannelOpenListener in project baseio by generallycloud.
the class TestHttp2Server method main.
public static void main(String[] args) throws Exception {
IoEventHandle eventHandleAdaptor = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
frame.write("Hello World", ch);
ch.writeAndFlush(frame);
}
};
NioEventLoopGroup group = new NioEventLoopGroup();
group.setMemoryCapacity(1024 * 1024 * 4);
group.setMemoryUnit(512);
group.setEnableMemoryPool(true);
ChannelAcceptor context = new ChannelAcceptor(group, 443);
context.addProtocolCodec(new Http2Codec());
context.setIoEventHandle(eventHandleAdaptor);
// context.setApplicationProtocols(new String[]{"h2", "http/1.1"});
context.addChannelEventListener(new LoggerChannelOpenListener());
context.bind();
}
use of com.firenio.component.LoggerChannelOpenListener in project baseio by generallycloud.
the class TestLengthValueServer method main.
public static void main(String[] args) throws Exception {
IoEventHandle eventHandleAdaptor = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame f) throws Exception {
String text = f.getStringContent();
f.setContent(ch.allocateWithSkipHeader(1));
f.write("yes server already accept your message:", ch);
f.write(text, ch);
ch.writeAndFlush(f);
}
};
ChannelAcceptor context = new ChannelAcceptor(8300);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.setIoEventHandle(eventHandleAdaptor);
context.addProtocolCodec(new LengthValueCodec());
context.bind();
}
Aggregations