Changeset 24dd783


Ignore:
Timestamp:
Oct 15, 2010 5:25:45 PM (10 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
1400c4d
Parents:
9afff4f
Message:

priority persistence

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

Legend:

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

    r9afff4f r24dd783  
    5555    public static final String PROP_META_PREFIX = "i2psnark.zmeta.";
    5656    public static final String PROP_META_BITFIELD_SUFFIX = ".bitfield";
     57    public static final String PROP_META_PRIORITY_SUFFIX = ".priority";
    5758
    5859    private static final String CONFIG_FILE = "i2psnark.config";
     
    511512                                            _peerCoordinatorSet, _connectionAcceptor,
    512513                                            false, dataDir.getPath());
     514                        loadSavedFilePriorities(torrent);
    513515                        torrent.completeListener = this;
    514516                        synchronized (_snarks) {
     
    589591   
    590592    /**
     593     * Get the saved priorities for a torrent from the config file.
     594     * @since 0.8.1
     595     */
     596    public void loadSavedFilePriorities(Snark snark) {
     597        MetaInfo metainfo = snark.meta;
     598        if (metainfo.getFiles() == null)
     599            return;
     600        byte[] ih = metainfo.getInfoHash();
     601        String infohash = Base64.encode(ih);
     602        infohash = infohash.replace('=', '$');
     603        String pri = _config.getProperty(PROP_META_PREFIX + infohash + PROP_META_PRIORITY_SUFFIX);
     604        if (pri == null)
     605            return;
     606        int filecount = metainfo.getFiles().size();
     607        int[] rv = new int[filecount];
     608        String[] arr = pri.split(",");
     609        for (int i = 0; i < filecount && i < arr.length; i++) {
     610            if (arr[i].length() > 0) {
     611                try {
     612                    rv[i] = Integer.parseInt(arr[i]);
     613                } catch (Throwable t) {}
     614            }
     615        }
     616        snark.storage.setFilePriorities(rv);
     617    }
     618   
     619    /**
    591620     * Save the completion status of a torrent and the current time in the config file
    592621     * in the form "i2psnark.zmeta.$base64infohash=$time,$base64bitfield".
     
    596625     * The status is either a bitfield converted to Base64 or "." for a completed
    597626     * torrent to save space in the config file and in memory.
    598      */
    599     public void saveTorrentStatus(MetaInfo metainfo, BitField bitfield) {
     627     * @param priorities may be null
     628     */
     629    public void saveTorrentStatus(MetaInfo metainfo, BitField bitfield, int[] priorities) {
    600630        byte[] ih = metainfo.getInfoHash();
    601631        String infohash = Base64.encode(ih);
     
    610640        }
    611641        _config.setProperty(PROP_META_PREFIX + infohash + PROP_META_BITFIELD_SUFFIX, now + "," + bfs);
     642
     643        // now the file priorities
     644        String prop = PROP_META_PREFIX + infohash + PROP_META_PRIORITY_SUFFIX;
     645        if (priorities != null) {
     646            boolean nonzero = false;
     647            for (int i = 0; i < priorities.length; i++) {
     648                if (priorities[i] != 0) {
     649                    nonzero = true;
     650                    break;
     651                }
     652            }
     653            if (nonzero) {
     654                // generate string like -5,,4,3,,,,,,-2 where no number is zero.
     655                StringBuilder buf = new StringBuilder(2 * priorities.length);
     656                for (int i = 0; i < priorities.length; i++) {
     657                    if (priorities[i] != 0)
     658                        buf.append(Integer.toString(priorities[i]));
     659                    if (i != priorities.length - 1)
     660                        buf.append(',');
     661                }
     662                _config.setProperty(prop, buf.toString());
     663            } else {
     664                _config.remove(prop);
     665            }
     666        } else {
     667            _config.remove(prop);
     668        }
     669
    612670        saveConfig();
    613671    }
     
    622680        infohash = infohash.replace('=', '$');
    623681        _config.remove(PROP_META_PREFIX + infohash + PROP_META_BITFIELD_SUFFIX);
     682        _config.remove(PROP_META_PREFIX + infohash + PROP_META_PRIORITY_SUFFIX);
    624683        saveConfig();
    625684    }
     
    743802   
    744803    public void updateStatus(Snark snark) {
    745         saveTorrentStatus(snark.meta, snark.storage.getBitField());
     804        saveTorrentStatus(snark.meta, snark.storage.getBitField(), snark.storage.getFilePriorities());
    746805    }
    747806   
  • apps/i2psnark/java/src/org/klomp/snark/Storage.java

    r9afff4f r24dd783  
    381381
    382382  /**
     383   *  Get the file priorities array.
     384   *  @return null on error, if complete, or if only one file
     385   *  @since 0.8.1
     386   */
     387  public int[] getFilePriorities() {
     388      return priorities;
     389  }
     390
     391  /**
     392   *  Set the file priorities array.
     393   *  Only call this when stopped, but after check()
     394   *  @param p may be null
     395   *  @since 0.8.1
     396   */
     397  void setFilePriorities(int[] p) {
     398      priorities = p;
     399  }
     400
     401  /**
    383402   *  Call setPriority() for all changed files first,
    384403   *  then call this.
     
    390409   */
    391410  public int[] getPiecePriorities() {
    392       if (complete() || metainfo.getFiles() == null)
     411      if (complete() || metainfo.getFiles() == null || priorities == null)
    393412          return null;
    394413      int[] rv = new int[metainfo.getPieces()];
  • apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java

    r9afff4f r24dd783  
    504504                        if (torrentFile.exists())
    505505                            throw new IOException("Cannot overwrite an existing .torrent file: " + torrentFile.getPath());
    506                         _manager.saveTorrentStatus(info, s.getBitField()); // so addTorrent won't recheck
     506                        _manager.saveTorrentStatus(info, s.getBitField(), null); // so addTorrent won't recheck
    507507                        // DirMonitor could grab this first, maybe hold _snarks lock?
    508508                        FileOutputStream out = new FileOutputStream(torrentFile);
     
    14991499
    15001500    /** @since 0.8.1 */
    1501     private static void savePriorities(Snark snark, Map postParams) {
     1501    private void savePriorities(Snark snark, Map postParams) {
    15021502        Set<Map.Entry> entries = postParams.entrySet();
    15031503        for (Map.Entry entry : entries) {
     
    15151515        if (snark.coordinator != null)
    15161516            snark.coordinator.updatePiecePriorities();
     1517        _manager.saveTorrentStatus(snark.storage.getMetaInfo(), snark.storage.getBitField(), snark.storage.getFilePriorities());
    15171518    }
    15181519
Note: See TracChangeset for help on using the changeset viewer.