Esempio n. 1
0
  private void start() {
    // 1. instance
    reactorManager = CommonUtils.newInstance(clientConfig.getReactorManagerClassName());
    ioWorker = CommonUtils.newInstance(clientConfig.getIoWorkerClassName());
    eventWorker = CommonUtils.newInstance(clientConfig.getEventWorkerClassName());
    protocolFactory = CommonUtils.newInstance(clientConfig.getProtocolFactoryClassName());
    messageHandler = CommonUtils.newInstance(clientConfig.getMessageHandlerClassName());

    ioWorker.setReactorManager(reactorManager);

    eventWorker.setProtocolFactory(protocolFactory).setMessageHandler(messageHandler);

    reactorManager.setNIOConfig(clientConfig).setIoWorker(ioWorker).setEventWorker(eventWorker);

    reactorManager.start();
  }
Esempio n. 2
0
  /**
   * @throws IOException
   * @throws InterruptedException
   */
  protected synchronized void reConnect() throws IOException, InterruptedException {
    // 0. Don't send connect request if it is connecting.
    if (isNotConnected()) {
      SocketAddress remoteAddress = new InetSocketAddress(this.host, this.port);

      // 1. Create socket channel
      SocketChannel channel = SocketChannel.open();
      channel.configureBlocking(false);
      channel.socket().setTcpNoDelay(true);
      channel.socket().setReceiveBufferSize(48 * 1024);
      channel.socket().setSendBufferSize(48 * 1024);
      channel.connect(remoteAddress);

      // 2. Create NioSession for each client connection
      IoSession client =
          new DefaultIoSession().setChannel(channel).setIoWork(ioWorker).setEventWork(eventWorker);
      // 3. Register event
      reactorManager.nextReactor().registerSession(client, SelectionKey.OP_CONNECT);

      // 4. Wait to connect
      if (!client.waitToConnect(connectTimeout, TimeUnit.MILLISECONDS)) {
        client.close(); // TODO:asyncClose();
        throw new IOException("connect timed out to " + this.host + ":" + this.port);
      }

      IoSession.Status status = client.getStatus();
      if (status == IoSession.Status.NOT_CONNECT || status == IoSession.Status.CLOSED) {
        client.close(); // TODO:.asyncClose();
        throw new IOException("connect failed to " + this.host + ":" + this.port);
      }

      this.session = client;
    }
  }