Changeset afe5751


Ignore:
Timestamp:
Dec 19, 2010 3:37:11 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
4899a6d
Parents:
91f1ece
Message:

PORT msg support; port and extension listener stubs

Location:
apps/i2psnark/java/src/org/klomp/snark
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/Message.java

    r91f1ece rafe5751  
    5454  // Used for HAVE, REQUEST, PIECE and CANCEL messages.
    5555  // low byte used for EXTENSION message
     56  // low two bytes used for PORT message
    5657  int piece;
    5758
     
    108109      datalen += 1;
    109110
     111    if (type == PORT)
     112      datalen += 2;
     113
    110114    // add length of data for piece or bitfield array.
    111115    if (type == BITFIELD || type == PIECE || type == EXTENSION)
     
    130134    if (type == EXTENSION)
    131135        dos.writeByte((byte) piece & 0xff);
     136
     137    if (type == PORT)
     138        dos.writeShort(piece & 0xffff);
    132139
    133140    // Send actual data
  • apps/i2psnark/java/src/org/klomp/snark/Peer.java

    r91f1ece rafe5751  
    7373   */
    7474  public Peer(PeerID peerID, byte[] my_id, MetaInfo metainfo)
    75     throws IOException
    7675  {
    7776    this.peerID = peerID;
     
    254253        }
    255254
     255        if ((options & OPTION_DHT) != 0 && util.getDHT() != null) {
     256            if (_log.shouldLog(Log.DEBUG))
     257                _log.debug("Peer supports DHT, sending PORT message");
     258            int port = util.getDHT().getPort();
     259            out.sendPort(port);
     260        }
     261
    256262        // Send our bitmap
    257263        if (bitfield != null)
     
    304310    dout.write("BitTorrent protocol".getBytes("UTF-8"));
    305311    // Handshake write - options
    306     dout.writeLong(OPTION_EXTENSION);
     312    // FIXME not if DHT disabled
     313    dout.writeLong(OPTION_EXTENSION | OPTION_DHT);
    307314    // Handshake write - metainfo hash
    308315    byte[] shared_hash = metainfo.getInfoHash();
     
    344351
    345352    if (options != 0) {
    346         // send them something
     353        // send them something in runConnection() above
    347354        if (_log.shouldLog(Log.DEBUG))
    348355            _log.debug("Peer supports options 0x" + Long.toString(options, 16) + ": " + toString());
     
    350357
    351358    return bs;
     359  }
     360
     361  /** @since 0.8.4 */
     362  public long getOptions() {
     363      return options;
    352364  }
    353365
  • apps/i2psnark/java/src/org/klomp/snark/PeerConnectionIn.java

    r91f1ece rafe5751  
    172172                    _log.debug("Received cancel(" + piece + "," + begin + ") from " + peer + " on " + peer.metainfo.getName());
    173173                break;
     174              case 9:  // PORT message
     175                int port = din.readUnsignedShort();
     176                ps.portMessage(port);
     177                if (_log.shouldLog(Log.DEBUG))
     178                    _log.debug("Received port message from " + peer + " on " + peer.metainfo.getName());
    174179              case 20:  // Extension message
    175180                int id = din.readUnsignedByte();
  • apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java

    r91f1ece rafe5751  
    548548    addMessage(m);
    549549  }
     550
     551  /** @since 0.8.4 */
     552  void sendPort(int port) {
     553    Message m = new Message();
     554    m.type = Message.PORT;
     555    m.piece = port;
     556    addMessage(m);
     557  }
    550558}
  • apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java

    r91f1ece rafe5751  
    10871087  }
    10881088
     1089  /** @since 0.8.4 */
     1090  public void gotExtension(Peer peer, int id, byte[] bs) {}
     1091
     1092  /** @since 0.8.4 */
     1093  public void gotPort(Peer peer, int port) {}
     1094
    10891095  /** Return number of allowed uploaders for this torrent.
    10901096   ** Check with Snark to see if we are over the total upload limit.
  • apps/i2psnark/java/src/org/klomp/snark/PeerListener.java

    r91f1ece rafe5751  
    180180   */
    181181  PartialPiece getPartialPiece(Peer peer, BitField havePieces);
     182
     183  /**
     184   * Called when an extension message is received.
     185   *
     186   * @param peer the Peer that got the message.
     187   * @param id the message ID
     188   * @param bs the message payload
     189   * @since 0.8.4
     190   */
     191  void gotExtension(Peer peer, int id, byte[] bs);
     192
     193  /**
     194   * Called when an extension message is received.
     195   *
     196   * @param peer the Peer that got the message.
     197   * @param port the port
     198   * @since 0.8.4
     199   */
     200  void gotPort(Peer peer, int port);
    182201}
  • apps/i2psnark/java/src/org/klomp/snark/PeerState.java

    r91f1ece rafe5751  
    3232import net.i2p.I2PAppContext;
    3333import net.i2p.util.Log;
    34 
    35 import org.klomp.snark.bencode.BDecoder;
    36 import org.klomp.snark.bencode.BEValue;
    3734
    3835class PeerState implements DataLoader
     
    486483  void extensionMessage(int id, byte[] bs)
    487484  {
    488       if (id == 0) {
    489           InputStream is = new ByteArrayInputStream(bs);
    490           try {
    491               BDecoder dec = new BDecoder(is);
    492               BEValue bev = dec.bdecodeMap();
    493               Map map = bev.getMap();
    494               if (_log.shouldLog(Log.DEBUG))
    495                   _log.debug("Got extension handshake message " + bev.toString());
    496           } catch (Exception e) {
    497               if (_log.shouldLog(Log.DEBUG))
    498                   _log.debug("Failed extension decode", e);
    499           }
    500       } else {
    501           if (_log.shouldLog(Log.DEBUG))
    502               _log.debug("Got extended message type: " + id + " length: " + bs.length);
    503       }
     485      listener.gotExtension(peer, id, bs);
     486  }
     487
     488  /** @since 0.8.4 */
     489  void portMessage(int port)
     490  {
     491      listener.gotPort(peer, port);
    504492  }
    505493
Note: See TracChangeset for help on using the changeset viewer.