Changeset e7b0691


Ignore:
Timestamp:
May 25, 2009 3:45:09 AM (11 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
7f4c23a0
Parents:
71b708b (diff), 235058e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

propagate from branch 'i2p.i2p.zzz.test' (head ea09c1bd1791674541854e77592153db6678aabb)

to branch 'i2p.i2p' (head 7ea5131c7a02f28ac2d3e23a8bf22558effb30ee)

Files:
9 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/time/Timestamper.java

    r71b708b re7b0691  
    1818    private I2PAppContext _context;
    1919    private Log _log;
    20     private List _servers;
    21     private List _listeners;
     20    private List<String> _servers;
     21    private List<String> _priorityServers;
     22    private List<UpdateListener> _listeners;
    2223    private int _queryFrequency;
    2324    private int _concurringServers;
     
    2526    private boolean _daemon;
    2627    private boolean _initialized;
    27    
     28    private boolean _wellSynced;
     29   
     30    private static final int MIN_QUERY_FREQUENCY = 5*60*1000;
    2831    private static final int DEFAULT_QUERY_FREQUENCY = 5*60*1000;
    29     private static final String DEFAULT_SERVER_LIST = "0.pool.ntp.org, 1.pool.ntp.org, 2.pool.ntp.org";
    30     private static final boolean DEFAULT_DISABLED = true;
     32    private static final String DEFAULT_SERVER_LIST = "0.pool.ntp.org,1.pool.ntp.org,2.pool.ntp.org";
     33    private static final String DEFAULT_DISABLED = "true";
    3134    /** how many times do we have to query if we are changing the clock? */
    3235    private static final int DEFAULT_CONCURRING_SERVERS = 3;
     
    3639    public static final String PROP_DISABLED = "time.disabled";
    3740    public static final String PROP_CONCURRING_SERVERS = "time.concurringServers";
     41    public static final String PROP_IP_COUNTRY = "i2np.lastCountry";
    3842   
    3943    /** if different SNTP servers differ by more than 10s, someone is b0rked */
     
    5155        _daemon = daemon;
    5256        _initialized = false;
    53         _servers = new ArrayList(1);
     57        _wellSynced = false;
     58        _servers = new ArrayList(3);
    5459        _listeners = new ArrayList(1);
    5560        if (lsnr != null)
     
    116121        if (_log.shouldLog(Log.INFO))
    117122            _log.info("Starting timestamper");
    118 
    119         if (_log.shouldLog(Log.INFO))
    120             _log.info("Starting up timestamper");
    121123        boolean lastFailed = false;
    122124        try {
     
    124126                updateConfig();
    125127                if (!_disabled) {
    126                     String serverList[] = null;
    127                     synchronized (_servers) {
    128                         serverList = new String[_servers.size()];
    129                         for (int i = 0; i < serverList.length; i++)
    130                             serverList[i] = (String)_servers.get(i);
     128                    // first the servers for our country, if we know what country we're in...
     129                    if (_priorityServers != null) {
     130                        if (_log.shouldLog(Log.DEBUG))
     131                            _log.debug("Querying servers " + _priorityServers);
     132                        try {
     133                            lastFailed = !queryTime(_priorityServers.toArray(new String[_priorityServers.size()]));
     134                        } catch (IllegalArgumentException iae) {
     135                            if ( (!lastFailed) && (_log.shouldLog(Log.WARN)) )
     136                                _log.warn("Unable to reach country-specific NTP servers");
     137                            lastFailed = true;
     138                        }
    131139                    }
    132                     if (_log.shouldLog(Log.DEBUG))
    133                         _log.debug("Querying servers " + _servers);
    134                     try {
    135                         lastFailed = !queryTime(serverList);
    136                     } catch (IllegalArgumentException iae) {
    137                         if ( (!lastFailed) && (_log.shouldLog(Log.ERROR)) )
    138                             _log.error("Unable to reach any of the NTP servers - network disconnected?");
    139                         lastFailed = true;
     140                    // ... and then the global list, if that failed
     141                    if (_priorityServers == null || lastFailed) {
     142                        if (_log.shouldLog(Log.DEBUG))
     143                            _log.debug("Querying servers " + _servers);
     144                        try {
     145                            lastFailed = !queryTime(_servers.toArray(new String[_servers.size()]));
     146                        } catch (IllegalArgumentException iae) {
     147                            if ( (!lastFailed) && (_log.shouldLog(Log.ERROR)) )
     148                                _log.error("Unable to reach any of the NTP servers - network disconnected?");
     149                            lastFailed = true;
     150                        }
    140151                    }
    141152                }
     
    143154                _initialized = true;
    144155                synchronized (this) { notifyAll(); }
    145                 long sleepTime = _context.random().nextInt(_queryFrequency) + _queryFrequency;
    146                 if (lastFailed)
     156                long sleepTime;
     157                if (lastFailed) {
    147158                    sleepTime = 30*1000;
     159                } else {
     160                    sleepTime = _context.random().nextInt(_queryFrequency) + _queryFrequency;
     161                    if (_wellSynced)
     162                        sleepTime *= 3;
     163                }
    148164                try { Thread.sleep(sleepTime); } catch (InterruptedException ie) {}
    149165            }
     
    161177        long now = -1;
    162178        long expectedDelta = 0;
     179        _wellSynced = false;
    163180        for (int i = 0; i < _concurringServers; i++) {
    164181            try { Thread.sleep(10*1000); } catch (InterruptedException ie) {}
     
    170187                    if (_log.shouldLog(Log.INFO))
    171188                        _log.info("a single SNTP query was within the tolerance (" + delta + "ms)");
     189                    // If less than a half second on the first try, we're in good shape
     190                    _wellSynced = Math.abs(delta) < 500;
    172191                    break;
    173192                } else {
     
    225244    private void updateConfig() {
    226245        String serverList = _context.getProperty(PROP_SERVER_LIST);
    227         if ( (serverList == null) || (serverList.trim().length() <= 0) )
     246        if ( (serverList == null) || (serverList.trim().length() <= 0) ) {
    228247            serverList = DEFAULT_SERVER_LIST;
    229         synchronized (_servers) {
    230             _servers.clear();
    231             StringTokenizer tok = new StringTokenizer(serverList, ",");
    232             while (tok.hasMoreTokens()) {
    233                 String val = (String)tok.nextToken();
    234                 val = val.trim();
    235                 if (val.length() > 0)
    236                     _servers.add(val);
    237             }
    238         }
    239        
    240         String freq = _context.getProperty(PROP_QUERY_FREQUENCY);
    241         if ( (freq == null) || (freq.trim().length() <= 0) )
    242             freq = DEFAULT_QUERY_FREQUENCY + "";
    243         try {
    244             int ms = Integer.parseInt(freq);
    245             if (ms > 60*1000) {
    246                 _queryFrequency = ms;
     248            String country = _context.getProperty(PROP_IP_COUNTRY);
     249            if (country != null) {
     250                _priorityServers = new ArrayList(3);
     251                for (int i = 0; i < 3; i++)
     252                     _priorityServers.add(i + "." + country + ".pool.ntp.org");
    247253            } else {
    248                 if ( (_log != null) && (_log.shouldLog(Log.ERROR)) )
    249                     _log.error("Query frequency once every " + ms + "ms is too fast!");
    250                 _queryFrequency = DEFAULT_QUERY_FREQUENCY;
    251             }
    252         } catch (NumberFormatException nfe) {
    253             if ( (_log != null) && (_log.shouldLog(Log.WARN)) )
    254                 _log.warn("Invalid query frequency [" + freq + "], falling back on " + DEFAULT_QUERY_FREQUENCY);
    255             _queryFrequency = DEFAULT_QUERY_FREQUENCY;
    256         }
    257        
    258         String disabled = _context.getProperty(PROP_DISABLED);
    259         if (disabled == null)
    260             disabled = DEFAULT_DISABLED + "";
     254                _priorityServers = null;
     255            }
     256        } else {
     257            _priorityServers = null;
     258        }
     259        _servers.clear();
     260        StringTokenizer tok = new StringTokenizer(serverList, ", ");
     261        while (tok.hasMoreTokens()) {
     262            String val = (String)tok.nextToken();
     263            val = val.trim();
     264            if (val.length() > 0)
     265                _servers.add(val);
     266        }
     267       
     268        _queryFrequency = Math.max(MIN_QUERY_FREQUENCY,
     269                                   _context.getProperty(PROP_QUERY_FREQUENCY, DEFAULT_QUERY_FREQUENCY));
     270       
     271        String disabled = _context.getProperty(PROP_DISABLED, DEFAULT_DISABLED);
    261272        _disabled = Boolean.valueOf(disabled).booleanValue();
    262273       
    263         String concurring = _context.getProperty(PROP_CONCURRING_SERVERS);
    264         if (concurring == null) {
    265             _concurringServers = DEFAULT_CONCURRING_SERVERS;
    266         } else {
    267             try {
    268                 int servers = Integer.parseInt(concurring);
    269                 if ( (servers > 0) && (servers < 5) )
    270                     _concurringServers = servers;
    271                 else
    272                     _concurringServers = DEFAULT_CONCURRING_SERVERS;
    273             } catch (NumberFormatException nfe) {
    274                 _concurringServers = DEFAULT_CONCURRING_SERVERS;
    275             }
    276         }
     274        _concurringServers = Math.min(4, Math.max(1,
     275                              _context.getProperty(PROP_CONCURRING_SERVERS, DEFAULT_CONCURRING_SERVERS)));
    277276    }
    278277   
  • router/java/src/net/i2p/router/DummyNetworkDatabaseFacade.java

    r71b708b re7b0691  
    5959    }
    6060   
     61    public Set<Hash> getAllRouters() { return new HashSet(_routers.keySet()); }
    6162    public Set findNearestRouters(Hash key, int maxNumRouters, Set peersToIgnore) { return new HashSet(_routers.values()); }
    6263
  • router/java/src/net/i2p/router/NetworkDatabaseFacade.java

    r71b708b re7b0691  
    5656    public abstract void fail(Hash dbEntry);
    5757   
     58    public abstract Set<Hash> getAllRouters();
    5859    public int getKnownRouters() { return 0; }
    5960    public int getKnownLeaseSets() { return 0; }
  • router/java/src/net/i2p/router/networkdb/kademlia/KademliaNetworkDatabaseFacade.java

    r71b708b re7b0691  
    312312   
    313313    /** get the hashes for all known routers */
    314     Set getAllRouters() {
     314    public Set<Hash> getAllRouters() {
    315315        if (!_initialized) return new HashSet(0);
    316316        Set keys = _ds.getKeys();
  • router/java/src/net/i2p/router/peermanager/ProfileOrganizer.java

    r71b708b re7b0691  
    7777    public static final String PROP_MINIMUM_FAST_PEERS = "profileOrganizer.minFastPeers";
    7878    public static final int DEFAULT_MINIMUM_FAST_PEERS = 8;
     79    private static final int DEFAULT_MAXIMUM_FAST_PEERS = 16;
    7980    /**
    8081     * Defines the minimum number of 'high capacity' peers that the organizer should
     
    11421143     * peers are particularly fast.
    11431144     *
     1145     * Increase default by two for every local destination, up to a max.
     1146     *
    11441147     * @return minimum number of peers to be placed in the 'fast' group
    11451148     */
    11461149    protected int getMinimumFastPeers() {
    1147         return _context.getProperty(PROP_MINIMUM_FAST_PEERS, DEFAULT_MINIMUM_FAST_PEERS);
     1150        int def = Math.min(DEFAULT_MAXIMUM_FAST_PEERS,
     1151                           (2 *_context.clientManager().listClients().size()) + DEFAULT_MINIMUM_FAST_PEERS - 2);
     1152        return _context.getProperty(PROP_MINIMUM_FAST_PEERS, def);
    11481153    }
    11491154   
  • router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java

    r71b708b re7b0691  
    376376     * Collect the IPs for all routers in the DB, and queue them for lookup,
    377377     * then fire off the periodic lookup task for the first time.
    378      *
    379      * We could use getAllRouters() if it were public, and that would be faster, but
    380      * we only do this once.
    381378     */
    382379    private class QueueAll implements SimpleTimer.TimedEvent {
    383380        public void timeReached() {
    384             Set routers = _context.netDb().findNearestRouters(_context.routerHash(), _context.netDb().getKnownRouters(), null);
    385             for (Iterator iter = routers.iterator(); iter.hasNext(); ) {
    386                 RouterInfo ri = (RouterInfo) iter.next();
     381            for (Iterator<Hash> iter = _context.netDb().getAllRouters().iterator(); iter.hasNext(); ) {
     382                RouterInfo ri = _context.netDb().lookupRouterInfoLocally(iter.next());
     383                if (ri == null)
     384                    continue;
    387385                String host = getIPString(ri);
    388386                if (host == null)
     
    407405     *  Uses the transport IP first because that lookup is fast,
    408406     *  then the SSU IP from the netDb.
     407     *
     408     *  @return two-letter lower-case country code or null
    409409     */
    410410    public String getCountry(Hash peer) {
  • router/java/src/net/i2p/router/transport/GeoIP.java

    r71b708b re7b0691  
    1616import java.util.concurrent.atomic.AtomicBoolean;
    1717
    18 import net.i2p.I2PAppContext;
    1918import net.i2p.data.DataHelper;
     19import net.i2p.router.RouterContext;
    2020import net.i2p.util.ConcurrentHashSet;
    2121import net.i2p.util.Log;
     
    3838public class GeoIP {
    3939    private Log _log;
    40     private I2PAppContext _context;
     40    private RouterContext _context;
    4141    private final Map<String, String> _codeToName;
    4242    private final Map<Long, String> _IPToCountry;
     
    4545    private final AtomicBoolean _lock;
    4646   
    47     public GeoIP(I2PAppContext context) {
     47    public GeoIP(RouterContext context) {
    4848        _context = context;
    4949        _log = context.logManager().getLog(GeoIP.class);
     
    6060    static final String GEOIP_FILE_DEFAULT = "geoip.txt";
    6161    static final String COUNTRY_FILE_DEFAULT = "countries.txt";
     62    public static final String PROP_IP_COUNTRY = "i2np.lastCountry";
    6263
    6364    /**
     
    8889        LookupJob j = new LookupJob();
    8990        j.run();
     91        updateOurCountry();
    9092    }
    9193
     
    234236
    235237    /**
     238     *  Put our country code in the config, where others (such as Timestamper) can get it,
     239     *  and it will be there next time at startup.
     240     */
     241    private void updateOurCountry() {
     242        String oldCountry = _context.router().getConfigSetting(PROP_IP_COUNTRY);
     243        String country = _context.commSystem().getCountry(_context.routerHash());
     244        if (country != null && !country.equals(oldCountry)) {
     245            _context.router().setConfigSetting(PROP_IP_COUNTRY, country);
     246            _context.router().saveConfig();
     247        }
     248    }
     249
     250    /**
    236251     * Add to the list needing lookup
    237252     */
     
    297312    }
    298313
     314/*** doesn't work since switched to RouterContext above
    299315    public static void main(String args[]) {
    300316        GeoIP g = new GeoIP(new I2PAppContext());
     
    310326
    311327    }
     328***/
    312329}
  • router/java/src/net/i2p/router/transport/ntcp/NTCPConnection.java

    r71b708b re7b0691  
    104104   
    105105    private static final int META_FREQUENCY = 10*60*1000;
    106     private static final int INFO_FREQUENCY = 6*60*60*1000;
     106    /** how often we send our routerinfo unsolicited */
     107    private static final int INFO_FREQUENCY = 90*60*1000;
    107108    /**
    108109     *  Why this is 16K, and where it is documented, good question?
     
    201202        _establishState = null;
    202203        _nextMetaTime = System.currentTimeMillis() + _context.random().nextInt(META_FREQUENCY);
    203         _nextInfoTime = System.currentTimeMillis() + INFO_FREQUENCY + _context.random().nextInt(INFO_FREQUENCY);
     204        _nextInfoTime = System.currentTimeMillis() + (INFO_FREQUENCY / 2) + _context.random().nextInt(INFO_FREQUENCY);
    204205    }
    205206    public long getClockSkew() { return _clockSkew; }
     
    371372            send(infoMsg);
    372373           
    373             enqueueFloodfillMessage(target);
     374            // See comment below
     375            //enqueueFloodfillMessage(target);
    374376        } else {
    375377            if (_isInbound) {
     
    381383    }
    382384
    383     private static final int PEERS_TO_FLOOD = 3;
     385    //private static final int PEERS_TO_FLOOD = 3;
    384386
    385387    /**
    386388     * to prevent people from losing track of the floodfill peers completely, lets periodically
    387389     * send those we are connected to references to the floodfill peers that we know
    388      */
     390     *
     391     * Do we really need this anymore??? Peers shouldn't lose track anymore, and if they do,
     392     * FloodOnlyLookupJob should recover.
     393     * The bandwidth isn't so much, but it is a lot of extra data at connection startup, which
     394     * hurts latency of new connections.
     395     */
     396/**********
    389397    private void enqueueFloodfillMessage(RouterInfo target) {
    390398        FloodfillNetworkDatabaseFacade fac = (FloodfillNetworkDatabaseFacade)_context.netDb();
     
    415423        }
    416424    }
     425***********/
    417426   
    418427    /**
     
    440449        }
    441450        _nextMetaTime = System.currentTimeMillis() + _context.random().nextInt(META_FREQUENCY);
    442         _nextInfoTime = System.currentTimeMillis() + INFO_FREQUENCY + _context.random().nextInt(INFO_FREQUENCY);
     451        _nextInfoTime = System.currentTimeMillis() + (INFO_FREQUENCY / 2) + _context.random().nextInt(INFO_FREQUENCY);
    443452        if (msgs)
    444453            _transport.getWriter().wantsWrite(this, "outbound established");
     
    466475     */
    467476    synchronized void prepareNextWrite() {
    468         if (FAST_LARGE)
     477        //if (FAST_LARGE)
    469478            prepareNextWriteFast();
    470         else
    471             prepareNextWriteSmall();
    472     }
     479        //else
     480        //    prepareNextWriteSmall();
     481    }
     482/**********  nobody's tried this one in years
    473483    private void prepareNextWriteSmall() {
    474484        if (_log.shouldLog(Log.DEBUG))
     
    564574        if (_nextInfoTime <= System.currentTimeMillis()) {
    565575            enqueueInfoMessage();
    566             _nextInfoTime = System.currentTimeMillis() + INFO_FREQUENCY + _context.random().nextInt(INFO_FREQUENCY);
    567         }
    568     }
     576            _nextInfoTime = System.currentTimeMillis() + (INFO_FREQUENCY / 2) + _context.random().nextInt(INFO_FREQUENCY);
     577        }
     578    }
     579**********/
    569580   
    570581    /**
     
    648659            // perhaps this should check to see if we are bw throttled, etc?
    649660            enqueueInfoMessage();
    650             _nextInfoTime = System.currentTimeMillis() + INFO_FREQUENCY + _context.random().nextInt(INFO_FREQUENCY);
     661            _nextInfoTime = System.currentTimeMillis() + (INFO_FREQUENCY / 2) + _context.random().nextInt(INFO_FREQUENCY);
    651662        }
    652663    }
  • router/java/src/net/i2p/router/transport/udp/UDPTransport.java

    r71b708b re7b0691  
    475475            if (!fixedPort)
    476476                _context.router().setConfigSetting(PROP_EXTERNAL_PORT, ourPort+"");
     477            // queue a country code lookup of the new IP
     478            _context.commSystem().queueLookup(ourIP);
    477479            // store these for laptop-mode (change ident on restart... or every time... when IP changes)
    478480            _context.router().setConfigSetting(PROP_IP, _externalListenHost.getHostAddress());
Note: See TracChangeset for help on using the changeset viewer.