Changeset b9d717b


Ignore:
Timestamp:
Oct 17, 2012 10:24:15 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f9b8f052
Parents:
4db4010
Message:
  • Split up Updater and Checker interfaces
  • Update router after check
Files:
1 added
17 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/update/ConsoleUpdateManager.java

    r4db4010 rb9d717b  
    4646 *  and controls notification to the user.
    4747 *
    48  *  @since 0.9.2
     48 *  @since 0.9.4
    4949 */
    5050public class ConsoleUpdateManager implements UpdateManager {
     
    5252    private final RouterContext _context;
    5353    private final Log _log;
    54     /** registered checkers / updaters */
    55     private final Collection<RegisteredUpdater> _registered;
     54    private final Collection<RegisteredUpdater> _registeredUpdaters;
     55    private final Collection<RegisteredChecker> _registeredCheckers;
    5656    /** active checking tasks */
    57     private final Collection<UpdateTask> _checkers;
     57    private final Collection<UpdateTask> _activeCheckers;
    5858    /** active updating tasks, pointing to the next ones to try */
    5959    private final Map<UpdateTask, List<RegisteredUpdater>> _downloaders;
     
    6262    /** downloaded but NOT installed */
    6363    private final Map<UpdateItem, Version> _downloaded;
    64     /** downloaded but NOT installed */
     64    /** downloaded AND installed */
    6565    private final Map<UpdateItem, Version> _installed;
    66     private final DecimalFormat _pct = new DecimalFormat("0.0%");
     66    private static final DecimalFormat _pct = new DecimalFormat("0.0%");
    6767    private static final VersionComparator _versionComparator = new VersionComparator();
    6868
    6969    private volatile String _status;
     70
     71    private static final long DEFAULT_MAX_TIME = 3*60*60*1000L;
    7072
    7173    public ConsoleUpdateManager(RouterContext ctx) {
    7274        _context = ctx;
    7375        _log = ctx.logManager().getLog(ConsoleUpdateManager.class);
    74         _registered = new ConcurrentHashSet();
    75         _checkers = new ConcurrentHashSet();
     76        _registeredUpdaters = new ConcurrentHashSet();
     77        _registeredCheckers = new ConcurrentHashSet();
     78        _activeCheckers = new ConcurrentHashSet();
    7679        _downloaders = new ConcurrentHashMap();
    7780        _available = new ConcurrentHashMap();
     
    102105        // register news before router, so we don't fire off an update
    103106        // right at instantiation if the news is already indicating a new version
    104         u = new NewsHandler(_context);
    105         register(u, NEWS, HTTP, 0);
    106         register(u, ROUTER_SIGNED, HTTP, 0);  // news is an update checker for the router
     107        Checker c = new NewsHandler(_context);
     108        register(c, NEWS, HTTP, 0);
     109        register(c, ROUTER_SIGNED, HTTP, 0);  // news is an update checker for the router
    107110        u = new UpdateHandler(_context);
    108111        register(u, ROUTER_SIGNED, HTTP, 0);
    109         u = new UnsignedUpdateHandler(_context);
    110         register(u, ROUTER_UNSIGNED, HTTP, 0);
    111         u = new PluginUpdateHandler(_context);
    112         register(u, PLUGIN, HTTP, 0);
    113         register(u, PLUGIN_INSTALL, HTTP, 0);
     112        UnsignedUpdateHandler uuh = new UnsignedUpdateHandler(_context);
     113        register((Checker)uuh, ROUTER_UNSIGNED, HTTP, 0);
     114        register((Updater)uuh, ROUTER_UNSIGNED, HTTP, 0);
     115        PluginUpdateHandler puh = new PluginUpdateHandler(_context);
     116        register((Checker)puh, PLUGIN, HTTP, 0);
     117        register((Checker)puh, PLUGIN_INSTALL, HTTP, 0);
     118        register((Updater)puh, PLUGIN_INSTALL, HTTP, 0);
    114119        new NewsTimerTask(_context);
    115120    }
     
    119124        stopChecks();
    120125        stopUpdates();
    121         _registered.clear();
     126        _registeredUpdaters.clear();
     127        _registeredCheckers.clear();
    122128        _available.clear();
    123129        _downloaded.clear();
     
    150156            return null;
    151157        }
    152         for (RegisteredUpdater r : _registered) {
     158        for (RegisteredChecker r : _registeredCheckers) {
    153159            if (r.type == type) {
    154                 UpdateTask t = r.updater.check(type, r.method, id, "FIXME", maxWait);
     160                UpdateTask t = r.checker.check(type, r.method, id, "FIXME", maxWait);
    155161                if (t != null) {
    156162                    synchronized(t) {
     
    176182            return;
    177183        }
    178         for (RegisteredUpdater r : _registered) {
     184        for (RegisteredChecker r : _registeredCheckers) {
    179185            if (r.type == type) {
    180186/// fixme "" will put an entry in _available for everything grrrrr????
    181                 UpdateTask t = r.updater.check(type, r.method, id, "", 5*60*1000);
     187                UpdateTask t = r.checker.check(type, r.method, id, "", 5*60*1000);
    182188                if (t != null)
    183189                    break;
     
    289295     */
    290296    public boolean isCheckInProgress() {
    291         return !_checkers.isEmpty();
     297        return !_activeCheckers.isEmpty();
    292298    }
    293299
     
    303309     */
    304310    public boolean isCheckInProgress(UpdateType type, String id) {
    305         for (UpdateTask t : _checkers) {
     311        for (UpdateTask t : _activeCheckers) {
    306312            if (t.getType() == type && id.equals(t.getID()))
    307313                return true;
     
    314320     */
    315321    public void stopChecks() {
    316         for (UpdateTask t : _checkers) {
     322        for (UpdateTask t : _activeCheckers) {
    317323            t.shutdown();
    318324        }
    319         _checkers.clear();
     325        _activeCheckers.clear();
    320326    }
    321327
     
    331337     */
    332338    public void stopCheck(UpdateType type, String id) {
    333         for (Iterator<UpdateTask> iter = _checkers.iterator(); iter.hasNext(); ) {
     339        for (Iterator<UpdateTask> iter = _activeCheckers.iterator(); iter.hasNext(); ) {
    334340            UpdateTask t = iter.next();
    335341            if (t.getType() == type && id.equals(t.getID())) {
     
    361367     */
    362368    public boolean update(UpdateType type) {
    363         return update(type, "", 3*60*1000);
     369        return update(type, "", DEFAULT_MAX_TIME);
    364370    }
    365371
     
    371377     */
    372378    public boolean update(UpdateType type, String id) {
    373         return update(type, id, 3*60*60*1000);
     379        return update(type, id, DEFAULT_MAX_TIME);
    374380    }
    375381
     
    386392    /**
    387393     *  Non-blocking. Does not check.
     394     *  Fails if check or update already in progress.
    388395     *  If returns true, then call isUpdateInProgress() in a loop
    389396     *  @param maxTime not honored by all Updaters
     
    391398     */
    392399    public boolean update(UpdateType type, String id, long maxTime) {
    393         if (isCheckInProgress(type, id) || isUpdateInProgress(type, id)) {
     400        if (isCheckInProgress(type, id)) {
    394401            if (_log.shouldLog(Log.WARN))
    395                 _log.warn("Check or update already in progress for: " + type + ' ' + id);
     402                _log.warn("Check already in progress for: " + type + ' ' + id);
     403            return false;
     404        }
     405        return update_fromCheck(type, id, maxTime);
     406    }
     407
     408    /**
     409     *  Non-blocking. Does not check.
     410     *  Fails update already in progress. Use this to call from within a checker task.
     411     *  If returns true, then call isUpdateInProgress() in a loop
     412     *  @param maxTime not honored by all Updaters
     413     *  @return true if task started
     414     */
     415    private boolean update_fromCheck(UpdateType type, String id, long maxTime) {
     416        if (isUpdateInProgress(type, id)) {
     417            if (_log.shouldLog(Log.WARN))
     418                _log.warn("Update already in progress for: " + type + ' ' + id);
    396419            return false;
    397420        }
     
    401424        if (va == null)
    402425            return false;
    403         List<RegisteredUpdater> sorted = new ArrayList(_registered);
     426        List<RegisteredUpdater> sorted = new ArrayList(_registeredUpdaters);
    404427        Collections.sort(sorted);
    405428        return retry(ui, va.sourceMap, sorted, maxTime) != null;
     
    413436            iter.remove();
    414437            // check in case unregistered later
    415             if (!_registered.contains(r))
     438            if (!_registeredUpdaters.contains(r))
    416439                continue;
    417440            for (Map.Entry<UpdateMethod, List<URI>> e : sourceMap.entrySet()) {
     
    435458
    436459    /**
    437      *  Call multiple times, one for each type/method pair.
     460     *  Call once for each type/method pair.
    438461     */
    439462    public void register(Updater updater, UpdateType type, UpdateMethod method, int priority) {
     
    441464        if (_log.shouldLog(Log.INFO))
    442465            _log.info("Registering " + ru);
    443         _registered.add(ru);
     466        _registeredUpdaters.add(ru);
    444467    }
    445468
     
    448471        if (_log.shouldLog(Log.INFO))
    449472            _log.info("Unregistering " + ru);
    450         _registered.remove(ru);
     473        _registeredUpdaters.remove(ru);
     474    }
     475   
     476    public void register(Checker updater, UpdateType type, UpdateMethod method, int priority) {
     477        RegisteredChecker rc = new RegisteredChecker(updater, type, method, priority);
     478        if (_log.shouldLog(Log.INFO))
     479            _log.info("Registering " + rc);
     480        _registeredCheckers.add(rc);
     481    }
     482
     483    public void unregister(Checker updater, UpdateType type, UpdateMethod method) {
     484        RegisteredChecker rc = new RegisteredChecker(updater, type, method, 0);
     485        if (_log.shouldLog(Log.INFO))
     486            _log.info("Unregistering " + rc);
     487        _registeredCheckers.remove(rc);
    451488    }
    452489   
     
    510547        _available.put(ui, newVA);
    511548
     549        String msg = null;
    512550        switch (type) {
    513551            case NEWS:
     
    516554            case ROUTER_SIGNED:
    517555            case ROUTER_SIGNED_PACK200:
    518                 if (shouldInstall()) {
    519 ////////////
     556            case ROUTER_UNSIGNED:
     557                if (shouldInstall() &&
     558                    !(isUpdateInProgress(ROUTER_SIGNED) ||
     559                      isUpdateInProgress(ROUTER_SIGNED_PACK200) ||
     560                      isUpdateInProgress(ROUTER_UNSIGNED))) {
     561                    update_fromCheck(type, id, DEFAULT_MAX_TIME);
    520562                }
    521                 break;
    522 
    523             case ROUTER_UNSIGNED:
    524                 if (shouldInstall()) {
    525 ////////////
    526                 }
     563                // ConfigUpdateHandler, SummaryHelper, SummaryBarRenderer handle status display
    527564                break;
    528565
    529566            case PLUGIN:
    530                 String msg = "<b>" + _("New plugin version {0} is available", newVersion) + "</b>";
    531                 finishStatus(msg);
     567                msg = "<b>" + _("New plugin version {0} is available", newVersion) + "</b>";
    532568                break;
    533569
     
    535571                break;
    536572        }
     573        if (msg != null)
     574            finishStatus(msg);
    537575        return true;
    538 // TODO
    539576    }
    540577
     
    545582        if (_log.shouldLog(Log.INFO))
    546583            _log.info(task.toString() + " complete");
    547         _checkers.remove(task);
     584        _activeCheckers.remove(task);
     585        String msg = null;
    548586        switch (task.getType()) {
    549587            case NEWS:
    550                 // NewsFetcher will notify and spin off update tasks
    551                 break;
    552 
    553588            case ROUTER_SIGNED:
    554589            case ROUTER_SIGNED_PACK200:
    555                 break;
    556 
    557590            case ROUTER_UNSIGNED:
    558                 // if  _mgr.getUpdateDownloaded(ROUTER_SIGNED) != null;
     591                // ConfigUpdateHandler, SummaryHelper, SummaryBarRenderer handle status display
    559592                break;
    560593
    561594            case PLUGIN:
    562                 String msg = null;
    563595                if (!success)
    564596                    msg = "<b>" + _("Update check failed for plugin {0}", task.getID()) + "</b>";
     
    567599                /// else success.... message for that?
    568600
    569                 if (msg != null)
    570                     finishStatus(msg);
    571601                break;
    572602
     
    574604                break;
    575605        }
     606        if (msg != null)
     607            finishStatus(msg);
    576608        synchronized(task) {
    577609            task.notifyAll();
     
    627659            VersionAvailable va = _available.get(ui);
    628660            if (va != null) {
    629                 UpdateTask next = retry(ui, va.sourceMap, toTry, 3*60*1000);  // fixme old maxtime lost
     661                UpdateTask next = retry(ui, va.sourceMap, toTry, DEFAULT_MAX_TIME);  // fixme old maxtime lost
    630662                if (next != null) {
    631663                   if (_log.shouldLog(Log.WARN))
     
    9881020
    9891021    /**
     1022     *  Equals on checker, type and method only
     1023     */
     1024    private static class RegisteredChecker implements Comparable<RegisteredChecker> {
     1025        public final Checker checker;
     1026        public final UpdateType type;
     1027        public final UpdateMethod method;
     1028        public final int priority;
     1029
     1030        public RegisteredChecker(Checker u, UpdateType t, UpdateMethod m, int priority) {
     1031            checker = u; type = t; method = m; this.priority = priority;
     1032        }
     1033
     1034        /** reverse, highest priority first, ensure different ones are different */
     1035        public int compareTo(RegisteredChecker r) {
     1036            int p = r.priority - priority;
     1037            if (p != 0)
     1038                return p;
     1039            return hashCode() - r.hashCode();
     1040        }
     1041
     1042        @Override
     1043        public int hashCode() {
     1044            return checker.hashCode() ^ type.hashCode() ^ method.hashCode();
     1045        }
     1046
     1047        @Override
     1048        public boolean equals(Object o) {
     1049            if (!(o instanceof RegisteredChecker))
     1050                return false;
     1051            RegisteredChecker r = (RegisteredChecker) o;
     1052            return type == r.type && method == r.method &&
     1053                   checker.equals(r.checker);
     1054        }
     1055
     1056        @Override
     1057        public String toString() {
     1058            return "RegisteredChecker " + checker + " for " + type + ' ' + method + " @pri " + priority;
     1059        }
     1060    }
     1061
     1062    /**
    9901063     *  Equals on type and ID only
    9911064     */
  • apps/routerconsole/java/src/net/i2p/router/update/DummyHandler.java

    r4db4010 rb9d717b  
    1111 * Dummy to lock up the updates for a period of time
    1212 *
    13  * @since 0.9.2
     13 * @since 0.9.4
    1414 */
    15 class DummyHandler implements Updater {
     15class DummyHandler implements Checker, Updater {
    1616    private final RouterContext _context;
    1717   
  • apps/routerconsole/java/src/net/i2p/router/update/NewsFetcher.java

    r4db4010 rb9d717b  
    3232 * track of whether that has an announcement for a new version.
    3333 *
    34  * @since 0.9.2 moved from NewsFetcher and make an Updater
     34 * @since 0.9.4 moved from NewsFetcher and make an Updater
    3535 */
    3636class NewsFetcher extends UpdateRunner {
  • apps/routerconsole/java/src/net/i2p/router/update/NewsHandler.java

    r4db4010 rb9d717b  
    1414 * track of whether that has an announcement for a new version.
    1515 *
    16  * @since 0.9.2 moved from NewsFetcher
     16 * @since 0.9.4 moved from NewsFetcher
    1717 */
    18 class NewsHandler extends UpdateHandler {
     18class NewsHandler extends UpdateHandler implements Checker {
    1919   
    2020    /** @since 0.7.14 not configurable */
     
    4646        return update;
    4747    }
    48 
    49     /**
    50      *  Does nothing. check() also does update.
    51      */
    52     public UpdateTask update(UpdateType type, UpdateMethod method, List<URI> updateSources,
    53                              String id, String newVersion, long maxTime) {
    54         return null;
    55     }
    5648}
  • apps/routerconsole/java/src/net/i2p/router/update/NewsTimerTask.java

    r4db4010 rb9d717b  
    3434 * Runs forever on instantiation, can't be stopped.
    3535 *
    36  * @since 0.9.2 moved from NewsFetcher
     36 * @since 0.9.4 moved from NewsFetcher
    3737 */
    3838class NewsTimerTask implements SimpleTimer.TimedEvent {
  • apps/routerconsole/java/src/net/i2p/router/update/PluginUpdateHandler.java

    r4db4010 rb9d717b  
    3535 * @author zzz
    3636 */
    37 class PluginUpdateHandler implements Updater {
     37class PluginUpdateHandler implements Checker, Updater {
    3838    private final RouterContext _context;
    3939
  • apps/routerconsole/java/src/net/i2p/router/update/PluginUpdateRunner.java

    r4db4010 rb9d717b  
    4040 * Moved from web/ and turned into an UpdateTask.
    4141 *
    42  * @since 0.9.2 moved from PluginUpdateHandler
     42 * @since 0.9.4 moved from PluginUpdateHandler
    4343 */
    4444class PluginUpdateRunner extends UpdateRunner {
  • apps/routerconsole/java/src/net/i2p/router/update/UnsignedUpdateChecker.java

    r4db4010 rb9d717b  
    2222 *  Overrides UpdateRunner for convenience, does not use super's Eepget StatusListener
    2323 *
    24  *  @since 0.9.2
     24 *  @since 0.9.4
    2525 */
    2626class UnsignedUpdateChecker extends UpdateRunner {
  • apps/routerconsole/java/src/net/i2p/router/update/UnsignedUpdateHandler.java

    r4db4010 rb9d717b  
    2828 * </p>
    2929 */
    30 class UnsignedUpdateHandler implements Updater {
     30class UnsignedUpdateHandler implements Checker, Updater {
    3131    private final RouterContext _context;
    3232
  • apps/routerconsole/java/src/net/i2p/router/update/UnsignedUpdateRunner.java

    r4db4010 rb9d717b  
    2020 *  Moved from UnsignedUpdateHandler and turned into an UpdateTask.
    2121 *
    22  *  @since 0.9.2
     22 *  @since 0.9.4
    2323 */
    2424class UnsignedUpdateRunner extends UpdateRunner {
  • apps/routerconsole/java/src/net/i2p/router/update/UpdateHandler.java

    r4db4010 rb9d717b  
    4444    }
    4545   
    46     /** Can't check, the NewsHandler does that */
    47     public UpdateTask check(UpdateType type, UpdateMethod method,
    48                             String id, String currentVersion, long maxTime) {
    49         return null;
    50     }
    51 
    5246    /**
    5347     *  Start a download and return a handle to the download task.
  • apps/routerconsole/java/src/net/i2p/router/update/UpdateRunner.java

    r4db4010 rb9d717b  
    2525 *  and the base class for all the other Checkers and Runners.
    2626 *
    27  *  @since 0.9.2 moved from UpdateHandler
     27 *  @since 0.9.4 moved from UpdateHandler
    2828 *
    2929 */
  • core/java/src/net/i2p/update/UpdateManager.java

    r4db4010 rb9d717b  
    33import java.io.File;
    44import java.net.URI;
    5 import java.util.List;;
     5import java.util.List;
    66
    77/**
     
    1212 *  and controls notification to the user.
    1313 *
    14  *  @since 0.9.2
     14 *  @since 0.9.4
    1515 */
    1616public interface UpdateManager {
    1717   
    1818    /**
    19      *  Call multiple times for each type/method pair.
    20      *  The UpdateManager will then call start()
     19     *  Call once for each type/method pair.
    2120     */
    2221    public void register(Updater updater, UpdateType type, UpdateMethod method, int priority);
    2322
     23    public void register(Checker checker, UpdateType type, UpdateMethod method, int priority);
     24
    2425    public void unregister(Updater updater, UpdateType type, UpdateMethod method);
     26
     27    public void unregister(Checker checker, UpdateType type, UpdateMethod method);
    2528   
    2629    public void start();
  • core/java/src/net/i2p/update/UpdateMethod.java

    r4db4010 rb9d717b  
    44 *  Transport mechanism for getting something.
    55 *
    6  *  @since 0.9.2
     6 *  @since 0.9.4
    77 */
    88public enum UpdateMethod {
  • core/java/src/net/i2p/update/UpdateTask.java

    r4db4010 rb9d717b  
    66 *  A running check or download. Cannot be restarted.
    77 *
    8  *  @since 0.9.2
     8 *  @since 0.9.4
    99 */
    1010public interface UpdateTask {
  • core/java/src/net/i2p/update/UpdateType.java

    r4db4010 rb9d717b  
    44 *  What to update
    55 *
    6  *  @since 0.9.2
     6 *  @since 0.9.4
    77 */
    88public enum UpdateType {
  • core/java/src/net/i2p/update/Updater.java

    r4db4010 rb9d717b  
    88 *  This must be registered with the UpdateManager.
    99 *
    10  *  @since 0.9.2
     10 *  @since 0.9.4
    1111 */
    1212public interface Updater {
    1313   
    14     /**
    15      *  Check for updates.
    16      *  Should not block.
    17      *  If any are found, call back to UpdateManager.notifyUpdateAvailable().
    18      *
    19      *  @param id plugin name or ignored
    20      *  @param maxTime how long you have
    21      *  @return active task or null if unable to check
    22      */
    23     public UpdateTask check(UpdateType type, UpdateMethod method,
    24                             String id, String currentVersion, long maxTime);
    25 
    2614    /**
    2715     *  Start a download and return a handle to the download task.
Note: See TracChangeset for help on using the changeset viewer.