Search in sources :

Example 46 with NioSocketChannelContext

use of com.generallycloud.baseio.component.NioSocketChannelContext in project baseio by generallycloud.

the class TestLineBasedBroadcastServer method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            long old = System.currentTimeMillis();
            String res = "hello world!";
            future.write(res);
            ChannelAcceptor acceptor = (ChannelAcceptor) session.getContext().getChannelService();
            acceptor.broadcast(future);
            long now = System.currentTimeMillis();
            System.out.println("广播花费时间:" + (now - old) + ",连接数:" + session.getContext().getSessionManager().getManagedSessionSize());
        }
    };
    ServerConfiguration configuration = new ServerConfiguration();
    configuration.setSERVER_PORT(18300);
    configuration.setSERVER_SESSION_IDLE_TIME(180000);
    configuration.setSERVER_MEMORY_POOL_CAPACITY(1024 * 512);
    configuration.setSERVER_MEMORY_POOL_UNIT(64);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new CharBasedProtocolFactory());
    acceptor.bind();
}
Also used : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) ChannelAcceptor(com.generallycloud.baseio.acceptor.ChannelAcceptor) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory)

Example 47 with NioSocketChannelContext

use of com.generallycloud.baseio.component.NioSocketChannelContext in project baseio by generallycloud.

the class TestLineBasedClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new CharBasedProtocolFactory());
    SocketSession session = connector.connect();
    CharBasedFuture future = new CharBasedFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) CharBasedFutureImpl(com.generallycloud.baseio.codec.charbased.future.CharBasedFutureImpl) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory) CharBasedFuture(com.generallycloud.baseio.codec.charbased.future.CharBasedFuture)

Example 48 with NioSocketChannelContext

use of com.generallycloud.baseio.component.NioSocketChannelContext in project baseio by generallycloud.

the class TestLineBasedServer method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            String res = "yes server already accept your message:" + future.getReadText();
            future.write(res);
            session.flush(future);
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new CharBasedProtocolFactory());
    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 : LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) SocketSession(com.generallycloud.baseio.component.SocketSession) 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) CharBasedProtocolFactory(com.generallycloud.baseio.codec.charbased.CharBasedProtocolFactory) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Example 49 with NioSocketChannelContext

use of com.generallycloud.baseio.component.NioSocketChannelContext in project baseio by generallycloud.

the class SimpleTestFIxedLengthBroadcastServer 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(8300));
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.addSessionEventListener(new SetOptionListener());
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    acceptor.bind();
    ThreadUtil.execute(new Runnable() {

        @Override
        public void run() {
            for (; ; ) {
                ThreadUtil.sleep(1000);
                FixedLengthFuture future = new FixedLengthFutureImpl(context);
                future.write("broadcast msg .........................");
                try {
                    acceptor.broadcast(future);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IOException(java.io.IOException) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) 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) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor)

Example 50 with NioSocketChannelContext

use of com.generallycloud.baseio.component.NioSocketChannelContext in project baseio by generallycloud.

the class SimpleTestFIxedLengthClient method main.

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

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 8300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    SocketSession session = connector.connect();
    FixedLengthFuture future = new FixedLengthFutureImpl(context);
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) FixedLengthFuture(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture) 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) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Aggregations

NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)56 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)54 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)54 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)51 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)40 SocketSession (com.generallycloud.baseio.component.SocketSession)33 Future (com.generallycloud.baseio.protocol.Future)31 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)27 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)26 FixedSession (com.generallycloud.baseio.container.FixedSession)21 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)21 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)16 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)11 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)11 FixedLengthFuture (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFuture)7 ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)7 FixedLengthFutureImpl (com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl)6 MessageConsumer (com.generallycloud.baseio.container.jms.client.MessageConsumer)6 DefaultMessageConsumer (com.generallycloud.baseio.container.jms.client.impl.DefaultMessageConsumer)6 ParamedProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)5