Beispiel #1
0
 /**
  * Set the options this this record. Container records should have the last nibble set to 0xF.
  *
  * <p
  * Note that <code>options</code> is an internal field. Use {@link #getInstance()} and
  * {@link #getVersion()} to access actual fields.
  */
 @Internal
 public void setOptions(short options) {
   // call to handle correct/incorrect values
   setVersion(fVersion.getShortValue(options));
   setInstance(fInstance.getShortValue(options));
   _options = options;
 }
Beispiel #2
0
 public void testIsAllSet() {
   for (int j = 0; j < 0x3F80; j += 0x80) {
     assertTrue(!bf_multi.isAllSet(j));
   }
   assertTrue(bf_multi.isAllSet(0x3F80));
   assertTrue(!bf_single.isAllSet(0));
   assertTrue(bf_single.isAllSet(0x4000));
 }
Beispiel #3
0
 /**
  * Switch from magnet mode to normal mode. If we already have the metainfo, this does nothing.
  *
  * @param meta non-null
  * @since 0.8.4
  */
 public void setMetaInfo(MetaInfo meta) {
   if (metainfo != null) return;
   BitField oldBF = bitfield;
   if (oldBF != null) {
     if (oldBF.size() != meta.getPieces())
       // fix bitfield, it was too big by 1-7 bits
       bitfield = new BitField(oldBF.getFieldBytes(), meta.getPieces());
     // else no extra
   } else {
     // it will be initialized later
     // bitfield = new BitField(meta.getPieces());
   }
   metainfo = meta;
   if (bitfield != null && bitfield.count() > 0) setInteresting(true);
 }
Beispiel #4
0
  void bitfieldMessage(byte[] bitmap) {
    synchronized (this) {
      if (_log.shouldLog(Log.DEBUG)) _log.debug(peer + " rcv bitfield");
      if (bitfield != null) {
        // XXX - Be liberal in what you accept?
        if (_log.shouldLog(Log.WARN)) _log.warn("Got unexpected bitfield message from " + peer);
        return;
      }

      // XXX - Check for weird bitfield and disconnect?
      // FIXME will have to regenerate the bitfield after we know exactly
      // how many pieces there are, as we don't know how many spare bits there are.
      if (metainfo == null) bitfield = new BitField(bitmap, bitmap.length * 8);
      else bitfield = new BitField(bitmap, metainfo.getPieces());
    }
    if (metainfo == null) return;
    boolean interest = listener.gotBitField(peer, bitfield);
    setInteresting(interest);
    if (bitfield.complete() && !interest) {
      // They are seeding and we are seeding,
      // why did they contact us? (robert)
      // Dump them quick before we send our whole bitmap
      if (_log.shouldLog(Log.WARN)) _log.warn("Disconnecting seed that connects to seeds: " + peer);
      peer.disconnect(true);
    }
  }
Beispiel #5
0
 @Override
 public void onBitfield(BitField receivedBitField) {
   if (logger.isDebugEnabled()) {
     String type = receivedBitField.hasBitsUnset() ? "LEECHER" : "SEEDER";
     logger.debug("Bitfield received from peer {} ({})", PeerConnection.this, type);
   }
   if (!handshakeReceived) {
     throw new TorrentProtocolException("no handshake received before bitfield");
   }
   bitField.get().add(receivedBitField);
   seeder = !bitField.get().hasBitsUnset();
 }
Beispiel #6
0
 public void testSetLargeValues() {
   final BitField bf1 = new BitField(0xF), bf2 = new BitField(0xF0000000);
   int a = 0;
   a = bf1.setValue(a, 9);
   a = bf2.setValue(a, 9);
   assertEquals(9, bf1.getValue(a));
   assertEquals(9, bf2.getValue(a));
 }
Beispiel #7
0
  public void testSetValue() {
    for (int j = 0; j < 128; j++) {
      assertEquals(bf_multi.getValue(bf_multi.setValue(0, j)), j);
      assertEquals(bf_multi.setValue(0, j), j << 7);
    }

    // verify that excess bits are stripped off
    assertEquals(bf_multi.setValue(0x3f80, 128), 0);
    for (int j = 0; j < 2; j++) {
      assertEquals(bf_single.getValue(bf_single.setValue(0, j)), j);
      assertEquals(bf_single.setValue(0, j), j << 14);
    }

    // verify that excess bits are stripped off
    assertEquals(bf_single.setValue(0x4000, 2), 0);
  }
Beispiel #8
0
  public void testSetShortValue() {
    for (int j = 0; j < 128; j++) {
      assertEquals(bf_multi.getShortValue(bf_multi.setShortValue((short) 0, (short) j)), (short) j);
      assertEquals(bf_multi.setShortValue((short) 0, (short) j), (short) (j << 7));
    }

    // verify that excess bits are stripped off
    assertEquals(bf_multi.setShortValue((short) 0x3f80, (short) 128), (short) 0);
    for (int j = 0; j < 2; j++) {
      assertEquals(
          bf_single.getShortValue(bf_single.setShortValue((short) 0, (short) j)), (short) j);
      assertEquals(bf_single.setShortValue((short) 0, (short) j), (short) (j << 14));
    }

    // verify that excess bits are stripped off
    assertEquals(bf_single.setShortValue((short) 0x4000, (short) 2), (short) 0);
  }
Beispiel #9
0
  void haveMessage(int piece) {
    if (_log.shouldLog(Log.DEBUG)) _log.debug(peer + " rcv have(" + piece + ")");
    // FIXME we will lose these until we get the metainfo
    if (metainfo == null) return;
    // Sanity check
    if (piece < 0 || piece >= metainfo.getPieces()) {
      // XXX disconnect?
      if (_log.shouldLog(Log.WARN))
        _log.warn("Got strange 'have: " + piece + "' message from " + peer);
      return;
    }

    synchronized (this) {
      // Can happen if the other side never send a bitfield message.
      if (bitfield == null) bitfield = new BitField(metainfo.getPieces());

      bitfield.set(piece);
    }

    if (listener.gotHave(peer, piece)) setInteresting(true);
  }
Beispiel #10
0
 public void testSetShort() {
   assertEquals(bf_multi.setShort((short) 0), (short) 0x3F80);
   assertEquals(bf_single.setShort((short) 0), (short) 0x4000);
 }
Beispiel #11
0
 /** Sets the shadow field value. display a shadow for the chart */
 public void setShadow(boolean value) {
   field_1_formatFlags = shadow.setShortBoolean(field_1_formatFlags, value);
 }
Beispiel #12
0
 /**
  * display a shadow for the chart
  *
  * @return the shadow field value.
  */
 public boolean isShadow() {
   return shadow.isSet(field_1_formatFlags);
 }
Beispiel #13
0
 public void testGetShortRawValue() {
   assertEquals(bf_multi.getShortRawValue((short) -1), (short) 0x3F80);
   assertEquals(bf_multi.getShortRawValue((short) 0), (short) 0);
   assertEquals(bf_single.getShortRawValue((short) -1), (short) 0x4000);
   assertEquals(bf_single.getShortRawValue((short) 0), (short) 0);
 }
Beispiel #14
0
 public void testGetRawValue() {
   assertEquals(bf_multi.getRawValue(-1), 0x3F80);
   assertEquals(bf_multi.getRawValue(0), 0);
   assertEquals(bf_single.getRawValue(-1), 0x4000);
   assertEquals(bf_single.getRawValue(0), 0);
 }
Beispiel #15
0
 public void testGetShortValue() {
   assertEquals(bf_multi.getShortValue((short) -1), (short) 127);
   assertEquals(bf_multi.getShortValue((short) 0), (short) 0);
   assertEquals(bf_single.getShortValue((short) -1), (short) 1);
   assertEquals(bf_single.getShortValue((short) 0), (short) 0);
 }
Beispiel #16
0
 /**
  * results displayed as percentages
  *
  * @return the display as percentage field value.
  */
 public boolean isDisplayAsPercentage() {
   return displayAsPercentage.isSet(field_1_formatFlags);
 }
Beispiel #17
0
 /**
  * Sets the version part of record
  *
  * @param value version part value
  */
 public void setVersion(short value) {
   _options = fVersion.setShortValue(_options, value);
 }
Beispiel #18
0
 /**
  * Sets the instance part of record
  *
  * @param value instance part value
  */
 public void setInstance(short value) {
   _options = fInstance.setShortValue(_options, value);
 }
 /** Sets the custom number format field value. true if this object has a custom number format */
 public void setCustomNumberFormat(boolean value) {
   field_3_options = customNumberFormat.setShortBoolean(field_3_options, value);
 }
Beispiel #20
0
 public void testSet() {
   assertEquals(bf_multi.set(0), 0x3F80);
   assertEquals(bf_single.set(0), 0x4000);
 }
Beispiel #21
0
 public void testClearShort() {
   assertEquals(bf_multi.clearShort((short) -1), (short) 0xC07F);
   assertEquals(bf_single.clearShort((short) -1), (short) 0xBFFF);
 }
Beispiel #22
0
 public void testClear() {
   assertEquals(bf_multi.clear(-1), 0xFFFFC07F);
   assertEquals(bf_single.clear(-1), 0xFFFFBFFF);
 }
 /**
  * true if this object has a custom number format
  *
  * @return the custom number format field value.
  */
 public boolean isCustomNumberFormat() {
   return customNumberFormat.isSet(field_3_options);
 }
Beispiel #24
0
 public void testSetBoolean() {
   assertEquals(bf_multi.set(0), bf_multi.setBoolean(0, true));
   assertEquals(bf_single.set(0), bf_single.setBoolean(0, true));
   assertEquals(bf_multi.clear(-1), bf_multi.setBoolean(-1, false));
   assertEquals(bf_single.clear(-1), bf_single.setBoolean(-1, false));
 }
Beispiel #25
0
 /**
  * Returns the instance part of the option record.
  *
  * @return The instance part of the record
  */
 public short getInstance() {
   return fInstance.getShortValue(_options);
 }
Beispiel #26
0
 public void testSetShortBoolean() {
   assertEquals(bf_multi.setShort((short) 0), bf_multi.setShortBoolean((short) 0, true));
   assertEquals(bf_single.setShort((short) 0), bf_single.setShortBoolean((short) 0, true));
   assertEquals(bf_multi.clearShort((short) -1), bf_multi.setShortBoolean((short) -1, false));
   assertEquals(bf_single.clearShort((short) -1), bf_single.setShortBoolean((short) -1, false));
 }
Beispiel #27
0
 /**
  * Returns the version part of the option record.
  *
  * @return The version part of the option record
  */
 public short getVersion() {
   return fVersion.getShortValue(_options);
 }
Beispiel #28
0
 public void testGetValue() {
   assertEquals(bf_multi.getValue(-1), 127);
   assertEquals(bf_multi.getValue(0), 0);
   assertEquals(bf_single.getValue(-1), 1);
   assertEquals(bf_single.getValue(0), 0);
 }
Beispiel #29
0
 /**
  * Read the options field from header and return instance part of it.
  *
  * @param data the byte array to read from
  * @param offset the offset to start reading from
  * @return value of instance part of options field
  */
 protected static short readInstance(byte data[], int offset) {
   final short options = LittleEndian.getShort(data, offset);
   return fInstance.getShortValue(options);
 }
Beispiel #30
0
 /** Sets the display as percentage field value. results displayed as percentages */
 public void setDisplayAsPercentage(boolean value) {
   field_1_formatFlags = displayAsPercentage.setShortBoolean(field_1_formatFlags, value);
 }