Changeset 7fe8573


Ignore:
Timestamp:
Dec 4, 2013 2:17:38 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
822ec4a
Parents:
445e430
Message:

i2psnark ConnectionAcceptor?:

  • Fix ConnectionAcceptor? not restarting after tunnel restart, preventing incoming connections
  • locking tweaks
  • cleaner reschedule tweaks
  • don't store server socket locally
  • constructor tweaks / finals
  • Stop acceptor when tunnel disconnects
  • javadocs
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/ConnectionAcceptor.java

    r445e430 r7fe8573  
    4242{
    4343  private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(ConnectionAcceptor.class);
    44   private I2PServerSocket serverSocket;
    45   private PeerAcceptor peeracceptor;
     44  private final PeerAcceptor peeracceptor;
    4645  private Thread thread;
    4746  private final I2PSnarkUtil _util;
     
    5049
    5150  private volatile boolean stop;
    52   private boolean socketChanged;
    5351
    5452  // protocol errors before blacklisting.
     
    5755
    5856  /**
    59    *  Multitorrent
    60    */
    61   public ConnectionAcceptor(I2PSnarkUtil util) {
     57   *  Multitorrent. Caller MUST call startAccepting()
     58   */
     59  public ConnectionAcceptor(I2PSnarkUtil util, PeerCoordinatorSet set) {
    6260      _util = util;
    6361      _cleaner = new Cleaner();
    64   }
    65  
    66   public synchronized void startAccepting(PeerCoordinatorSet set, I2PServerSocket socket) {
    67     if (serverSocket != socket) {
    68       if ( (peeracceptor == null) || (peeracceptor.coordinators != set) )
    69         peeracceptor = new PeerAcceptor(set);
    70       serverSocket = socket;
     62      peeracceptor = new PeerAcceptor(set);
     63  }
     64 
     65  /**
     66   *  May be called even when already running. May be called to start up again after halt().
     67   */
     68  public synchronized void startAccepting() {
    7169      stop = false;
    72       socketChanged = true;
     70      if (_log.shouldLog(Log.WARN))
     71          _log.warn("ConnectionAcceptor startAccepting new thread? " + (thread == null));
    7372      if (thread == null) {
    7473          thread = new I2PAppThread(this, "I2PSnark acceptor");
    7574          thread.setDaemon(true);
    7675          thread.start();
    77           _cleaner.schedule(BAD_CLEAN_INTERVAL);
    78       }
    79     }
    80   }
    81  
    82   /**
    83    *  Unused (single torrent)
    84    */
    85   public ConnectionAcceptor(I2PSnarkUtil util, I2PServerSocket serverSocket,
     76          _cleaner.reschedule(BAD_CLEAN_INTERVAL, false);
     77      }
     78  }
     79 
     80  /**
     81   *  Unused (single torrent).
     82   *  Do NOT call startAccepting().
     83   */
     84  public ConnectionAcceptor(I2PSnarkUtil util,
    8685                            PeerAcceptor peeracceptor)
    8786  {
    88     this.serverSocket = serverSocket;
    8987    this.peeracceptor = peeracceptor;
    9088    _util = util;
     
    9694  }
    9795
    98   public void halt()
     96  /**
     97   *  May be restarted later with startAccepting().
     98   */
     99  public synchronized void halt()
    99100  {
    100101    if (stop) return;
    101102    stop = true;
    102 
    103     I2PServerSocket ss = serverSocket;
    104     if (ss != null)
     103    locked_halt();
     104    Thread t = thread;
     105    if (t != null) {
     106      t.interrupt();
     107      thread = null;
     108    }
     109  }
     110
     111
     112  /**
     113   *  Caller must synch
     114   *  @since 0.9.9
     115   */
     116  private void locked_halt()
     117  {
     118    I2PServerSocket ss = _util.getServerSocket();
     119    if (ss != null) {
    105120      try
    106121        {
     
    108123        }
    109124      catch(I2PException ioe) { }
    110 
    111     Thread t = thread;
    112     if (t != null)
    113       t.interrupt();
     125    }
     126    _badCounter.clear();
    114127    _cleaner.cancel();
    115128  }
     
    118131   *  Effectively unused, would only be called if we changed
    119132   *  I2CP host/port, which is hidden in the gui if in router context
    120    */
    121   public void restart() {
    122       serverSocket = _util.getServerSocket();
    123       socketChanged = true;
     133   *  FIXME this only works if already running
     134   */
     135  public synchronized void restart() {
    124136      Thread t = thread;
    125137      if (t != null)
    126138          t.interrupt();
    127       _cleaner.schedule(BAD_CLEAN_INTERVAL);
    128139  }
    129140
     
    134145
    135146  public void run()
     147  {
     148      try {
     149          run2();
     150      } finally {
     151          synchronized(this) {
     152              thread = null;
     153          }
     154      }
     155  }
     156
     157  private void run2()
    136158  {
    137159    while(!stop)
    138160      {
    139         if (socketChanged) {
    140             // ok, already updated
    141             socketChanged = false;
    142         }
     161        I2PServerSocket serverSocket = _util.getServerSocket();
    143162        while ( (serverSocket == null) && (!stop)) {
     163            if (!(_util.isConnecting() || _util.connected())) {
     164                stop = true;
     165                break;
     166            }
     167            try { Thread.sleep(10*1000); } catch (InterruptedException ie) {}
    144168            serverSocket = _util.getServerSocket();
    145             if (serverSocket == null)
    146                 try { Thread.sleep(10*1000); } catch (InterruptedException ie) {}
    147169        }
    148170        if(stop)
     
    152174            I2PSocket socket = serverSocket.accept();
    153175            if (socket == null) {
    154                 if (socketChanged) {
    155                     continue;
    156                 } else {
    157                     I2PServerSocket ss = _util.getServerSocket();
    158                     if (ss != serverSocket) {
    159                         serverSocket = ss;
    160                         socketChanged = true;
    161                     }
    162                 }
     176                    continue;
    163177            } else {
    164178                if (socket.getPeerDestination().equals(_util.getMyDestination())) {
     
    189203        catch (I2PException ioe)
    190204          {
    191             if (!socketChanged) {
    192                 _log.error("Error while accepting", ioe);
    193                 stop = true;
     205            int level = stop ? Log.WARN : Log.ERROR;
     206            if (_log.shouldLog(level))
     207                _log.log(level, "Error while accepting", ioe);
     208            synchronized(this) {
     209                if (!stop) {
     210                    locked_halt();
     211                    thread = null;
     212                    stop = true;
     213                }
    194214            }
    195215          }
    196216        catch (IOException ioe)
    197217          {
    198             _log.error("Error while accepting", ioe);
    199             stop = true;
     218            int level = stop ? Log.WARN : Log.ERROR;
     219            if (_log.shouldLog(level))
     220                _log.log(level, "Error while accepting", ioe);
     221            synchronized(this) {
     222                if (!stop) {
     223                    locked_halt();
     224                    thread = null;
     225                    stop = true;
     226                }
     227            }
    200228          }
    201229        // catch oom?
    202230      }
    203 
    204     try
    205       {
    206         if (serverSocket != null)
    207           serverSocket.close();
    208       }
    209     catch (I2PException ignored) { }
    210    
     231      if (_log.shouldLog(Log.WARN))
     232          _log.warn("ConnectionAcceptor closed");
    211233  }
    212234 
  • apps/i2psnark/java/src/org/klomp/snark/Snark.java

    r445e430 r7fe8573  
    526526            // multitorrent
    527527            _peerCoordinatorSet.add(coordinator);
    528             if (acceptor != null) {
    529                 acceptor.startAccepting(_peerCoordinatorSet, serversocket);
    530             } else {
    531               // error
    532             }
    533528        } else {
    534529            // single torrent
    535             acceptor = new ConnectionAcceptor(_util, serversocket, new PeerAcceptor(coordinator));
     530            acceptor = new ConnectionAcceptor(_util, new PeerAcceptor(coordinator));
    536531        }
    537532        // TODO pass saved closest DHT nodes to the tracker? or direct to the coordinator?
    538533        trackerclient = new TrackerClient(_util, meta, additionalTrackerURL, coordinator, this);
     534    }
     535    // ensure acceptor is running when in multitorrent
     536    if (_peerCoordinatorSet != null && acceptor != null) {
     537        acceptor.startAccepting();
    539538    }
    540539
  • apps/i2psnark/java/src/org/klomp/snark/SnarkManager.java

    r445e430 r7fe8573  
    170170        _running = true;
    171171        _peerCoordinatorSet = new PeerCoordinatorSet();
    172         _connectionAcceptor = new ConnectionAcceptor(_util);
     172        _connectionAcceptor = new ConnectionAcceptor(_util, _peerCoordinatorSet);
    173173        _monitor = new I2PAppThread(new DirMonitor(), "Snark DirMonitor", true);
    174174        _monitor.start();
  • history.txt

    r445e430 r7fe8573  
     12013-12-04 zzz
     2i2psnark: Fix ConnectionAcceptor not restarting after tunnel
     3  restart, preventing incoming connections
     4
    152013-12-01 kytv
    26 * Update geoip.txt based on Maxmind GeoLite Country database from 2013-11-05.
  • router/java/src/net/i2p/router/RouterVersion.java

    r445e430 r7fe8573  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 37;
     21    public final static long BUILD = 38;
    2222
    2323    /** for example "-test" */
Note: See TracChangeset for help on using the changeset viewer.