Search in sources :

Example 6 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor 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();
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ProtobaseBeatFutureFactory(com.generallycloud.baseio.codec.protobase.future.ProtobaseBeatFutureFactory) SocketSessionAliveSEListener(com.generallycloud.baseio.component.SocketSessionAliveSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 7 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.

the class SimpleTestFIxedLengthServerPush method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            SocketSessionManager sessionManager = session.getContext().getSessionManager();
            Map<Integer, SocketSession> sessions = sessionManager.getManagedSessions();
            String msg = future.getReadText();
            String[] arr = msg.split(" ");
            String cmd = arr[0];
            logger.info("msg received: {}", msg);
            if ("list".equals(cmd)) {
                String keys = sessions.keySet().toString();
                future.write(keys);
            } else if ("id".equals(cmd)) {
                future.write(String.valueOf(session.getSessionId()));
            } else if ("push".equals(cmd)) {
                Integer id = Integer.valueOf(arr[1]);
                SocketSession target = sessions.get(id);
                if (target == null) {
                    future.write("offline id: " + id);
                } else {
                    future.write("from [");
                    future.write(String.valueOf(session.getSessionId()));
                    future.write("] push msg>");
                    future.write(arr[2]);
                    target.flush(future);
                    return;
                }
            } else if ("broadcast".equals(cmd)) {
                future.write("from [");
                future.write(String.valueOf(session.getSessionId()));
                future.write("] broadcast msg>");
                future.write(arr[1]);
                sessionManager.broadcast(future);
                return;
            } else {
                future.write("no cmd: " + cmd);
            }
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.addSessionEventListener(new SocketSessionEventListener() {

        @Override
        public void sessionOpened(SocketSession session) throws Exception {
        }

        @Override
        public void sessionClosed(SocketSession session) {
            SocketSessionManager sessionManager = session.getContext().getSessionManager();
            FixedLengthFuture future = new FixedLengthFutureImpl(session.getContext());
            future.write("client left: " + session.getSessionId());
            try {
                sessionManager.broadcast(future);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    });
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    acceptor.bind();
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSessionManager(com.generallycloud.baseio.component.SocketSessionManager) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketSessionEventListener(com.generallycloud.baseio.component.SocketSessionEventListener) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Example 8 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.

the class TestFIxedLengthServer method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            future.write("yes server already accept your message:");
            future.write(future.getReadText());
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SocketSessionAliveSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    File certificate = FileUtil.readFileByCls("generallycloud.com.crt");
    File privateKey = FileUtil.readFileByCls("generallycloud.com.key");
    SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
    context.setSslContext(sslContext);
    acceptor.bind();
}
Also used : FLBeatFutureFactory(com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) File(java.io.File) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Example 9 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.

the class TestLoadServer method main.

public static void main(String[] args) throws Exception {
    LoggerFactory.configure();
    final AtomicInteger res = new AtomicInteger();
    final AtomicInteger req = new AtomicInteger();
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            FixedLengthFuture f = (FixedLengthFuture) future;
            String res = "yes server already accept your message" + f.getReadText();
            f.write(res);
            session.flush(future);
        // System.out.println("req======================"+req.getAndIncrement());
        }
    };
    ServerConfiguration c = new ServerConfiguration(8300);
    c.setSERVER_MEMORY_POOL_CAPACITY(2560000);
    c.setSERVER_MEMORY_POOL_UNIT(128);
    c.setSERVER_MEMORY_POOL_CAPACITY_RATE(0.5);
    c.setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
    c.setSERVER_CORE_SIZE(6);
    SocketChannelContext context = new NioSocketChannelContext(c);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SetOptionListener());
    acceptor.bind();
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketSession(com.generallycloud.baseio.component.SocketSession) FixedLengthProtocolFactory(com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 10 with SocketChannelAcceptor

use of com.generallycloud.baseio.acceptor.SocketChannelAcceptor in project baseio by generallycloud.

the class TestHttpLoadServer method main.

public static void main(String[] args) throws Exception {
    final AtomicInteger res = new AtomicInteger();
    final AtomicInteger req = new AtomicInteger();
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            future.write("hello world!");
            session.flush(future);
        // System.out.println("req======================"+req.getAndIncrement());
        }
    };
    ServerConfiguration c = new ServerConfiguration(8080);
    // c.setSERVER_MEMORY_POOL_CAPACITY(2560000);
    c.setSERVER_MEMORY_POOL_UNIT(256);
    c.setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
    // c.setSERVER_CORE_SIZE(2);
    c.setSERVER_ENABLE_MEMORY_POOL(true);
    c.setSERVER_MEMORY_POOL_CAPACITY_RATE(4);
    SocketChannelContext context = new NioSocketChannelContext(c);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.setProtocolFactory(new ServerHTTPProtocolFactory());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    acceptor.bind();
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) ServerHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ServerHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Aggregations

SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)14 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)12 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)12 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)11 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)11 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)11 SocketSession (com.generallycloud.baseio.component.SocketSession)11 Future (com.generallycloud.baseio.protocol.Future)11 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)5 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)3 SslContext (com.generallycloud.baseio.component.ssl.SslContext)3 File (java.io.File)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CharBasedProtocolFactory (com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)2 FLBeatFutureFactory (com.generallycloud.baseio.codec.fixedlength.future.FLBeatFutureFactory)2 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)2 ServerHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ServerHTTPProtocolFactory)2 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)2 IOException (java.io.IOException)2 ChannelAcceptor (com.generallycloud.baseio.acceptor.ChannelAcceptor)1