use of com.firenio.codec.http11.HttpCodec in project baseio by generallycloud.
the class TestHttpLoadServerTFB method main.
public static void main(String[] args) throws Exception {
System.setProperty("core", "1");
System.setProperty("frame", "16");
System.setProperty("readBuf", "512");
System.setProperty("pool", "true");
System.setProperty("inline", "true");
System.setProperty("level", "1");
System.setProperty("read", "false");
System.setProperty("epoll", "true");
System.setProperty("nodelay", "true");
System.setProperty("cachedurl", "true");
System.setProperty("unsafeBuf", "false");
boolean lite = Util.getBooleanProperty("lite");
boolean read = Util.getBooleanProperty("read");
boolean pool = Util.getBooleanProperty("pool");
boolean epoll = Util.getBooleanProperty("epoll");
boolean direct = Util.getBooleanProperty("direct");
boolean nodelay = Util.getBooleanProperty("nodelay");
boolean cachedurl = Util.getBooleanProperty("cachedurl");
boolean unsafeBuf = Util.getBooleanProperty("unsafeBuf");
int core = Util.getIntProperty("core", 1);
int frame = Util.getIntProperty("frame", 16);
int level = Util.getIntProperty("level", 1);
int readBuf = Util.getIntProperty("readBuf", 16);
LoggerFactory.setEnableSLF4JLogger(false);
LoggerFactory.setLogLevel(LoggerFactory.LEVEL_INFO);
Options.setBufAutoExpansion(false);
Options.setChannelReadFirst(read);
Options.setEnableEpoll(epoll);
Options.setEnableUnsafeBuf(unsafeBuf);
DebugUtil.info("lite: {}", lite);
DebugUtil.info("read: {}", read);
DebugUtil.info("pool: {}", pool);
DebugUtil.info("core: {}", core);
DebugUtil.info("epoll: {}", epoll);
DebugUtil.info("frame: {}", frame);
DebugUtil.info("level: {}", level);
DebugUtil.info("readBuf: {}", readBuf);
DebugUtil.info("nodelay: {}", nodelay);
DebugUtil.info("cachedurl: {}", cachedurl);
DebugUtil.info("unsafeBuf: {}", unsafeBuf);
int processors = Util.availableProcessors() * core;
int fcache = 1024 * 16;
int pool_unit = 256 * 16;
int pool_cap = 1024 * 8 * pool_unit * processors;
String server = "firenio";
ByteTree cachedUrls = null;
if (cachedurl) {
cachedUrls = new ByteTree();
cachedUrls.add("/plaintext");
cachedUrls.add("/json");
}
HttpCodec codec = new HttpCodec(server, fcache, lite, cachedUrls) {
@Override
protected Object newAttachment() {
return new MyHttpAttachment();
}
};
IoEventHandle eventHandle = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
HttpFrame f = (HttpFrame) frame;
String action = f.getRequestURL();
if ("/plaintext".equals(action)) {
MyHttpAttachment att = (MyHttpAttachment) ch.getAttachment();
ByteBuf buf = att.write_buf;
if (buf == null) {
buf = ch.allocate();
ByteBuf temp = buf;
att.write_buf = buf;
ch.getEventLoop().submit(() -> {
ch.writeAndFlush(temp);
att.write_buf = null;
});
}
f.setContent(STATIC_PLAINTEXT);
f.setContentType(HttpContentType.text_plain);
f.setConnection(HttpConnection.NONE);
f.setDate(HttpDateUtil.getDateLine());
codec.encode(ch, buf, f);
codec.release(ch.getEventLoop(), f);
} else if ("/json".equals(action)) {
ByteBuf temp = FastThreadLocal.get().getAttribute(JSON_BUF);
if (temp == null) {
temp = ByteBuf.heap(0);
FastThreadLocal.get().setAttribute(JSON_BUF, temp);
}
JsonStream stream = JsonStreamPool.borrowJsonStream();
try {
stream.reset(null);
stream.writeVal(Message.class, new Message("Hello, World!"));
Slice slice = stream.buffer();
temp.reset(slice.data(), slice.head(), slice.tail());
f.setContent(temp);
f.setContentType(HttpContentType.application_json);
f.setConnection(HttpConnection.NONE);
f.setDate(HttpDateUtil.getDateLine());
ch.writeAndFlush(f);
ch.release(f);
} finally {
JsonStreamPool.returnJsonStream(stream);
}
} else {
System.err.println("404");
f.setString("404,page not found!", ch);
f.setContentType(HttpContentType.text_plain);
f.setStatus(HttpStatus.C404);
f.setDate(HttpDateUtil.getDateLine());
ch.writeAndFlush(f);
ch.release(f);
}
}
};
HttpDateUtil.start();
NioEventLoopGroup group = new NioEventLoopGroup();
ChannelAcceptor context = new ChannelAcceptor(group, 8081);
group.setMemoryCapacity(pool_cap);
group.setEnableMemoryPool(pool);
group.setMemoryUnit(pool_unit);
group.setWriteBuffers(8);
group.setChannelReadBuffer(1024 * readBuf);
group.setEventLoopSize(Util.availableProcessors() * core);
group.setConcurrentFrameStack(false);
if (nodelay) {
context.addChannelEventListener(new ChannelEventListenerAdapter() {
@Override
public void channelOpened(Channel ch) throws Exception {
ch.setOption(SocketOptions.TCP_NODELAY, 1);
ch.setOption(SocketOptions.SO_KEEPALIVE, 0);
}
});
}
context.addProtocolCodec(codec);
context.setIoEventHandle(eventHandle);
context.bind(1024 * 8);
}
use of com.firenio.codec.http11.HttpCodec in project baseio by generallycloud.
the class TestHttpBootstrapEngine method bootstrap.
@Override
public void bootstrap(String rootPath, boolean prodMode) throws Exception {
ClassLoader cl = this.getClass().getClassLoader();
boolean debug = Util.isTrueValue(System.getProperty("http.debug"));
if (debug) {
for (; debug; ) {
Util.sleep(100);
}
}
DevelopConfig.NATIVE_DEBUG = true;
DevelopConfig.BUF_DEBUG = true;
DevelopConfig.BUF_PATH_DEBUG = true;
DevelopConfig.SSL_DEBUG = true;
DevelopConfig.CHANNEL_DEBUG = true;
DevelopConfig.DEBUG_ERROR = true;
Options.setEnableEpoll(true);
Options.setEnableUnsafe(true);
Options.setEnableOpenssl(true);
Options.setBufThreadYield(true);
// Options.setEnableUnsafeBuf(true);
HttpDateUtil.start();
final SpringHttpFrameHandle handle = new SpringHttpFrameHandle();
Properties properties = FileUtil.readPropertiesByCls("server.properties", cl);
NioEventLoopGroup group = new NioEventLoopGroup(true);
ChannelAcceptor context = new ChannelAcceptor(group);
ConfigurationParser.parseConfiguration("server.", context, properties);
ConfigurationParser.parseConfiguration("server.", group, properties);
context.setIoEventHandle(handle);
context.addChannelIdleEventListener(new ChannelAliveListener());
context.addChannelEventListener(new WebSocketChannelListener());
context.addChannelEventListener(new LoggerChannelOpenListener());
context.addChannelEventListener(new CountChannelListener());
context.setExecutorGroup(new ThreadEventLoopGroup());
context.addLifeCycleListener(new LifeCycleListener() {
@Override
public void lifeCycleStarting(LifeCycle lifeCycle) {
try {
handle.initialize(context, rootPath, prodMode);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
@Override
public void lifeCycleStopped(LifeCycle lifeCycle) {
handle.destroy(context);
}
});
String[] applicationProtocols = null;
if (properties.getBooleanProperty("app.enableHttp2")) {
context.addProtocolCodec(new Http2Codec());
applicationProtocols = new String[] { "h2", "http/1.1" };
} else {
context.addProtocolCodec(new HttpCodec(4));
context.addProtocolCodec(new WebSocketCodec());
}
int defaultPort = 80;
String pem = properties.getProperty("server.sslPem");
if (!Util.isNullOrBlank(pem)) {
defaultPort = 443;
SslContext sslContext = loadSslContextFromPem(pem, applicationProtocols, cl);
context.setSslContext(sslContext);
}
int port = properties.getIntegerProperty("server.port", defaultPort);
context.setPort(port);
try {
context.bind();
} catch (Exception e) {
HttpDateUtil.stop();
group.stop();
throw e;
}
this.group = group;
this.context = context;
if (properties.getBooleanProperty("app.proxy")) {
NetDataTransferServer.get().startup(group, 18088);
}
}
use of com.firenio.codec.http11.HttpCodec in project baseio by generallycloud.
the class TestHttpLoadServer method main.
public static void main(String[] args) throws Exception {
IoEventHandle eventHandle = new IoEventHandle() {
@Override
public void accept(Channel ch, Frame frame) throws Exception {
HttpFrame f = (HttpFrame) frame;
f.setConnection(HttpConnection.KEEP_ALIVE);
f.setContentType(HttpContentType.text_plain);
f.setString("Hello World", ch);
ch.writeAndFlush(f);
ch.release(f);
}
};
NioEventLoopGroup group = new NioEventLoopGroup();
group.setMemoryCapacity(1024 * 1024 * 64);
group.setMemoryUnit(512);
group.setEventLoopSize(2);
ChannelAcceptor context = new ChannelAcceptor(group, 8080);
context.addProtocolCodec(new HttpCodec(8));
context.addProtocolCodec(new WebSocketCodec());
context.setIoEventHandle(eventHandle);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.bind();
}
Aggregations