/**
   * Remove an outstanding piece data request.
   *
   * @param piece_number
   * @param piece_offset
   * @param length
   */
  public void removePieceRequest(int piece_number, int piece_offset, int length) {
    if (destroyed) return;

    DiskManagerReadRequest dmr =
        peer.getManager().getDiskManager().createReadRequest(piece_number, piece_offset, length);

    try {
      lock_mon.enter();

      if (requests.contains(dmr)) {
        requests.remove(dmr);
        return;
      }

      if (loading_messages.contains(dmr)) {
        loading_messages.remove(dmr);
        return;
      }

      for (Iterator i = queued_messages.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry entry = (Map.Entry) i.next();
        if (entry.getValue().equals(dmr)) { // it's already been queued
          BTPiece msg = (BTPiece) entry.getKey();
          if (outgoing_message_queue.removeMessage(msg, true)) {
            i.remove();
          }
          break; // do manual listener notify
        }
      }
    } finally {
      lock_mon.exit();
    }

    outgoing_message_queue.doListenerNotifications();
  }
  private void sendPendingHaves() {
    if (destroyed) {

      return;
    }

    try {
      pending_haves_mon.enter();

      int num_haves = pending_haves.size();

      if (num_haves == 0) {

        return;
      }

      // single have -> use BT

      if (num_haves == 1 || az_have_version < BTMessageFactory.MESSAGE_VERSION_SUPPORTS_PADDING) {

        for (int i = 0; i < num_haves; i++) {

          Integer piece_num = (Integer) pending_haves.get(i);

          outgoing_message_q.addMessage(new BTHave(piece_num.intValue(), bt_have_version), true);
        }
      } else {

        int[] piece_numbers = new int[num_haves];

        for (int i = 0; i < num_haves; i++) {

          piece_numbers[i] = ((Integer) pending_haves.get(i)).intValue();
        }

        outgoing_message_q.addMessage(new AZHave(piece_numbers, az_have_version), true);
      }

      outgoing_message_q.doListenerNotifications();

      pending_haves.clear();

    } finally {

      pending_haves_mon.exit();
    }
  }
  /**
   * Create a new aggregator, which will send messages out the given queue.
   *
   * @param outgoing_message_q
   */
  public OutgoingBTHaveMessageAggregator(
      OutgoingMessageQueue outgoing_message_q, byte _bt_have_version, byte _az_have_version) {
    this.outgoing_message_q = outgoing_message_q;
    bt_have_version = _bt_have_version;
    az_have_version = _az_have_version;

    outgoing_message_q.registerQueueListener(added_message_listener);
  }
  /** Remove all outstanding piece data requests. */
  public void removeAllPieceRequests() {
    if (destroyed) return;

    try {
      lock_mon.enter();

      // removed this trace as Alon can't remember why the trace is here anyway and as far as I can
      // see there's nothing to stop a piece being delivered to transport and removed from
      // the message queue before we're notified of this and thus it is entirely possible that
      // our view of queued messages is lagging.
      // String before_trace = outgoing_message_queue.getQueueTrace();
      /*
      int num_queued = queued_messages.size();
      int num_removed = 0;

      for( Iterator i = queued_messages.keySet().iterator(); i.hasNext(); ) {
        BTPiece msg = (BTPiece)i.next();
        if( outgoing_message_queue.removeMessage( msg, true ) ) {
          i.remove();
          num_removed++;
        }
      }

      if( num_removed < num_queued -2 ) {
        Debug.out( "num_removed[" +num_removed+ "] < num_queued[" +num_queued+ "]:\nBEFORE:\n" +before_trace+ "\nAFTER:\n" +outgoing_message_queue.getQueueTrace() );
      }
      */

      for (Iterator i = queued_messages.keySet().iterator(); i.hasNext(); ) {
        BTPiece msg = (BTPiece) i.next();
        outgoing_message_queue.removeMessage(msg, true);
      }

      queued_messages.clear(); // this replaces stuff above
      requests.clear();
      loading_messages.clear();
    } finally {
      lock_mon.exit();
    }

    outgoing_message_queue.doListenerNotifications();
  }
        public void readCompleted(DiskManagerReadRequest request, DirectByteBuffer data) {
          try {
            lock_mon.enter();

            if (!loading_messages.contains(request) || destroyed) { // was canceled
              data.returnToPool();
              return;
            }
            loading_messages.remove(request);

            BTPiece msg =
                new BTPiece(request.getPieceNumber(), request.getOffset(), data, piece_version);
            queued_messages.put(msg, request);

            outgoing_message_queue.addMessage(msg, true);
          } finally {
            lock_mon.exit();
          }

          outgoing_message_queue.doListenerNotifications();
        }
  /**
   * Create a new handler for outbound piece messages, reading piece data from the given disk
   * manager and transmitting the messages out the given message queue.
   *
   * @param disk_manager
   * @param outgoing_message_q
   */
  public OutgoingBTPieceMessageHandler(
      PEPeer _peer,
      OutgoingMessageQueue _outgoing_message_q,
      OutgoingBTPieceMessageHandlerAdapter _adapter,
      byte _piece_version) {
    peer = _peer;
    outgoing_message_queue = _outgoing_message_q;
    adapter = _adapter;
    piece_version = _piece_version;

    outgoing_message_queue.registerQueueListener(sent_message_listener);
  }
  public void destroy() {
    try {
      lock_mon.enter();

      removeAllPieceRequests();

      queued_messages.clear();

      destroyed = true;

      outgoing_message_queue.cancelQueueListener(sent_message_listener);
    } finally {
      lock_mon.exit();
    }
  }
  /**
   * Queue a new have message for aggregated sending.
   *
   * @param piece_number of the have message
   * @param force if true, send this and any other pending haves right away
   */
  public void queueHaveMessage(int piece_number, boolean force) {
    if (destroyed) return;

    try {
      pending_haves_mon.enter();

      pending_haves.add(new Integer(piece_number));
      if (force) {
        sendPendingHaves();
      } else {
        int pending_bytes = pending_haves.size() * 9;
        if (pending_bytes >= outgoing_message_q.getMssSize()) {
          // System.out.println("enough pending haves for a full packet!");
          // there's enough pending bytes to fill a packet payload
          sendPendingHaves();
        }
      }
    } finally {

      pending_haves_mon.exit();
    }
  }