Search in sources :

Example 31 with ServerConfiguration

use of com.generallycloud.baseio.configuration.ServerConfiguration in project baseio by generallycloud.

the class ConnectExecutable method exec.

@Override
public CmdResponse exec(CommandContext context, HashMap<String, String> params) {
    CmdResponse response = new CmdResponse();
    SocketChannelConnector connector = getClientConnector(context);
    if (connector != null) {
        response.setResponse("已登录。");
        return response;
    }
    String username = params.get("-un");
    String password = params.get("-p");
    String host = params.get("-host");
    String port = params.get("-port");
    if (StringUtil.isNullOrBlank(username) || StringUtil.isNullOrBlank(password) || StringUtil.isNullOrBlank(host) || StringUtil.isNullOrBlank(port)) {
        response.setResponse("参数不正确!\n" + "example:\n" + "connect -host:localhost -port:8300 -un:admin -p:admin100");
        return response;
    }
    try {
        NioSocketChannelContext baseContext = new NioSocketChannelContext(new ServerConfiguration(Integer.parseInt(port)));
        connector = new SocketChannelConnector(baseContext);
        SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
        baseContext.setIoEventHandleAdaptor(eventHandle);
        baseContext.addSessionEventListener(new LoggerSocketSEListener());
        FixedSession session = new FixedSession(connector.connect());
        // FIXME denglu cuowu
        session.login(username, password);
        MessageBrowser browser = new DefaultMessageBrowser(session);
        response.setResponse("连接成功!");
        setClientConnector(context, connector);
        setMessageBrowser(context, browser);
    } catch (Exception e) {
        setClientConnector(context, null);
        setMessageBrowser(context, null);
        response.setResponse(e.getMessage());
        // debug
        logger.debug(e);
    }
    return response;
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) CmdResponse(com.generallycloud.baseio.container.jms.cmd.CmdResponse) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) MessageBrowser(com.generallycloud.baseio.container.jms.client.MessageBrowser) DefaultMessageBrowser(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageBrowser) DefaultMessageBrowser(com.generallycloud.baseio.container.jms.client.impl.DefaultMessageBrowser) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 32 with ServerConfiguration

use of com.generallycloud.baseio.configuration.ServerConfiguration in project baseio by generallycloud.

the class ThreadEventLoop method doStartup.

@Override
protected void doStartup() throws Exception {
    ServerConfiguration sc = context.getServerConfiguration();
    int eventQueueSize = sc.getSERVER_WORK_EVENT_QUEUE_SIZE();
    this.jobs = new ArrayBlockingQueue<>(eventQueueSize);
    super.doStartup();
}
Also used : ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration)

Example 33 with ServerConfiguration

use of com.generallycloud.baseio.configuration.ServerConfiguration in project baseio by generallycloud.

the class UnpooledByteBufAllocatorManager method doStart.

@Override
protected void doStart() throws Exception {
    ServerConfiguration c = context.getServerConfiguration();
    boolean isDirect = c.isSERVER_ENABLE_MEMORY_POOL_DIRECT();
    unpooledByteBufAllocator = new UnpooledByteBufAllocator(isDirect);
    LifeCycleUtil.start(unpooledByteBufAllocator);
}
Also used : ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration)

Example 34 with ServerConfiguration

use of com.generallycloud.baseio.configuration.ServerConfiguration in project baseio by generallycloud.

the class SimpleTestFIxedLengthClientPush method main.

@SuppressWarnings("resource")
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(">msg from server: " + future.getReadText());
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new FixedLengthProtocolFactory());
    SocketSession session = connector.connect();
    ThreadUtil.execute(new Runnable() {

        @Override
        public void run() {
            System.out.println("************************************************");
            System.out.println("提示:");
            System.out.println("list(获取所有客户端id)");
            System.out.println("id(获取当前客户端id)");
            System.out.println("push id msg(推送消息到)");
            System.out.println("broadcast msg(广播消息)");
            System.out.println("exit(退出客户端)");
            System.out.println("仅用于演示,msg请勿包含空格");
            System.out.println("************************************************");
            Scanner scanner = new Scanner(System.in);
            for (; ; ) {
                System.out.println(">");
                String line = scanner.nextLine();
                if ("exit".equals(line)) {
                    CloseUtil.close(session);
                    break;
                }
                FixedLengthFuture future = new FixedLengthFutureImpl(context);
                future.write(line);
                session.flush(future);
            }
        }
    });
}
Also used : FixedLengthFutureImpl(com.generallycloud.baseio.codec.fixedlength.future.FixedLengthFutureImpl) Scanner(java.util.Scanner) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) 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)

Example 35 with ServerConfiguration

use of com.generallycloud.baseio.configuration.ServerConfiguration 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)

Aggregations

ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)62 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)55 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)54 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)53 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)41 SocketSession (com.generallycloud.baseio.component.SocketSession)35 Future (com.generallycloud.baseio.protocol.Future)33 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)29 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)28 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)12 FixedLengthProtocolFactory (com.generallycloud.baseio.codec.fixedlength.FixedLengthProtocolFactory)12 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