Changeset 2a805dd for apps


Ignore:
Timestamp:
Dec 16, 2018 2:44:37 PM (19 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
cae5dcd
Parents:
fa0d63f
Message:

Sybil: Refactoring part 5
Make Analysis class singleton, hang off ClientAppManager?
prep for offline scheduling

Location:
apps/routerconsole/java/src/net/i2p/router
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/sybil/Analysis.java

    rfa0d63f r2a805dd  
    1212import java.util.Set;
    1313
     14import net.i2p.app.ClientAppManager;
     15import net.i2p.app.ClientAppState;
     16import static net.i2p.app.ClientAppState.*;
    1417import net.i2p.data.DataHelper;
    1518import net.i2p.data.Destination;
     
    2124import net.i2p.router.RouterContext;
    2225import net.i2p.router.TunnelPoolSettings;
     26import net.i2p.router.app.RouterApp;
    2327import net.i2p.router.crypto.FamilyKeyCrypto;
    2428import net.i2p.router.peermanager.DBHistory;
     
    3741 *
    3842 */
    39 public class Analysis {
     43public class Analysis implements RouterApp {
    4044
    4145    private final RouterContext _context;
     46    private final ClientAppManager _cmgr;
     47    private final PersistSybil _persister;
     48    private volatile ClientAppState _state = UNINITIALIZED;
    4249    private final DecimalFormat fmt = new DecimalFormat("#0.00");
     50    /**
     51     *  The name we register with the ClientAppManager
     52     */
     53    public static final String APP_NAME = "sybil";
     54    public static final String PROP_FREQUENCY = "router.sybilFrequency";
     55    private static final long MIN_FREQUENCY = 60*60*1000L;
     56    private static final long MIN_UPTIME = 75*60*1000L;
    4357
    4458    public static final int PAIRMAX = 20;
     
    6579    private static final double POINTS_BANLIST = 25.0;
    6680
    67     public Analysis(RouterContext ctx) {
     81    /** Get via getInstance() */
     82    private Analysis(RouterContext ctx, ClientAppManager mgr, String[] args) {
    6883        _context = ctx;
     84        _cmgr = mgr;
     85        _persister = new PersistSybil(ctx);
     86    }
     87
     88    /**
     89     *  @return non-null, creates new if not already registered
     90     */
     91    public synchronized static Analysis getInstance(RouterContext ctx) {
     92        ClientAppManager cmgr = ctx.clientAppManager();
     93        if (cmgr == null)
     94            return null;
     95        Analysis rv = (Analysis) cmgr.getRegisteredApp(APP_NAME);
     96        if (rv == null) {
     97            rv = new Analysis(ctx, cmgr, null);
     98            rv.startup();
     99        }
     100        return rv;
     101    }
     102
     103    public PersistSybil getPersister() { return _persister; }
     104
     105    /**
     106     *  ClientApp interface
     107     */
     108    public synchronized void startup() {
     109        changeState(STARTING);
     110        changeState(RUNNING);
     111        _cmgr.register(this);
     112        schedule();
     113    }
     114
     115    /**
     116     *  ClientApp interface
     117     *  @param args ignored
     118     */
     119    public synchronized void shutdown(String[] args) {
     120        if (_state == STOPPED)
     121            return;
     122        changeState(STOPPING);
     123        changeState(STOPPED);
     124    }
     125
     126    public ClientAppState getState() {
     127        return _state;
     128    }
     129
     130    public String getName() {
     131        return APP_NAME;
     132    }
     133
     134    public String getDisplayName() {
     135        return "Sybil Analyzer";
     136    }
     137
     138    /////// end ClientApp methods
     139
     140    private synchronized void changeState(ClientAppState state) {
     141        _state = state;
     142        if (_cmgr != null)
     143            _cmgr.notify(this, state, null, null);
     144    }
     145
     146    public void schedule() {
     147        long freq = _context.getProperty(PROP_FREQUENCY, 0L);
     148        if (freq > 0) {
     149            List<Long> previous = _persister.load();
     150            long now = _context.clock().now();
     151            long when;
     152            if (!previous.isEmpty()) {
     153                if (freq < MIN_FREQUENCY)
     154                    freq = MIN_FREQUENCY;
     155                when = Math.max(previous.get(0).longValue() + freq, now);
     156            } else {
     157                when = now;
     158            }
     159            long up = _context.router().getUptime();
     160            when = Math.max(when, now + MIN_UPTIME - up);
     161            // TODO schedule for when
     162        }
    69163    }
    70164
  • apps/routerconsole/java/src/net/i2p/router/sybil/PersistSybil.java

    rfa0d63f r2a805dd  
    4545    private static final String SFX = ".txt.gz";
    4646
    47     public PersistSybil(I2PAppContext ctx) {
     47    /** access via Analysis.getPersister() */
     48    PersistSybil(I2PAppContext ctx) {
    4849        _context = ctx;
    4950        _log = ctx.logManager().getLog(PersistSybil.class);
  • apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java

    rfa0d63f r2a805dd  
    3535import net.i2p.router.app.RouterApp;
    3636import net.i2p.router.news.NewsManager;
     37import net.i2p.router.sybil.Analysis;
    3738import net.i2p.router.update.ConsoleUpdateManager;
    3839import net.i2p.util.Addresses;
     
    866867                _context.addShutdownTask(new ServerShutdown());
    867868            ConfigServiceHandler.registerSignalHandler(_context);
     869
     870            if (_mgr != null &&
     871                _context.getBooleanProperty(HelperBase.PROP_ADVANCED) &&
     872                _context.getProperty(Analysis.PROP_FREQUENCY, 0L) > 0) {
     873                // registers and starts itself
     874                Analysis.getInstance(_context);
     875            }
    868876    }
    869877   
  • apps/routerconsole/java/src/net/i2p/router/web/helpers/SybilRenderer.java

    rfa0d63f r2a805dd  
    126126    private void renderRouterInfoHTML(Writer out, int mode, long date) throws IOException {
    127127        Hash us = _context.routerHash();
    128         Analysis analysis = new Analysis(_context);
     128        Analysis analysis = Analysis.getInstance(_context);
    129129        List<RouterInfo> ris = analysis.getFloodfills(us);
    130130        if (ris.isEmpty()) {
     
    181181            renderDestSummary(out, buf, analysis, avgMinDist, ris, points);
    182182        } else if (mode == 12) {
    183             PersistSybil ps = new PersistSybil(_context);
     183            PersistSybil ps = analysis.getPersister();
    184184            try {
    185185                points = ps.load(date);
     
    197197            points = analysis.backgroundAnalysis();
    198198            if (!points.isEmpty()) {
    199                 PersistSybil ps = new PersistSybil(_context);
     199                PersistSybil ps = analysis.getPersister();
    200200                try {
    201201                    ps.store(now, points);
     
    215215     */
    216216    private void renderOverview(Writer out, StringBuilder buf, Analysis analysis) throws IOException {
    217         PersistSybil ps = new PersistSybil(_context);
     217        PersistSybil ps = analysis.getPersister();
    218218        List<Long> dates = ps.load();
    219219        if (dates.isEmpty()) {
Note: See TracChangeset for help on using the changeset viewer.