예제 #1
0
  /**
   * Update handles announcing an event to a group of peers
   *
   * @param event the event we are announcing
   * @param manager the manager
   * @return arraylist the arraylist of updated peers
   */
  @SuppressWarnings("unchecked")
  public ArrayList<Peer> update(String event, Manager manager) {
    this.event = event;
    this.requestString = createURL(this.announce);

    HashMap<ByteBuffer, Object> response = null;

    try {
      byte[] trackerResponse = sendGETRecieveResponse();

      if (trackerResponse == null) {
        log.severe("Error communicating with tracker");
        try {
          manager.close();
        } catch (IOException e) {
          log.severe("Error closing manager after miscommunication with tracker");
        }
        return null;
      }
      response = (HashMap<ByteBuffer, Object>) Bencoder2.decode(trackerResponse);
    } catch (BencodingException e1) {
      log.severe("Error decoding tracker response");
      try {
        manager.close();
      } catch (IOException e) {
        log.severe("Error closing manager after miscommunication with tracker");
      }
      return null;
    }

    if (response.containsKey(KEY_FAILURE)) {
      log.severe("Uh-oh. Failure from the tracker!");
      try {
        manager.close();
      } catch (IOException e) {
        log.severe("Error closing manager after miscommunication with tracker");
      }
      return null;
    }

    ArrayList<Peer> peers = new ArrayList<Peer>();

    this.interval = (Integer) response.get(KEY_INTERVAL);

    List<Map<ByteBuffer, Object>> peersList =
        (List<Map<ByteBuffer, Object>>) response.get(KEY_PEERS);
    // TODO: Check if peersList is null!

    if (peersList == null) {
      log.severe("Error: list of peers given by tracker is null");
      return null;
    }

    for (Map<ByteBuffer, Object> rawPeer : peersList) {
      int peerPort = ((Integer) rawPeer.get(KEY_PORT)).intValue();
      byte[] peerId = ((ByteBuffer) rawPeer.get(KEY_PEERID)).array();
      String ip = null;
      try {
        ip = new String(((ByteBuffer) rawPeer.get(KEY_IP)).array(), "ASCII");
      } catch (UnsupportedEncodingException e) {
        log.severe("Unable to parse encoding");
        continue;
      }

      peers.add(new Peer(peerId, peerPort, ip, manager));
    }

    log.fine("Converted: " + (peers));

    return peers;
  }
  public static void main(String[] args) {

    // boolean ready = false;
    // final Vector<Phidget> attachedPhidgets = new Vector<Phidget>();
    Manager mgr;

    try {
      mgr = new Manager();
      mgr.open();
      /*
       * With listeners...
       */
      mgr.addAttachListener(
          new AttachListener() {
            @Override
            public void attached(AttachEvent ae) {
              try {
                System.out.println(
                    "Phidget attached: "
                        + ae.getSource().getDeviceName()
                        + " sn="
                        + ae.getSource().getSerialNumber());
              } catch (PhidgetException e) {
                e.printStackTrace();
              }
            }
          });

      mgr.addDetachListener(
          new DetachListener() {
            @Override
            public void detached(DetachEvent de) {
              try {
                System.out.print(de.getSource().getSerialNumber() + " detached.");
              } catch (PhidgetException e) {
                e.printStackTrace();
              }
            }
          });

      while (true) {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          mgr.close();
        }
      }
      /*
       * Without listeners...
       */
      // while(!ready){
      // if(!mgr.getPhidgets().isEmpty()){
      // // get and create the phidgets
      // Vector currPhidgets = mgr.getPhidgets();
      // for(int i = 0; i < currPhidgets.size(); i++){
      // Phidget p = (Phidget) currPhidgets.elementAt(i);
      // int sn = p.getSerialNumber();
      // p.open(sn);
      // p.waitForAttachment();
      // attachedPhidgets.addElement(p);
      // System.out.println(p.getDeviceName() + ": " + sn + " attached.");
      // }
      // ready = true;
      // }
      //
      // try {
      // Thread.sleep(500);
      // } catch (InterruptedException e) {
      // e.printStackTrace();
      // }
      //
      // }
      // System.out.println("All done!");
      // for(Phidget ap : attachedPhidgets){
      // ap.close();
      // }
      // mgr.close();
    } catch (PhidgetException e1) {
      e1.printStackTrace();
    }

    System.exit(0);
  }