コード例 #1
0
ファイル: TCPNode.java プロジェクト: uttamhoode/mts-project
 boolean initiateConnection(Connection conn_, Peer peer) {
   TCPConnection conn = (TCPConnection) conn_;
   try {
     SocketChannel channel = SocketChannel.open();
     InetSocketAddress localAddress = new InetSocketAddress(conn.host_id, 0);
     channel.socket().bind(localAddress);
     channel.configureBlocking(false);
     try {
       InetSocketAddress remoteAddress = new InetSocketAddress(peer.host(), peer.port());
       if (channel.connect(remoteAddress)) {
         // This only happens on Solaris when connecting locally
         logger.log(Level.FINEST, "Connected!");
         conn.state = Connection.State.connected_out;
         conn.channel = channel;
         selector.wakeup();
         channel.register(selector, SelectionKey.OP_READ, conn);
         initiateCER(conn);
         return true;
       }
     } catch (java.nio.channels.UnresolvedAddressException ex) {
       channel.close();
       return false;
     }
     conn.state = Connection.State.connecting;
     conn.channel = channel;
     selector.wakeup();
     channel.register(selector, SelectionKey.OP_CONNECT, conn);
   } catch (java.io.IOException ex) {
     logger.log(
         Level.WARNING,
         "java.io.IOException caught while initiating connection to '" + peer.host() + "'.",
         ex);
   }
   return true;
 }
コード例 #2
0
  public void addTarget(Target target) {
    // 向targets队列中加入一个任务
    SocketChannel socketChannel = null;
    try {
      socketChannel = SocketChannel.open();
      socketChannel.configureBlocking(false);
      socketChannel.connect(target.address);

      target.channel = socketChannel;
      target.connectStart = System.currentTimeMillis();

      synchronized (targets) {
        targets.add(target);
      }
      selector.wakeup();
    } catch (Exception x) {
      if (socketChannel != null) {
        try {
          socketChannel.close();
        } catch (IOException xx) {
        }
      }
      target.failure = x;
      addFinishedTarget(target);
    }
  }
コード例 #3
0
ファイル: ShowComp.java プロジェクト: vishalshar/NetMon
    void add(Target t) {
      SocketChannel sc = null;
      try {

        sc = SocketChannel.open();
        sc.configureBlocking(false);

        boolean connected = sc.connect(t.address);

        t.channel = sc;
        t.connectStart = System.currentTimeMillis();

        if (connected) {
          t.connectFinish = t.connectStart;
          sc.close();
          printer.add(t);
        } else {
          synchronized (pending) {
            pending.add(t);
          }

          sel.wakeup();
        }
      } catch (IOException x) {
        if (sc != null) {
          try {
            sc.close();
          } catch (IOException xx) {
          }
        }
        t.failure = x;
        printer.add(t);
      }
    }
コード例 #4
0
    public void sendMessage(Address address, byte[] message) throws java.io.IOException {
      Socket s = null;
      SocketEntry entry = (SocketEntry) sockets.get(address);
      if (logger.isDebugEnabled()) {
        logger.debug("Looking up connection for destination '" + address + "' returned: " + entry);
        logger.debug(sockets.toString());
      }
      if (entry != null) {
        s = entry.getSocket();
      }
      if ((s == null) || (s.isClosed())) {
        if (logger.isDebugEnabled()) {
          logger.debug("Socket for address '" + address + "' is closed, opening it...");
        }
        SocketChannel sc = null;
        try {
          // Open the channel, set it to non-blocking, initiate connect
          sc = SocketChannel.open();
          sc.configureBlocking(false);
          sc.connect(
              new InetSocketAddress(
                  ((TcpAddress) address).getInetAddress(), ((TcpAddress) address).getPort()));
          s = sc.socket();
          entry = new SocketEntry((TcpAddress) address, s);
          entry.addMessage(message);
          sockets.put(address, entry);

          synchronized (pending) {
            pending.add(entry);
          }

          selector.wakeup();
          logger.debug("Trying to connect to " + address);
        } catch (IOException iox) {
          logger.error(iox);
          throw iox;
        }
      } else {
        entry.addMessage(message);
        synchronized (pending) {
          pending.add(entry);
        }
        selector.wakeup();
      }
    }
コード例 #5
0
  /*
   * CancelledKeyException is the failure symptom of 4729342
   * NOTE: The failure is timing dependent and is not always
   * seen immediately when the bug is present.
   */
  public static void main(String[] args) throws Exception {
    InetAddress lh = InetAddress.getLocalHost();
    isa = new InetSocketAddress(lh, TEST_PORT);
    selector = Selector.open();
    ssc = ServerSocketChannel.open();

    // Create and start a selector in a separate thread.
    new Thread(
            new Runnable() {
              public void run() {
                try {
                  ssc.configureBlocking(false);
                  ssc.socket().bind(isa);
                  sk = ssc.register(selector, SelectionKey.OP_ACCEPT);
                  selector.select();
                } catch (IOException e) {
                  System.err.println("error in selecting thread");
                  e.printStackTrace();
                }
              }
            })
        .start();

    // Wait for above thread to get to select() before we call close.
    Thread.sleep(3000);

    // Try to close. This should wakeup select.
    new Thread(
            new Runnable() {
              public void run() {
                try {
                  SocketChannel sc = SocketChannel.open();
                  sc.connect(isa);
                  ssc.close();
                  sk.cancel();
                  sc.close();
                } catch (IOException e) {
                  System.err.println("error in closing thread");
                  System.err.println(e);
                }
              }
            })
        .start();

    // Wait for select() to be awakened, which should be done by close.
    Thread.sleep(3000);

    selector.wakeup();
    selector.close();
  }
コード例 #6
0
 public void receiveTarget() {
   // 接收用户输入的地址,向targets队列中加入任务
   try {
     BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
     String msg = null;
     while ((msg = localReader.readLine()) != null) {
       if (!msg.equals("bye")) {
         Target target = new Target(msg);
         addTarget(target);
       } else {
         shutdown = true;
         selector.wakeup();
         break;
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
コード例 #7
0
ファイル: MDCServer.java プロジェクト: abil1/webgiisoo
  /** Close. */
  public void close() {
    if (selector != null) {
      selector.wakeup();
      try {
        selector.close();
      } catch (IOException e1) {
        log.warn("close selector fails", e1);
      } finally {
        selector = null;
      }
    }

    if (server != null) {
      try {
        server.socket().close();
        server.close();
      } catch (IOException e) {
        log.warn("close socket server fails", e);
      } finally {
        server = null;
      }
    }
  }
コード例 #8
0
ファイル: TCPNode.java プロジェクト: uttamhoode/mts-project
 void wakeup() {
   logger.log(Level.FINEST, "Waking up selector thread");
   selector.wakeup();
 }
コード例 #9
0
ファイル: ShowComp.java プロジェクト: vishalshar/NetMon
 void shutdown() {
   shutdown = true;
   sel.wakeup();
 }
コード例 #10
0
 public void signalLoopbreak() {
   loopBreaker.set(true);
   if (mySelector != null) mySelector.wakeup();
 }