Changeset 690aea2


Ignore:
Timestamp:
Dec 21, 2010 11:43:13 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
9734074
Parents:
f15b329
Message:
  • Rework traffic counters
  • Record the metadata bandwidth
  • More null announce handling
  • Callbacks for got MetaInfo? event
  • Cleanups
Location:
apps/i2psnark/java/src/org/klomp/snark
Files:
7 edited

Legend:

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

    rf15b329 r690aea2  
    3232  void peerChange(PeerCoordinator coordinator, Peer peer);
    3333
     34  /**
     35   * Called when the PeerCoordinator got the MetaInfo via magnet.
     36   * @since 0.8.4
     37   */
     38  void gotMetaInfo(PeerCoordinator coordinator, MetaInfo metainfo);
     39
    3440  public boolean overUploadLimit(int uploaders);
    3541  public boolean overUpBWLimit();
  • apps/i2psnark/java/src/org/klomp/snark/ExtensionHandler.java

    rf15b329 r690aea2  
    2727    public static final int ID_METADATA = 3;
    2828    private static final String TYPE_METADATA = "ut_metadata";
    29     private static final int MAX_METADATA_SIZE = Storage.MAX_PIECES * 32 * 5 / 4;
     29    /** Pieces * SHA1 Hash length, + 25% extra for file names, benconding overhead, etc */
     30    private static final int MAX_METADATA_SIZE = Storage.MAX_PIECES * 20 * 5 / 4;
    3031    private static final int PARALLEL_REQUESTS = 3;
    3132
     
    5051    }
    5152
    52     public static void handleMessage(Peer peer, int id, byte[] bs) {
     53    public static void handleMessage(Peer peer, PeerListener listener, int id, byte[] bs) {
    5354        if (id == 0)
    54             handleHandshake(peer, bs);
     55            handleHandshake(peer, listener, bs);
    5556        else if (id == ID_METADATA)
    56             handleMetadata(peer, bs);
     57            handleMetadata(peer, listener, bs);
    5758        else if (_log.shouldLog(Log.INFO))
    5859            _log.info("Unknown extension msg " + id + " from " + peer);
    5960    }
    6061
    61     private static void handleHandshake(Peer peer, byte[] bs) {
     62    private static void handleHandshake(Peer peer, PeerListener listener, byte[] bs) {
    6263        if (_log.shouldLog(Log.DEBUG))
    6364            _log.debug("Got handshake msg from " + peer);
     
    6869            BEValue bev = dec.bdecodeMap();
    6970            Map<String, BEValue> map = bev.getMap();
     71            peer.setHandshakeMap(map);
    7072            Map<String, BEValue> msgmap = map.get("m").getMap();
    71             peer.setHandshakeMap(map);
    72 
    73             // not used, just to throw out of here
    74             int hisMsgCode = peer.getHandshakeMap().get("m").getMap().get(TYPE_METADATA).getInt();
     73
     74            // rv not used, just to throw an NPE to get out of here
     75            msgmap.get(TYPE_METADATA).getInt();
    7576
    7677            int metaSize = map.get("metadata_size").getInt();
     
    122123
    123124    private static final int CHUNK_SIZE = 16*1024;
    124     /** 25% extra for file names, benconding overhead, etc */
    125125
    126126    /**
     
    128128     * @since 0.8.4
    129129     */
    130     private static void handleMetadata(Peer peer, byte[] bs) {
     130    private static void handleMetadata(Peer peer, PeerListener listener, byte[] bs) {
    131131        if (_log.shouldLog(Log.DEBUG))
    132132            _log.debug("Got metadata msg from " + peer);
     
    146146                }
    147147                sendPiece(peer, piece, pc);
     148                // Do this here because PeerConnectionOut only reports for PIECE messages
     149                peer.uploaded(pc.length);
     150                listener.uploaded(peer, pc.length);
    148151            } else if (type == TYPE_DATA) {
    149152                int size = map.get("total_size").getInt();
     
    154157                        return;
    155158                    int len = is.available();
     159                    peer.downloaded(len);
     160                    listener.downloaded(peer, len);
    156161                    done = state.saveChunk(piece, bs, bs.length - len, len);
    157162                    if (_log.shouldLog(Log.INFO))
     
    190195
    191196    private static void sendRequest(Peer peer, int piece) {
     197        sendMessage(peer, TYPE_REQUEST, piece);
     198    }
     199
     200    private static void sendReject(Peer peer, int piece) {
     201        sendMessage(peer, TYPE_REJECT, piece);
     202    }
     203
     204    /** REQUEST and REJECT are the same except for message type */
     205    private static void sendMessage(Peer peer, int type, int piece) {
    192206        Map<String, Object> map = new HashMap();
    193         map.put("msg_type", TYPE_REQUEST);
     207        map.put("msg_type", Integer.valueOf(type));
    194208        map.put("piece", Integer.valueOf(piece));
    195209        byte[] payload = BEncoder.bencode(map);
     
    206220    private static void sendPiece(Peer peer, int piece, byte[] data) {
    207221        Map<String, Object> map = new HashMap();
    208         map.put("msg_type", TYPE_REQUEST);
     222        map.put("msg_type", Integer.valueOf(TYPE_REQUEST));
    209223        map.put("piece", Integer.valueOf(piece));
    210224        map.put("total_size", Integer.valueOf(data.length));
  • apps/i2psnark/java/src/org/klomp/snark/Peer.java

    rf15b329 r690aea2  
    5353  private DataInputStream din;
    5454  private DataOutputStream dout;
     55
     56  /** running counters */
     57  private long downloaded;
     58  private long uploaded;
    5559
    5660  // Keeps state for in/out connections.  Non-null when the handshake
     
    411415   *  @since 0.8.4
    412416   */
    413   public void gotMetaInfo(MetaInfo meta) {
    414     PeerState s = state;
    415     if (s != null)
    416         s.gotMetaInfo(meta);
     417  public void setMetaInfo(MetaInfo meta) {
     418    PeerState s = state;
     419    if (s != null)
     420        s.setMetaInfo(meta);
    417421  }
    418422
     
    579583
    580584  /**
     585   * Increment the counter.
     586   * @since 0.8.4
     587   */
     588  public void downloaded(int size) {
     589      downloaded += size;
     590  }
     591
     592  /**
     593   * Increment the counter.
     594   * @since 0.8.4
     595   */
     596  public void uploaded(int size) {
     597      uploaded += size;
     598  }
     599
     600  /**
    581601   * Returns the number of bytes that have been downloaded.
    582602   * Can be reset to zero with <code>resetCounters()</code>/
     
    584604  public long getDownloaded()
    585605  {
    586     PeerState s = state;
    587     return (s != null) ? s.downloaded : 0;
     606      return downloaded;
    588607  }
    589608
     
    594613  public long getUploaded()
    595614  {
    596     PeerState s = state;
    597     return (s != null) ? s.uploaded : 0;
     615      return uploaded;
    598616  }
    599617
     
    603621  public void resetCounters()
    604622  {
    605     PeerState s = state;
    606     if (s != null)
    607       {
    608         s.downloaded = 0;
    609         s.uploaded = 0;
    610       }
     623      downloaded = 0;
     624      uploaded = 0;
    611625  }
    612626 
  • apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java

    rf15b329 r690aea2  
    4646  /**
    4747   * External use by PeerMonitorTask only.
    48    */
    49   final MetaInfo metainfo;
     48   * Will be null when in magnet mode.
     49   */
     50  MetaInfo metainfo;
    5051
    5152  /**
    5253   * External use by PeerMonitorTask only.
    53    */
    54   final Storage storage;
     54   * Will be null when in magnet mode.
     55   */
     56  Storage storage;
    5557  private final Snark snark;
    5658
     
    11511153  }
    11521154
    1153   /** @since 0.8.4 */
     1155  /**
     1156   *  PeerListener callback
     1157   *  @since 0.8.4
     1158   */
    11541159  public void gotExtension(Peer peer, int id, byte[] bs) {
    11551160      if (_log.shouldLog(Log.DEBUG))
     
    11611166                  if (_log.shouldLog(Log.WARN))
    11621167                      _log.warn("Got completed metainfo via extension");
    1163                   MetaInfo newinfo = magnetState.getMetaInfo();
    1164                   // more validation
    1165                   // set global
    1166                   // instantiate storage
    1167                   // tell Snark listener
    1168                   // tell all peers
     1168                  metainfo = magnetState.getMetaInfo();
     1169                  listener.gotMetaInfo(this, metainfo);
     1170                  for (Peer p : peers) {
     1171                      p.setMetaInfo(metainfo);
     1172                  }
    11691173              }
    11701174          }
     
    11721176  }
    11731177
    1174   /** @since 0.8.4 */
     1178  /**
     1179   *  Sets the storage after transition out of magnet mode
     1180   *  Snark calls this after we call gotMetaInfo()
     1181   *  @since 0.8.4
     1182   */
     1183  public void setStorage(Storage stg) {
     1184      storage = stg;
     1185  }
     1186
     1187  /**
     1188   *  PeerListener callback
     1189   *  @since 0.8.4
     1190   */
    11751191  public void gotPort(Peer peer, int port) {
    11761192      // send to DHT
  • apps/i2psnark/java/src/org/klomp/snark/PeerState.java

    rf15b329 r690aea2  
    3737  private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(PeerState.class);
    3838  private final Peer peer;
     39  /** Fixme, used by Peer.disconnect() to get to the coordinator */
    3940  final PeerListener listener;
    40   private final MetaInfo metainfo;
     41  private MetaInfo metainfo;
    4142
    4243  // Interesting and choking describes whether we are interested in or
     
    4950  boolean interested = false;
    5051  boolean choked = true;
    51 
    52   // Package local for use by Peer.
    53   long downloaded;
    54   long uploaded;
    5552
    5653  /** the pieces the peer has */
     
    286283  void uploaded(int size)
    287284  {
    288     uploaded += size;
     285    peer.uploaded(size);
    289286    listener.uploaded(peer, size);
    290287  }
     
    306303  {
    307304    int size = req.len;
    308     downloaded += size;
     305    peer.downloaded(size);
    309306    listener.downloaded(peer, size);
    310307
     
    327324            if (_log.shouldLog(Log.WARN))
    328325              _log.warn("Got BAD " + req.piece + " from " + peer);
    329             // XXX ARGH What now !?!
    330             // FIXME Why would we set downloaded to 0?
    331             downloaded = 0;
    332326          }
    333327      }
     
    373367                      + begin + ", " + length + "' received from "
    374368                      + peer);
    375         downloaded = 0; // XXX - punishment?
    376369        return null;
    377370      }
     
    398391                          + length + "' received from "
    399392                          + peer);
    400             downloaded = 0; // XXX - punishment?
    401393            return null;
    402394          }
     
    498490  void extensionMessage(int id, byte[] bs)
    499491  {
    500       ExtensionHandler.handleMessage(peer, id, bs);
     492      ExtensionHandler.handleMessage(peer, listener, id, bs);
    501493      // Peer coord will get metadata from MagnetState,
    502494      // verify, and then call gotMetaInfo()
     
    508500   *  @since 0.8.4
    509501   */
    510   public void gotMetaInfo(MetaInfo meta) {
    511       // set metainfo
    512       // fix bitfield
     502  public void setMetaInfo(MetaInfo meta) {
     503      BitField oldBF = bitfield;
     504      if (oldBF != null) {
     505          if (oldBF.size() != meta.getPieces())
     506              // fix bitfield, it was too big by 1-7 bits
     507              bitfield = new BitField(oldBF.getFieldBytes(), meta.getPieces());
     508          // else no extra
     509      } else {
     510          // it will be initialized later
     511          //bitfield = new BitField(meta.getPieces());
     512      }
     513      metainfo = meta;
    513514  }
    514515
  • apps/i2psnark/java/src/org/klomp/snark/Snark.java

    rf15b329 r690aea2  
    10051005  }
    10061006 
     1007  /**
     1008   * Called when the PeerCoordinator got the MetaInfo via magnet.
     1009   * CoordinatorListener.
     1010   * Create the storage, tell SnarkManager, and give the storage
     1011   * back to the coordinator.
     1012   *
     1013   * @throws RuntimeException via fatal()
     1014   * @since 0.8.4
     1015   */
     1016  public void gotMetaInfo(PeerCoordinator coordinator, MetaInfo metainfo) {
     1017      meta = metainfo;
     1018      try {
     1019          storage = new Storage(_util, meta, this);
     1020          if (completeListener != null)
     1021              completeListener.gotMetaInfo(this);
     1022          coordinator.setStorage(storage);
     1023      } catch (IOException ioe) {
     1024          if (storage != null) {
     1025              try { storage.close(); } catch (IOException ioee) {}
     1026          }
     1027          fatal("Could not check or create storage", ioe);
     1028      }
     1029  }
     1030
    10071031  private boolean allocating = false;
    10081032  public void storageCreateFile(Storage storage, String name, long length)
  • apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java

    rf15b329 r690aea2  
    574574                    if (!TrackerClient.isValidAnnounce(info.getAnnounce())) {
    575575                        if (_util.shouldUseOpenTrackers() && _util.getOpenTrackers() != null) {
    576                             addMessage(_("Warning - Ignoring non-i2p tracker in \"{0}\", will announce to i2p open trackers only", info.getName()));
     576                            addMessage(_("Warning - No I2P trackers in \"{0}\", will announce to I2P open trackers and DHT only.", info.getName()));
     577                        } else if (_util.getDHT() != null) {
     578                            addMessage(_("Warning - No I2P trackers in \"{0}\", and open trackers are disabled, will announce to DHT only.", info.getName()));
    577579                        } else {
    578                             addMessage(_("Warning - Ignoring non-i2p tracker in \"{0}\", and open trackers are disabled, you must enable open trackers before starting the torrent!", info.getName()));
     580                            addMessage(_("Warning - No I2P trackers in \"{0}\", and DHT and open trackers are disabled, you should enable open trackers or DHT before starting the torrent.", info.getName()));
    579581                            dontAutoStart = true;
    580582                        }
Note: See TracChangeset for help on using the changeset viewer.