Changeset a687180


Ignore:
Timestamp:
Oct 2, 2010 4:30:07 PM (10 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
3b2aa94
Parents:
b1fd835
Message:

to prevent a rare concurrent exception

File:
1 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/transport/TransportManager.java

    rb1fd835 ra687180  
    2222import java.util.TreeMap;
    2323import java.util.Vector;
     24import java.util.concurrent.ConcurrentHashMap;
    2425
    2526import net.i2p.data.Hash;
     
    3738public class TransportManager implements TransportEventListener {
    3839    private Log _log;
    39     private List<Transport> _transports;
     40    /**
     41     * Converted from List to prevent concurrent modification exceptions.
     42     * If we want more than one transport with the same style we will have to change this.
     43     */
     44    private Map<String, Transport> _transports;
    4045    private RouterContext _context;
    4146    private UPnPManager _upnpManager;
     
    5762        _context.statManager().createRateStat("transport.bidFailNoTransports", "Could not attempt to bid on message, as none of the transports could attempt it", "Transport", new long[] { 60*1000, 10*60*1000, 60*60*1000 });
    5863        _context.statManager().createRateStat("transport.bidFailAllTransports", "Could not attempt to bid on message, as all of the transports had failed", "Transport", new long[] { 60*1000, 10*60*1000, 60*60*1000 });
    59         _transports = new ArrayList();
     64        _transports = new ConcurrentHashMap(2);
    6065        if (Boolean.valueOf(_context.getProperty(PROP_ENABLE_UPNP, "true")).booleanValue())
    6166            _upnpManager = new UPnPManager(context, this);
     
    6469    public void addTransport(Transport transport) {
    6570        if (transport == null) return;
    66         _transports.add(transport);
     71        _transports.put(transport.getStyle(), transport);
    6772        transport.setListener(this);
    6873    }
     
    7075    public void removeTransport(Transport transport) {
    7176        if (transport == null) return;
    72         _transports.remove(transport);
     77        _transports.remove(transport.getStyle());
    7378        transport.setListener(null);
    7479    }
     
    141146        configTransports();
    142147        _log.debug("Starting up the transport manager");
    143         for (int i = 0; i < _transports.size(); i++) {
    144             Transport t = _transports.get(i);
     148        for (Transport t : _transports.values()) {
    145149            RouterAddress addr = t.startListening();
    146150            if (_log.shouldLog(Log.DEBUG))
    147                 _log.debug("Transport " + i + " (" + t.getStyle() + ") started");
     151                _log.debug("Transport " + t.getStyle() + " started");
    148152        }
    149153        // kick UPnP - Do this to get the ports opened even before UDP registers an address
     
    162166        if (_upnpManager != null)
    163167            _upnpManager.stop();
    164         for (int i = 0; i < _transports.size(); i++) {
    165             _transports.get(i).stopListening();
     168        for (Transport t : _transports.values()) {
     169            t.stopListening();
    166170        }
    167171        _transports.clear();
     
    169173   
    170174    public Transport getTransport(String style) {
    171         for (int i = 0; i < _transports.size(); i++) {
    172             Transport t = _transports.get(i);
    173             if(style.equals(t.getStyle()))
    174                 return t;
    175         }
    176         return null;
     175        return _transports.get(style);
    177176    }
    178177   
     
    190189    public int countActivePeers() {
    191190        int peers = 0;
    192         for (int i = 0; i < _transports.size(); i++) {
    193             peers += _transports.get(i).countActivePeers();
     191        for (Transport t : _transports.values()) {
     192            peers += t.countActivePeers();
    194193        }
    195194        return peers;
     
    198197    public int countActiveSendPeers() {
    199198        int peers = 0;
    200         for (int i = 0; i < _transports.size(); i++) {
    201             peers += _transports.get(i).countActiveSendPeers();
     199        for (Transport t : _transports.values()) {
     200            peers += t.countActiveSendPeers();
    202201        }
    203202        return peers;
     
    211210      */
    212211    public boolean haveOutboundCapacity(int pct) {
    213         for (int i = 0; i < _transports.size(); i++) {
    214             if (_transports.get(i).haveCapacity(pct))
     212        for (Transport t : _transports.values()) {
     213            if (t.haveCapacity(pct))
    215214                return true;
    216215        }
     
    226225        if (_transports.isEmpty())
    227226            return false;
    228         for (int i = 0; i < _transports.size(); i++) {
    229             if (!_transports.get(i).haveCapacity(HIGH_CAPACITY_PCT))
     227        for (Transport t : _transports.values()) {
     228            if (!t.haveCapacity(HIGH_CAPACITY_PCT))
    230229                return false;
    231230        }
     
    240239      */
    241240    public boolean haveInboundCapacity(int pct) {
    242         for (int i = 0; i < _transports.size(); i++) {
    243             if (_transports.get(i).getCurrentAddress() != null && _transports.get(i).haveCapacity(pct))
     241        for (Transport t : _transports.values()) {
     242            if (t.getCurrentAddress() != null && t.haveCapacity(pct))
    244243                return true;
    245244        }
     
    254253    public Vector getClockSkews() {
    255254        Vector skews = new Vector();
    256         for (int i = 0; i < _transports.size(); i++) {
    257             Vector tempSkews = _transports.get(i).getClockSkews();
     255        for (Transport t : _transports.values()) {
     256            Vector tempSkews = t.getClockSkews();
    258257            if ((tempSkews == null) || (tempSkews.isEmpty())) continue;
    259258            skews.addAll(tempSkews);
     
    267266    public short getReachabilityStatus() {
    268267        short rv = CommSystemFacade.STATUS_UNKNOWN;
    269         for (Transport t : _transports) {
     268        for (Transport t : _transports.values()) {
    270269            short s = t.getReachabilityStatus();
    271270            if (s < rv)
     
    276275
    277276    public void recheckReachability() {
    278         for (int i = 0; i < _transports.size(); i++)
    279             _transports.get(i).recheckReachability();
     277        for (Transport t : _transports.values())
     278            t.recheckReachability();
    280279    }
    281280
    282281    public boolean isBacklogged(Hash dest) {
    283         for (int i = 0; i < _transports.size(); i++) {
    284             Transport t = _transports.get(i);
     282        for (Transport t : _transports.values()) {
    285283            if (t.isBacklogged(dest))
    286284                return true;
     
    290288   
    291289    public boolean isEstablished(Hash dest) {
    292         for (int i = 0; i < _transports.size(); i++) {
    293             Transport t = _transports.get(i);
     290        for (Transport t : _transports.values()) {
    294291            if (t.isEstablished(dest))
    295292                return true;
     
    304301     */
    305302    public boolean wasUnreachable(Hash dest) {
    306         for (int i = 0; i < _transports.size(); i++) {
    307             Transport t = _transports.get(i);
     303        for (Transport t : _transports.values()) {
    308304            if (!t.wasUnreachable(dest))
    309305                return false;
     
    331327        Map<String, RouterAddress> rv = new HashMap(_transports.size());
    332328        // do this first since SSU may force a NTCP change
    333         for (Transport t : _transports)
     329        for (Transport t : _transports.values())
    334330            t.updateAddress();
    335         for (Transport t : _transports) {
     331        for (Transport t : _transports.values()) {
    336332            if (t.getCurrentAddress() != null)
    337333                rv.put(t.getStyle(), t.getCurrentAddress());
     
    346342    private Map<String, Integer> getPorts() {
    347343        Map<String, Integer> rv = new HashMap(_transports.size());
    348         for (Transport t : _transports) {
     344        for (Transport t : _transports.values()) {
    349345            int port = t.getRequestedPort();
    350346            if (t.getCurrentAddress() != null) {
     
    387383        List<TransportBid> rv = new ArrayList(_transports.size());
    388384        Set failedTransports = msg.getFailedTransports();
    389         for (int i = 0; i < _transports.size(); i++) {
    390             Transport t = _transports.get(i);
     385        for (Transport t : _transports.values()) {
    391386            if (failedTransports.contains(t.getStyle())) {
    392387                if (_log.shouldLog(Log.DEBUG))
     
    416411        Set failedTransports = msg.getFailedTransports();
    417412        TransportBid rv = null;
    418         for (int i = 0; i < _transports.size(); i++) {
    419             Transport t = _transports.get(i);
     413        for (Transport t : _transports.values()) {
    420414            if (t.isUnreachable(peer)) {
    421415                unreachableTransports++;
     
    483477    public List getMostRecentErrorMessages() {
    484478        List rv = new ArrayList(16);
    485         for (int i = 0; i < _transports.size(); i++) {
    486             Transport t = _transports.get(i);
     479        for (Transport t : _transports.values()) {
    487480            rv.addAll(t.getMostRecentErrorMessages());
    488481        }
     
    492485    public void renderStatusHTML(Writer out, String urlBase, int sortFlags) throws IOException {
    493486        TreeMap transports = new TreeMap();
    494         for (int i = 0; i < _transports.size(); i++) {
    495             Transport t = _transports.get(i);
     487        for (Transport t : _transports.values()) {
    496488            transports.put(t.getStyle(), t);
    497489        }
     
    507499        StringBuilder buf = new StringBuilder(4*1024);
    508500        buf.append("<h3>").append(_("Router Transport Addresses")).append("</h3><pre>\n");
    509         for (int i = 0; i < _transports.size(); i++) {
    510             Transport t = _transports.get(i);
     501        for (Transport t : _transports.values()) {
    511502            if (t.getCurrentAddress() != null)
    512503                buf.append(t.getCurrentAddress());
Note: See TracChangeset for help on using the changeset viewer.