Changeset 9b69dad0 for apps/addressbook


Ignore:
Timestamp:
Nov 19, 2010 9:12:45 PM (10 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
c061984
Parents:
729aede
Message:
  • Addressbook
    • Store last-fetched time so we don't always fetch subscriptions after restart
    • Randomize first fetch time
    • Make most classes package private
Location:
apps/addressbook/java/src/net/i2p/addressbook
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • apps/addressbook/java/src/net/i2p/addressbook/AddressBook.java

    r729aede r9b69dad0  
    3939 * 
    4040 */
    41 public class AddressBook {
     41class AddressBook {
    4242
    4343    private String location;
     
    8989     * Set a maximum size of the remote book to make it a little harder for a malicious book-sender.
    9090     *
     91     * Yes, the EepGet fetch() is done in this constructor.
     92     *
    9193     * @param subscription
    9294     *            A Subscription instance pointing at a remote address book.
     
    103105            subscription.setEtag(get.getETag());
    104106            subscription.setLastModified(get.getLastModified());
     107            subscription.setLastFetched(I2PAppContext.getGlobalContext().clock().now());
    105108        }
    106109        try {           
  • apps/addressbook/java/src/net/i2p/addressbook/ConfigParser.java

    r729aede r9b69dad0  
    4848 * @author Ragnarok
    4949 */
    50 public class ConfigParser {
     50class ConfigParser {
    5151
    5252    /**
     
    144144     *            A File to attempt to parse.
    145145     * @param map
    146      *            A Map to use as the default, if file fails.
     146     *            A Map containing values to use as defaults.
    147147     * @return A Map containing the key, value pairs from file, or if file
    148148     *         cannot be read, map.
     
    152152        try {
    153153            result = ConfigParser.parse(file);
     154            for (Iterator iter = map.keySet().iterator(); iter.hasNext(); ) {
     155                String key = (String) iter.next();
     156                if (!result.containsKey(key))
     157                    result.put(key, map.get(key));
     158            }
    154159        } catch (IOException exp) {
    155160            result = map;
  • apps/addressbook/java/src/net/i2p/addressbook/Daemon.java

    r729aede r9b69dad0  
    3939 */
    4040public class Daemon {
    41     public static final String VERSION = "2.0.3";
     41    public static final String VERSION = "2.0.4";
    4242    private static final Daemon _instance = new Daemon();
    4343    private boolean _running;
     
    6767        Iterator iter = subscriptions.iterator();
    6868        while (iter.hasNext()) {
     69            // yes, the EepGet fetch() is done in next()
    6970            router.merge((AddressBook) iter.next(), false, log);
    7071        }
     
    9899        File lastModifiedFile = new File(home, (String) settings
    99100                .get("last_modified"));
     101        File lastFetchedFile = new File(home, (String) settings
     102                .get("last_fetched"));
     103        long delay;
     104        try {
     105            delay = Long.parseLong((String) settings.get("update_delay"));
     106        } catch (NumberFormatException nfe) {
     107            delay = 12;
     108        }
     109        delay *= 60 * 60 * 1000;
    100110
    101111        AddressBook master = new AddressBook(masterFile);
     
    107117       
    108118        SubscriptionList subscriptions = new SubscriptionList(subscriptionFile,
    109                 etagsFile, lastModifiedFile, defaultSubs, (String) settings
     119                etagsFile, lastModifiedFile, lastFetchedFile, delay, defaultSubs, (String) settings
    110120                .get("proxy_host"), Integer.parseInt((String) settings.get("proxy_port")));
    111121        Log log = new Log(logFile);
     
    151161        defaultSettings.put("etags", "etags");
    152162        defaultSettings.put("last_modified", "last_modified");
     163        defaultSettings.put("last_fetched", "last_fetched");
    153164        defaultSettings.put("update_delay", "12");
    154165       
     
    166177        // wait
    167178        try {
    168             Thread.sleep(5*60*1000);
     179            Thread.sleep(5*60*1000 + I2PAppContext.getGlobalContext().random().nextLong(5*60*1000));
    169180            // Static method, and redundent Thread.currentThread().sleep(5*60*1000);
    170181        } catch (InterruptedException ie) {}
  • apps/addressbook/java/src/net/i2p/addressbook/DaemonThread.java

    r729aede r9b69dad0  
    2828 *
    2929 */
    30 public class DaemonThread extends Thread {
     30class DaemonThread extends Thread {
    3131
    3232    private String[] args;
  • apps/addressbook/java/src/net/i2p/addressbook/Log.java

    r729aede r9b69dad0  
    3434 * 
    3535 */
    36 public class Log {
     36class Log {
    3737
    3838    private File file;
  • apps/addressbook/java/src/net/i2p/addressbook/Subscription.java

    r729aede r9b69dad0  
    2828 * 
    2929 */
    30 public class Subscription {
     30class Subscription {
    3131
    3232    private String location;
     
    3535
    3636    private String lastModified;
     37    private long lastFetched;
    3738
    3839    /**
     
    4849     *            the last-modified header we recieved the last time we read
    4950     *            this subscription.
     51     * @param lastFetched when the subscription was last fetched (Java time, as a String)
    5052     */
    51     public Subscription(String location, String etag, String lastModified) {
     53    public Subscription(String location, String etag, String lastModified, String lastFetched) {
    5254        this.location = location;
    5355        this.etag = etag;
    5456        this.lastModified = lastModified;
     57        if (lastFetched != null) {
     58            try {
     59                this.lastFetched = Long.parseLong(lastFetched);
     60            } catch (NumberFormatException nfe) {}
     61        }
    5562    }
    5663
     
    103110        this.lastModified = lastModified;
    104111    }
     112
     113    /** @since 0.8.2 */
     114    public long getLastFetched() {
     115        return this.lastFetched;
     116    }
     117
     118    /** @since 0.8.2 */
     119    public void setLastFetched(long t) {
     120        this.lastFetched = t;
     121    }
    105122}
  • apps/addressbook/java/src/net/i2p/addressbook/SubscriptionIterator.java

    r729aede r9b69dad0  
    2222package net.i2p.addressbook;
    2323
     24import java.util.Collections;
    2425import java.util.Iterator;
    2526import java.util.List;
     27
     28import net.i2p.I2PAppContext;
     29import net.i2p.data.DataHelper;  // debug
    2630
    2731/**
    2832 * An iterator over the subscriptions in a SubscriptionList.  Note that this iterator
    2933 * returns AddressBook objects, and not Subscription objects.
     34 * Yes, the EepGet fetch() is done in here in next().
    3035 *
    3136 * @author Ragnarok
    3237 */
    33 public class SubscriptionIterator implements Iterator {
     38class SubscriptionIterator implements Iterator {
    3439
    3540    private Iterator subIterator;
    3641    private String proxyHost;
    3742    private int proxyPort;
     43    private final long delay;
    3844
    3945    /**
     
    4248     * @param subscriptions
    4349     *            List of Subscription objects that represent address books.
     50     * @param delay the minimum delay since last fetched for the iterator to actually fetch
    4451     * @param proxyHost proxy hostname
    4552     * @param proxyPort proxt port number
    4653     */
    47     public SubscriptionIterator(List subscriptions, String proxyHost, int proxyPort) {
     54    public SubscriptionIterator(List subscriptions, long delay, String proxyHost, int proxyPort) {
    4855        this.subIterator = subscriptions.iterator();
     56        this.delay = delay;
    4957        this.proxyHost = proxyHost;
    5058        this.proxyPort = proxyPort;
     
    5967    }
    6068
    61     /* (non-Javadoc)
    62      * @see java.util.Iterator#next()
     69    /**
     70     * Yes, the EepGet fetch() is done in here in next().
     71     *
     72     * see java.util.Iterator#next()
     73     * @return an AddressBook (empty if the minimum delay has not been met)
    6374     */
    6475    public Object next() {
    6576        Subscription sub = (Subscription) this.subIterator.next();
    66         return new AddressBook(sub, this.proxyHost, this.proxyPort);
     77        if (sub.getLastFetched() + this.delay < I2PAppContext.getGlobalContext().clock().now()) {
     78            //System.err.println("Fetching addressbook from " + sub.getLocation());
     79            return new AddressBook(sub, this.proxyHost, this.proxyPort);
     80        } else {
     81            //System.err.println("Addressbook " + sub.getLocation() + " was last fetched " +
     82            //                   DataHelper.formatDuration(I2PAppContext.getGlobalContext().clock().now() - sub.getLastFetched()) +
     83            //                   " ago but the minimum delay is " +
     84            //                   DataHelper.formatDuration(this.delay));
     85            return new AddressBook(Collections.EMPTY_MAP);
     86        }
    6787    }
    6888
  • apps/addressbook/java/src/net/i2p/addressbook/SubscriptionList.java

    r729aede r9b69dad0  
    3636 * 
    3737 */
    38 public class SubscriptionList {
     38class SubscriptionList {
    3939
    4040    private List subscriptions;
     
    4343
    4444    private File lastModifiedFile;
     45    private File lastFetchedFile;
     46    private final long delay;
    4547   
    4648    private String proxyHost;
     
    6163     *            A file containg the last-modified headers used for conditional
    6264     *            GET. The file is in the format "url=leastmodified".
     65     * @param delay the minimum delay since last fetched for the iterator to actually fetch
    6366     * @param defaultSubs default subscription file
    6467     * @param proxyHost proxy hostname
     
    6669     */
    6770    public SubscriptionList(File locationsFile, File etagsFile,
    68             File lastModifiedFile, List defaultSubs, String proxyHost,
     71            File lastModifiedFile, File lastFetchedFile, long delay, List defaultSubs, String proxyHost,
    6972            int proxyPort) {
    7073        this.subscriptions = new LinkedList();
    7174        this.etagsFile = etagsFile;
    7275        this.lastModifiedFile = lastModifiedFile;
     76        this.lastFetchedFile = lastFetchedFile;
     77        this.delay = delay;
    7378        this.proxyHost = proxyHost;
    7479        this.proxyPort = proxyPort;
    7580        Map etags;
    7681        Map lastModified;
     82        Map lastFetched;
    7783        String location;
    7884        List locations = ConfigParser.parseSubscriptions(locationsFile,
     
    8894            lastModified = new HashMap();
    8995        }
     96        try {
     97            lastFetched = ConfigParser.parse(lastFetchedFile);
     98        } catch (IOException exp) {
     99            lastFetched = new HashMap();
     100        }
    90101        Iterator iter = locations.iterator();
    91102        while (iter.hasNext()) {
    92103            location = (String) iter.next();
    93             this.subscriptions.add(new Subscription(location, (String) etags
    94                     .get(location), (String) lastModified.get(location)));
     104            this.subscriptions.add(new Subscription(location, (String) etags.get(location),
     105                                   (String) lastModified.get(location),
     106                                   (String) lastFetched.get(location)));
    95107        }
    96108    }
     
    103115     */
    104116    public SubscriptionIterator iterator() {
    105         return new SubscriptionIterator(this.subscriptions, this.proxyHost,
     117        return new SubscriptionIterator(this.subscriptions, this.delay, this.proxyHost,
    106118                this.proxyPort);
    107119    }
    108120
    109121    /**
    110      * Write the etag and last-modified headers for each Subscription to files.
     122     * Write the etag and last-modified headers,
     123     * and the last-fetched time, for each Subscription to files.
     124     * BUG - If the subscription URL is a cgi containing an '=' the files
     125     * won't be read back correctly; the '=' should be escaped.
    111126     */
    112127    public void write() {
     
    115130        Map etags = new HashMap();
    116131        Map lastModified = new HashMap();
     132        Map lastFetched = new HashMap();
    117133        while (iter.hasNext()) {
    118134            sub = (Subscription) iter.next();
     
    123139                lastModified.put(sub.getLocation(), sub.getLastModified());
    124140            }
     141            lastFetched.put(sub.getLocation(), "" + sub.getLastFetched());
    125142        }
    126143        try {
    127144            ConfigParser.write(etags, this.etagsFile);
    128145            ConfigParser.write(lastModified, this.lastModifiedFile);
     146            ConfigParser.write(lastFetched, this.lastFetchedFile);
    129147        } catch (IOException exp) {
    130148        }
Note: See TracChangeset for help on using the changeset viewer.