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);
    }
  }
Exemplo n.º 2
0
    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);
      }
    }
Exemplo n.º 3
0
  public ShowComp() throws InterruptedException, IOException {
    super("CONNECTED COMPUTERS");
    int x = 0, d = 20;
    mb = new JMenuBar();
    File = new JMenu("File");
    mb.add(File);
    exit = new JMenuItem("Exit");
    exit.addActionListener(this);
    File.add(exit);
    ta = new JTextArea();
    ta.setBounds(20, 30, 315, 470);
    ta.setEditable(false);
    add(ta);

    setJMenuBar(mb);

    sel = new JLabel("The connected computers are..");
    sel.setBounds(15, 5, 300, 30);
    add(sel);
    b1 = new JButton("<< BACK");
    b1.setBounds(140, 510, 100, 30);
    b1.setToolTipText("Back to main page");
    b1.addActionListener(this);
    add(b1);
    setLayout(null);
    while (x < 360) {
      x = x + d;
      setBounds(675, 50, x, 600);
      this.show();
    }
    // setVisible(true);
    String s = "192.168.0.", temp = null;
    Printer printer = new Printer();
    printer.start();
    Connector connector = new Connector(printer);
    connector.start();

    LinkedList targets = new LinkedList();
    for (int i = 1; i <= 255; i++) {
      temp = s + Integer.toString(i);
      Target t = new Target(temp);
      targets.add(t);
      connector.add(t);
    }
    Thread.sleep(2000);
    connector.shutdown();
    connector.join();

    for (Iterator i = targets.iterator(); i.hasNext(); ) {
      Target t = (Target) i.next();
      if (!t.shown) t.show();
    }

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  }
Exemplo n.º 4
0
 public void run() {
   try {
     for (; ; ) {
       Target t = null;
       synchronized (pending) {
         while (pending.size() == 0) pending.wait();
         t = (Target) pending.removeFirst();
       }
       t.show();
     }
   } catch (InterruptedException x) {
     return;
   }
 }
 public void printFinishedTargets() {
   // 打印finisedTargets队列中的任务
   try {
     for (; ; ) {
       Target target = null;
       synchronized (finishedTargets) {
         while (finishedTargets.size() == 0) finishedTargets.wait();
         target = (Target) finishedTargets.removeFirst();
       }
       target.show();
     }
   } catch (InterruptedException x) {
     return;
   }
 }
Exemplo n.º 6
0
    void processPendingTargets() throws IOException {
      synchronized (pending) {
        while (pending.size() > 0) {
          Target t = (Target) pending.removeFirst();
          try {

            t.channel.register(sel, SelectionKey.OP_CONNECT, t);

          } catch (IOException x) {

            t.channel.close();
            t.failure = x;
            printer.add(t);
          }
        }
      }
    }
  public void registerTargets() {
    // 取出targets队列中的任务,向Selector注册连接就绪事件
    synchronized (targets) {
      while (targets.size() > 0) {
        Target target = (Target) targets.removeFirst();

        try {
          target.channel.register(selector, SelectionKey.OP_CONNECT, target);
        } catch (IOException x) {
          try {
            target.channel.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
          target.failure = x;
          addFinishedTarget(target);
        }
      }
    }
  }
  public void processSelectedKeys() throws IOException {
    // 处理连接就绪事件
    for (Iterator it = selector.selectedKeys().iterator(); it.hasNext(); ) {
      SelectionKey selectionKey = (SelectionKey) it.next();
      it.remove();

      Target target = (Target) selectionKey.attachment();
      SocketChannel socketChannel = (SocketChannel) selectionKey.channel();

      try {
        if (socketChannel.finishConnect()) {
          selectionKey.cancel();
          target.connectFinish = System.currentTimeMillis();
          socketChannel.close();
          addFinishedTarget(target);
        }
      } catch (IOException x) {
        socketChannel.close();
        target.failure = x;
        addFinishedTarget(target);
      }
    }
  }
Exemplo n.º 9
0
    void processSelectedKeys() throws IOException {
      for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) {

        SelectionKey sk = (SelectionKey) i.next();
        i.remove();

        Target t = (Target) sk.attachment();
        SocketChannel sc = (SocketChannel) sk.channel();

        try {
          if (sc.finishConnect()) {
            sk.cancel();
            t.connectFinish = System.currentTimeMillis();
            sc.close();
            printer.add(t);
          }
        } catch (IOException x) {
          sc.close();
          t.failure = x;
          printer.add(t);
        }
      }
    }