Search in sources :

Example 1 with ChannelEventListenerAdapter

use of com.firenio.component.ChannelEventListenerAdapter in project baseio by generallycloud.

the class TestLengthValueServerJunit method server.

@Before
public void server() throws Exception {
    IoEventHandle eventHandle = 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);
        }
    };
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setIoEventHandle(eventHandle);
    context.addProtocolCodec(new LengthValueCodec());
    context.addChannelEventListener(new ChannelEventListenerAdapter() {

        @Override
        public void channelOpened(Channel ch) throws Exception {
            System.out.println(ch.getOption(SocketOptions.TCP_NODELAY));
            System.out.println(ch.getOption(SocketOptions.SO_RCVBUF));
            ch.setOption(SocketOptions.TCP_NODELAY, 1);
            ch.setOption(SocketOptions.SO_RCVBUF, 1028);
            System.out.println(ch.getOption(SocketOptions.TCP_NODELAY));
            System.out.println(ch.getOption(SocketOptions.SO_RCVBUF));
        }
    });
    context.bind();
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) LengthValueCodec(com.firenio.codec.lengthvalue.LengthValueCodec) LengthValueFrame(com.firenio.codec.lengthvalue.LengthValueFrame) Frame(com.firenio.component.Frame) Channel(com.firenio.component.Channel) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) Before(org.junit.Before)

Example 2 with ChannelEventListenerAdapter

use of com.firenio.component.ChannelEventListenerAdapter 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);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) HttpFrame(com.firenio.codec.http11.HttpFrame) Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) JsonStream(com.jsoniter.output.JsonStream) ByteBuf(com.firenio.buffer.ByteBuf) HttpFrame(com.firenio.codec.http11.HttpFrame) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) ByteTree(com.firenio.collection.ByteTree) HttpCodec(com.firenio.codec.http11.HttpCodec) Slice(com.jsoniter.spi.Slice) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup)

Example 3 with ChannelEventListenerAdapter

use of com.firenio.component.ChannelEventListenerAdapter 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);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) Channel(com.firenio.component.Channel) IOException(java.io.IOException) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) IOException(java.io.IOException) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec)

Example 4 with ChannelEventListenerAdapter

use of com.firenio.component.ChannelEventListenerAdapter in project baseio by generallycloud.

the class HttpProxy4CloudServer method strtup.

public synchronized void strtup(NioEventLoopGroup serverG, int port) throws Exception {
    if (context != null && context.isActive()) {
        return;
    }
    context = new ChannelAcceptor(serverG, 8088);
    context.addProtocolCodec(new HttpProxy4CloudCodec());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new ChannelEventListenerAdapter() {

        @Override
        public void channelClosed(Channel ch) {
            Util.close(((HttpProxy4CloudAttr) ch.getAttachment()).target);
        }
    });
    context.bind();
}
Also used : Channel(com.firenio.component.Channel) ChannelAcceptor(com.firenio.component.ChannelAcceptor) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener)

Aggregations

Channel (com.firenio.component.Channel)4 ChannelEventListenerAdapter (com.firenio.component.ChannelEventListenerAdapter)4 Frame (com.firenio.component.Frame)3 IoEventHandle (com.firenio.component.IoEventHandle)3 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)3 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 ByteBuf (com.firenio.buffer.ByteBuf)1 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)1 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)1 HttpCodec (com.firenio.codec.http11.HttpCodec)1 HttpFrame (com.firenio.codec.http11.HttpFrame)1 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)1 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)1 LengthValueFrame (com.firenio.codec.lengthvalue.LengthValueFrame)1 ByteTree (com.firenio.collection.ByteTree)1 ChannelConnector (com.firenio.component.ChannelConnector)1 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)1 JsonStream (com.jsoniter.output.JsonStream)1 Slice (com.jsoniter.spi.Slice)1 IOException (java.io.IOException)1