Beispiel #1
0
    private void run_() throws java.io.IOException {
      if (serverChannel != null) {
        // set non-blocking mode for the listening socket
        serverChannel.configureBlocking(false);

        // register the ServerSocketChannel with the Selector
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
      }

      for (; ; ) {
        if (please_stop) {
          if (System.currentTimeMillis() >= shutdown_deadline) break;
          if (!anyOpenConnections()) break;
        }
        long timeout = calcNextTimeout();
        int n;
        // System.out.println("selecting...");
        if (timeout != -1) {
          long now = System.currentTimeMillis();
          if (timeout > now) n = selector.select(timeout - now);
          else n = selector.selectNow();
        } else n = selector.select();
        // System.out.println("Woke up from select()");

        // get an iterator over the set of selected keys
        Iterator it = selector.selectedKeys().iterator();
        // look at each key in the selected set
        while (it.hasNext()) {
          SelectionKey key = (SelectionKey) it.next();

          if (key.isAcceptable()) {
            logger.log(Level.FINE, "Got an inbound connection (key is acceptable)");
            ServerSocketChannel server = (ServerSocketChannel) key.channel();
            SocketChannel channel = server.accept();

            InetSocketAddress address =
                (InetSocketAddress) channel.socket().getRemoteSocketAddress();
            logger.log(Level.INFO, "Got an inbound connection from " + address.toString());
            if (!please_stop) {
              TCPConnection conn =
                  new TCPConnection(
                      TCPNode.this, settings.watchdogInterval(), settings.idleTimeout());
              conn.host_id = address.getAddress().getHostAddress();
              conn.state = Connection.State.connected_in;
              conn.channel = channel;
              channel.configureBlocking(false);
              channel.register(selector, SelectionKey.OP_READ, conn);

              registerInboundConnection(conn);

              // Devoteam Configure the CER/CEA sending
              boolean autoCERCEAEnable =
                  Config.getConfigByName("diameter.properties")
                      .getBoolean("capability.AUTO_CER_CEA_ENABLE", true);
              if (!autoCERCEAEnable) {
                getNode().sendCER(conn);
              }

            } else {
              // We don't want to add the connection if were are shutting down.
              channel.close();
            }
          } else if (key.isConnectable()) {
            logger.log(Level.FINE, "An outbound connection is ready (key is connectable)");
            SocketChannel channel = (SocketChannel) key.channel();
            TCPConnection conn = (TCPConnection) key.attachment();
            try {
              if (channel.finishConnect()) {
                logger.log(Level.FINEST, "Connected!");
                conn.state = Connection.State.connected_out;
                channel.register(selector, SelectionKey.OP_READ, conn);
                initiateCER(conn);
              }
            } catch (java.io.IOException ex) {
              logger.log(Level.WARNING, "Connection to '" + conn.host_id + "' failed", ex);
              try {
                channel.register(selector, 0);
                channel.close();
              } catch (java.io.IOException ex2) {
              }
              unregisterConnection(conn);
            }
          } else if (key.isReadable()) {
            logger.log(Level.FINEST, "Key is readable");
            // System.out.println("key is readable");
            SocketChannel channel = (SocketChannel) key.channel();
            TCPConnection conn = (TCPConnection) key.attachment();
            handleReadable(conn);
            if (conn.state != Connection.State.closed && conn.hasNetOutput())
              channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn);
          } else if (key.isWritable()) {
            logger.log(Level.FINEST, "Key is writable");
            SocketChannel channel = (SocketChannel) key.channel();
            TCPConnection conn = (TCPConnection) key.attachment();
            synchronized (getLockObject()) {
              handleWritable(conn);
              if (conn.state != Connection.State.closed && conn.hasNetOutput())
                channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn);
            }
          }

          // remove key from selected set, it's been handled
          it.remove();
        }

        runTimers();
      }

      // Remaining connections are close by Node instance

      // selector is closed in stop()
    }
Beispiel #2
0
 public Config getConfig() throws Exception {
   return Config.getConfigByName("snmp.properties");
 }