Changeset f11a543


Ignore:
Timestamp:
Feb 11, 2011 11:59:10 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f4ca3976
Parents:
3f3385f
Message:
  • Clock: Cleanups and javadocs
  • EepGet?: Cleanups and javadocs
  • Reseed: Use the reseeder as a clock source
Files:
1 added
12 edited

Legend:

Unmodified
Added
Removed
  • apps/ministreaming/java/src/net/i2p/client/streaming/I2PSocketEepGet.java

    r3f3385f rf11a543  
    6060        super(ctx, false, null, -1, numRetries, minSize, maxSize, outputFile, outputStream, url, true, null, null);
    6161        _socketManager = mgr;
    62         _log = ctx.logManager().getLog(I2PSocketEepGet.class);
    6362    }
    6463   
  • apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java

    r3f3385f rf11a543  
    44import java.io.FileInputStream;
    55import java.io.IOException;
    6 import java.text.ParseException;
    76import java.text.SimpleDateFormat;
    87import java.util.Date;
    98import java.util.List;
    10 import java.util.Locale;
    119
    1210import net.i2p.I2PAppContext;
     
    1614import net.i2p.router.RouterContext;
    1715import net.i2p.router.RouterVersion;
     16import net.i2p.router.util.RFC822Date;
    1817import net.i2p.util.EepGet;
    1918import net.i2p.util.EepHead;
     
    7473                _lastFetch = _lastUpdated;
    7574            if (_lastModified == null)
    76                 _lastModified = to822Date(_lastFetch);
     75                _lastModified = RFC822Date.to822Date(_lastFetch);
    7776        } else {
    7877            _lastUpdated = 0;
     
    213212                if (lastmod != null) {
    214213                    if (!(_context.isRouterContext())) return;
    215                     long modtime = parse822Date(lastmod);
     214                    long modtime = RFC822Date.parse822Date(lastmod);
    216215                    if (modtime <= 0) return;
    217216                    String lastUpdate = _context.getProperty(UpdateHandler.PROP_LAST_UPDATE_TIME);
     
    250249                                                          _unsignedUpdateVersion);
    251250        handler.update();
    252     }
    253 
    254     /**
    255      * http://jimyjoshi.com/blog/2007/08/rfc822dateparsinginjava.html
    256      * Apparently public domain
    257      * Probably don't need all of these...
    258      */
    259     private static final SimpleDateFormat rfc822DateFormats[] = new SimpleDateFormat[] {
    260                  new SimpleDateFormat("EEE, d MMM yy HH:mm:ss z", Locale.US),
    261                  new SimpleDateFormat("EEE, d MMM yy HH:mm z", Locale.US),
    262                  new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US),
    263                  new SimpleDateFormat("EEE, d MMM yyyy HH:mm z", Locale.US),
    264                  new SimpleDateFormat("d MMM yy HH:mm z", Locale.US),
    265                  new SimpleDateFormat("d MMM yy HH:mm:ss z", Locale.US),
    266                  new SimpleDateFormat("d MMM yyyy HH:mm z", Locale.US),
    267                  new SimpleDateFormat("d MMM yyyy HH:mm:ss z", Locale.US)
    268     };
    269 
    270     /**
    271      * new Date(String foo) is deprecated, so let's do this the hard way
    272      *
    273      * @param s non-null
    274      * @return -1 on failure
    275      */
    276     public static long parse822Date(String s) {
    277         for (int i = 0; i < rfc822DateFormats.length; i++) {
    278             try {
    279                 Date date = rfc822DateFormats[i].parse(s);
    280                 if (date != null)
    281                     return date.getTime();
    282             } catch (ParseException pe) {}
    283         }
    284         return -1;
    285     }
    286 
    287     /** @since 0.8.2 */
    288     private static String to822Date(long t) {
    289         return (new SimpleDateFormat("d MMM yyyy HH:mm:ss z", Locale.US)).format(new Date(t));
    290251    }
    291252
  • apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java

    r3f3385f rf11a543  
    55import net.i2p.router.Router;
    66import net.i2p.router.RouterContext;
     7import net.i2p.router.util.RFC822Date;
    78import net.i2p.util.EepGet;
    89import net.i2p.util.FileUtil;
     
    102103                long modtime = 0;
    103104                if (lastmod != null)
    104                     modtime = NewsFetcher.parse822Date(lastmod);
     105                    modtime = RFC822Date.parse822Date(lastmod);
    105106                if (modtime <= 0)
    106107                    modtime = _context.clock().now();
  • apps/routerconsole/java/src/net/i2p/router/web/UpdateHandler.java

    r3f3385f rf11a543  
    1616import net.i2p.router.RouterContext;
    1717import net.i2p.router.RouterVersion;
     18import net.i2p.router.util.RFC822Date;
    1819import net.i2p.util.EepGet;
    1920import net.i2p.util.I2PAppThread;
     
    272273                long modtime = 0;
    273274                if (lastmod != null)
    274                     modtime = NewsFetcher.parse822Date(lastmod);
     275                    modtime = RFC822Date.parse822Date(lastmod);
    275276                if (modtime <= 0)
    276277                    modtime = _context.clock().now();
  • core/java/src/net/i2p/time/Timestamper.java

    r3f3385f rf11a543  
    55import java.util.Locale;
    66import java.util.StringTokenizer;
     7import java.util.concurrent.CopyOnWriteArrayList;
    78
    89import net.i2p.I2PAppContext;
     
    5758        // moved here to prevent problems with synchronized statements.
    5859        _servers = new ArrayList(3);
    59         _listeners = new ArrayList(1);
     60        _listeners = new CopyOnWriteArrayList();
    6061        // Don't bother starting a thread if we are disabled.
    6162        // This means we no longer check every 5 minutes to see if we got enabled,
     
    9394   
    9495    public void addListener(UpdateListener lsnr) {
    95         synchronized (_listeners) {
    9696            _listeners.add(lsnr);
    97         }
    9897    }
    9998    public void removeListener(UpdateListener lsnr) {
    100         synchronized (_listeners) {
    10199            _listeners.remove(lsnr);
    102         }
    103100    }
    104101    public int getListenerCount() {
    105         synchronized (_listeners) {
    106102            return _listeners.size();
    107         }
    108103    }
    109104    public UpdateListener getListener(int index) {
    110         synchronized (_listeners) {
    111105            return _listeners.get(index);
    112         }
    113106    }
    114107   
     
    258251    private void stampTime(long now, int stratum) {
    259252        long before = _context.clock().now();
    260         synchronized (_listeners) {
    261             for (int i = 0; i < _listeners.size(); i++) {
    262                 UpdateListener lsnr = _listeners.get(i);
    263                 lsnr.setNow(now, stratum);
    264             }
     253        for (UpdateListener lsnr : _listeners) {
     254             lsnr.setNow(now, stratum);
    265255        }
    266256        if (_log.shouldLog(Log.DEBUG))
  • core/java/src/net/i2p/util/Clock.java

    r3f3385f rf11a543  
    5050    public final static long MIN_OFFSET_CHANGE = 5 * 1000;
    5151
     52    /**
     53     * Specify how far away from the "correct" time the computer is - a positive
     54     * value means that the system time is slow, while a negative value means the system time is fast.
     55     *
     56     * @param offsetMs the delta from System.currentTimeMillis() (NOT the delta from now())
     57     */
    5258    public void setOffset(long offsetMs) {
    5359        setOffset(offsetMs, false);       
     
    5662    /**
    5763     * Specify how far away from the "correct" time the computer is - a positive
    58      * value means that we are slow, while a negative value means we are fast.
     64     * value means that the system time is slow, while a negative value means the system time is fast.
    5965     * Warning - overridden in RouterClock
     66     *
     67     * @param offsetMs the delta from System.currentTimeMillis() (NOT the delta from now())
    6068     */
    6169    public void setOffset(long offsetMs, boolean force) {
     
    102110    }
    103111
     112    /*
     113     * @return the current delta from System.currentTimeMillis() in milliseconds
     114     */
    104115    public long getOffset() {
    105116        return _offset;
  • core/java/src/net/i2p/util/EepGet.java

    r3f3385f rf11a543  
    2929 */
    3030public class EepGet {
    31     protected I2PAppContext _context;
    32     protected Log _log;
    33     protected boolean _shouldProxy;
    34     private String _proxyHost;
    35     private int _proxyPort;
    36     protected int _numRetries;
    37     private long _minSize; // minimum and maximum acceptable response size, -1 signifies unlimited,
    38     private long _maxSize; // applied both against whole responses and chunks
    39     protected String _outputFile;
    40     protected OutputStream _outputStream;
     31    protected final I2PAppContext _context;
     32    protected final Log _log;
     33    protected final boolean _shouldProxy;
     34    private final String _proxyHost;
     35    private final int _proxyPort;
     36    protected final int _numRetries;
     37    private final long _minSize; // minimum and maximum acceptable response size, -1 signifies unlimited,
     38    private final long _maxSize; // applied both against whole responses and chunks
     39    protected final String _outputFile;
     40    protected final OutputStream _outputStream;
    4141    /** url we were asked to fetch */
    42     protected String _url;
     42    protected final String _url;
    4343    /** the URL we actually fetch from (may differ from the _url in case of redirect) */
    4444    protected String _actualURL;
    45     private String _postData;
     45    private final String _postData;
    4646    private boolean _allowCaching;
    4747    protected final List<StatusListener> _listeners;
     
    107107                  String etag, String lastModified, String postData) {
    108108        _context = ctx;
    109         _log = ctx.logManager().getLog(EepGet.class);
     109        _log = ctx.logManager().getLog(getClass());
    110110        _shouldProxy = (proxyHost != null) && (proxyHost.length() > 0) && (proxyPort > 0) && shouldProxy;
    111111        _proxyHost = proxyHost;
     
    119119        _actualURL = url;
    120120        _postData = postData;
    121         _alreadyTransferred = 0;
    122         _bytesTransferred = 0;
    123121        _bytesRemaining = -1;
    124         _currentAttempt = 0;
    125         _transferFailed = false;
    126         _headersRead = false;
    127         _aborted = false;
    128122        _fetchHeaderTimeout = CONNECT_TIMEOUT;
    129123        _listeners = new ArrayList(1);
     
    256250    }
    257251    protected class CLIStatusListener implements StatusListener {
    258         private int _markSize;
    259         private int _lineSize;
    260         private long _startedOn;
     252        private final int _markSize;
     253        private final int _lineSize;
     254        private final long _startedOn;
    261255        private long _written;
    262256        private long _previousWritten;
     
    272266            _markSize = markSize;
    273267            _lineSize = lineSize;
    274             _written = 0;
    275             _previousWritten = 0;
    276             _discarded = 0;
    277268            _lastComplete = _context.clock().now();
    278269            _startedOn = _lastComplete;
  • router/java/src/net/i2p/router/RouterClock.java

    r3f3385f rf11a543  
    2525     */
    2626    private static final long MAX_SLEW = 50;
    27     private static final int DEFAULT_STRATUM = 8;
     27    public static final int DEFAULT_STRATUM = 8;
    2828    private static final int WORST_STRATUM = 16;
    2929    /** the max NTP Timestamper delay is 30m right now, make this longer than that */
     
    4545    /**
    4646     * Specify how far away from the "correct" time the computer is - a positive
    47      * value means that we are slow, while a negative value means we are fast.
     47     * value means that the system time is slow, while a negative value means the system time is fast.
    4848     *
     49     * @param offsetMs the delta from System.currentTimeMillis() (NOT the delta from now())
    4950     */
    5051    @Override
     
    5354    }
    5455
    55     /** @since 0.7.12 */
     56    /**
     57     * @since 0.7.12
     58     * @param offsetMs the delta from System.currentTimeMillis() (NOT the delta from now())
     59     */
    5660    private void setOffset(long offsetMs, int stratum) {
    5761         setOffset(offsetMs, false, stratum);
    5862    }
    5963
    60     /** @since 0.7.12 */
     64    /**
     65     * @since 0.7.12
     66     * @param offsetMs the delta from System.currentTimeMillis() (NOT the delta from now())
     67     */
    6168    private void setOffset(long offsetMs, boolean force, int stratum) {
    6269        long delta = offsetMs - _offset;
     
    9299           
    93100            // If so configured, check sanity of proposed clock offset
    94             if (Boolean.valueOf(_contextRC.getProperty("router.clockOffsetSanityCheck","true")).booleanValue() &&
     101            if (_contextRC.getBooleanPropertyDefaultTrue("router.clockOffsetSanityCheck") &&
    95102                _alreadyChanged) {
    96103
     
    193200     *  How far we still have to slew, for diagnostics
    194201     *  @since 0.7.12
     202     *  @deprecated for debugging only
    195203     */
    196204    public long getDeltaOffset() {
  • router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java

    r3f3385f rf11a543  
    1515
    1616import net.i2p.I2PAppContext;
     17import net.i2p.data.DataHelper;
     18import net.i2p.router.RouterClock;
    1719import net.i2p.router.RouterContext;
     20import net.i2p.router.util.RFC822Date;
    1821import net.i2p.util.EepGet;
    1922import net.i2p.util.I2PAppThread;
     
    3538 */
    3639public class Reseeder {
     40    /** FIXME don't keep a static reference, store _isRunning some other way */
    3741    private static ReseedRunner _reseedRunner;
    3842    private final RouterContext _context;
     
    4751     *  NOTE - URLs in both the standard and SSL groups should use the same hostname and path,
    4852     *         so the reseed process will not download from both.
     53     *
     54     *  NOTE - Each seedURL must be a directory, it must end with a '/',
     55     *         it can't end with 'index.html', for example. Both because of how individual file
     56     *         URLs are constructed, and because SSLEepGet doesn't follow redirects.
    4957     */
    5058    public static final String DEFAULT_SEED_URL =
     
    99107        private int _proxyPort;
    100108        private SSLEepGet.SSLState _sslState;
     109        private int _gotDate;
     110        private long _attemptStarted;
     111        private static final int MAX_DATE_SETS = 2;
    101112
    102113        public ReseedRunner() {
    103             _isRunning = false;
    104             System.clearProperty(PROP_ERROR);
    105             System.setProperty(PROP_STATUS, _("Reseeding"));
    106             System.setProperty(PROP_INPROGRESS, "true");
    107         }
     114        }
     115
    108116        public boolean isRunning() { return _isRunning; }
    109117
     
    114122        public void run() {
    115123            _isRunning = true;
     124            System.clearProperty(PROP_ERROR);
     125            System.setProperty(PROP_STATUS, _("Reseeding"));
     126            System.setProperty(PROP_INPROGRESS, "true");
     127            _attemptStarted = 0;
     128            _gotDate = 0;
    116129            _sslState = null;  // start fresh
    117130            if (_context.getBooleanProperty(PROP_PROXY_ENABLE)) {
     
    153166        public void transferComplete(long alreadyTransferred, long bytesTransferred, long bytesRemaining, String url, String outputFile, boolean notModified) {}
    154167        public void transferFailed(String url, long bytesTransferred, long bytesRemaining, int currentAttempt) {}
    155         public void headerReceived(String url, int attemptNum, String key, String val) {}
    156         public void attempting(String url) {}
     168
     169        /**
     170         *  Use the Date header as a backup time source
     171         */
     172        public void headerReceived(String url, int attemptNum, String key, String val) {
     173            // We do this more than once, because
     174            // the first SSL handshake may take a while, and it may take the server
     175            // a while to render the index page.
     176            if (_gotDate < MAX_DATE_SETS && "date".equalsIgnoreCase(key) && _attemptStarted > 0) {
     177                long timeRcvd = System.currentTimeMillis();
     178                long serverTime = RFC822Date.parse822Date(val);
     179                if (serverTime > 0) {
     180                    // add 500ms since it's 1-sec resolution, and add half the RTT
     181                    long now = serverTime + 500 + ((timeRcvd - _attemptStarted) / 2);
     182                    long offset = now - _context.clock().now();
     183                    if (_context.clock().getUpdatedSuccessfully()) {
     184                        // 2nd time better than the first
     185                        if (_gotDate > 0)
     186                            _context.clock().setNow(now, RouterClock.DEFAULT_STRATUM - 2);
     187                        else
     188                            _context.clock().setNow(now, RouterClock.DEFAULT_STRATUM - 1);
     189                        if (_log.shouldLog(Log.WARN))
     190                            _log.warn("Reseed adjusting clock by " +
     191                                      DataHelper.formatDuration(Math.abs(offset)));
     192                    } else {
     193                        // No peers or NTP yet, this is probably better than the peer average will be for a while
     194                        // default stratum - 1, so the peer average is a worse stratum
     195                        _context.clock().setNow(now, RouterClock.DEFAULT_STRATUM - 1);
     196                        _log.logAlways(Log.WARN, "NTP failure, Reseed adjusting clock by " +
     197                                                 DataHelper.formatDuration(Math.abs(offset)));
     198                    }
     199                    _gotDate++;
     200                }
     201            }
     202        }
     203
     204        /** save the start time */
     205        public void attempting(String url) {
     206            if (_gotDate < MAX_DATE_SETS)
     207                _attemptStarted = System.currentTimeMillis();
     208        }
     209
    157210        // End of EepGet status listeners
    158211
     
    236289        private int reseedOne(String seedURL, boolean echoStatus) {
    237290            try {
    238                 final long timeLimit = _context.clock().now() + MAX_TIME_PER_HOST;
     291                // Don't use context clock as we may be adjusting the time
     292                final long timeLimit = System.currentTimeMillis() + MAX_TIME_PER_HOST;
    239293                System.setProperty(PROP_STATUS, _("Reseeding: fetching seed URL."));
    240294                System.err.println("Reseeding from " + seedURL);
     
    276330                // 200 max from one URL
    277331                for (Iterator<String> iter = urlList.iterator();
    278                      iter.hasNext() && fetched < 200 && _context.clock().now() < timeLimit; ) {
     332                     iter.hasNext() && fetched < 200 && System.currentTimeMillis() < timeLimit; ) {
    279333                    try {
    280334                        System.setProperty(PROP_STATUS,
  • router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java

    r3f3385f rf11a543  
    510510        boolean found = _context.netDb().lookupRouterInfoLocally(peer) != null;
    511511        if (found)
    512             buf.append("<a title=\"").append(_("NetDb entry")).append("\" href=\"netdb.jsp?r=").append(h).append("\">");
     512            buf.append("<a title=\"").append(_("NetDb entry")).append("\" href=\"netdb?r=").append(h).append("\">");
    513513        buf.append(h);
    514514        if (found)
  • router/java/src/net/i2p/router/transport/ntcp/EstablishState.java

    r3f3385f rf11a543  
    400400                    _tsA = _tsB;
    401401                    if (diff != 0)
    402                         _log.error("NTP failure, NTCP adjusting clock by " + DataHelper.formatDuration(diff));
     402                        _log.logAlways(Log.WARN, "NTP failure, NTCP adjusting clock by " + DataHelper.formatDuration(diff));
    403403                } else if (diff >= Router.CLOCK_FUDGE_FACTOR) {
    404404                    _context.statManager().addRateData("ntcp.invalidOutboundSkew", diff, 0);
     
    618618                    tsA = _tsB;
    619619                    if (diff != 0)
    620                         _log.error("NTP failure, NTCP adjusting clock by " + DataHelper.formatDuration(diff));
     620                        _log.logAlways(Log.WARN, "NTP failure, NTCP adjusting clock by " + DataHelper.formatDuration(diff));
    621621                } else if (diff >= Router.CLOCK_FUDGE_FACTOR) {
    622622                    _context.statManager().addRateData("ntcp.invalidInboundSkew", diff, 0);
  • router/java/src/net/i2p/router/transport/udp/PacketHandler.java

    r3f3385f rf11a543  
    474474                _context.clock().setOffset(0 - skew, true);
    475475                if (skew != 0)
    476                     _log.error("NTP failure, UDP adjusting clock by " + DataHelper.formatDuration(Math.abs(skew)));
     476                    _log.logAlways(Log.WARN, "NTP failure, UDP adjusting clock by " + DataHelper.formatDuration(Math.abs(skew)));
    477477            }
    478478
Note: See TracChangeset for help on using the changeset viewer.