Ejemplo n.º 1
0
 private void handleWritable(Connection conn_) {
   TCPConnection conn = (TCPConnection) conn_;
   logger.log(Level.FINEST, "handleWritable():");
   ByteBuffer net_out_buffer = conn.connection_buffers.netOutBuffer();
   // int bytes = net_out_buffer.position();
   // net_out_buffer.rewind();
   // net_out_buffer.limit(bytes);
   net_out_buffer.flip();
   // logger.log(Level.FINEST,"                :bytes= " + bytes);
   int count;
   try {
     count = conn.channel.write(net_out_buffer);
     if (count < 0) {
       closeConnection(conn);
       return;
     }
     // conn.consumeNetOutBuffer(count);
     net_out_buffer.compact();
     conn.processAppOutBuffer();
     if (!conn.hasNetOutput()) conn.channel.register(selector, SelectionKey.OP_READ, conn);
   } catch (java.io.IOException ex) {
     closeConnection(conn);
     return;
   }
 }
Ejemplo n.º 2
0
 private void outputBecameAvailable(Connection conn_) {
   TCPConnection conn = (TCPConnection) conn_;
   handleWritable(conn);
   if (conn.hasNetOutput()) {
     try {
       conn.channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn);
     } catch (java.nio.channels.ClosedChannelException ex) {
     }
   }
 }
Ejemplo n.º 3
0
  void sendMessage(TCPConnection conn, byte[] raw) {
    boolean was_empty = !conn.hasNetOutput();
    conn.makeSpaceInAppOutBuffer(raw.length);
    // System.out.println("sendMessage: A: position=" + out_buffer.position() + " limit=" +
    // conn.out_buffer.limit());
    conn.connection_buffers.appOutBuffer().put(raw);
    conn.connection_buffers.processAppOutBuffer();
    // System.out.println("sendMessage: B: position=" + out_buffer.position() + " limit=" +
    // conn.out_buffer.limit());

    if (was_empty) outputBecameAvailable(conn);
  }
Ejemplo n.º 4
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()
    }