diff -r ed84c8bdd87b -r 7520a9246ff5 src/org/sonews/daemon/Connections.java --- a/src/org/sonews/daemon/Connections.java Sun Aug 29 17:28:58 2010 +0200 +++ b/src/org/sonews/daemon/Connections.java Tue Oct 18 21:10:45 2011 +0200 @@ -41,141 +41,120 @@ public final class Connections extends AbstractDaemon { - private static final Connections instance = new Connections(); - - /** - * @return Active Connections instance. - */ - public static Connections getInstance() - { - return Connections.instance; - } - - private final List connections - = new ArrayList(); - private final Map connByChannel - = new HashMap(); - - private Connections() - { - setName("Connections"); - } - - /** - * Adds the given NNTPConnection to the Connections management. - * @param conn - * @see org.sonews.daemon.NNTPConnection - */ - public void add(final NNTPConnection conn) - { - synchronized(this.connections) - { - this.connections.add(conn); - this.connByChannel.put(conn.getSocketChannel(), conn); - } - } - - /** - * @param channel - * @return NNTPConnection instance that is associated with the given - * SocketChannel. - */ - public NNTPConnection get(final SocketChannel channel) - { - synchronized(this.connections) - { - return this.connByChannel.get(channel); - } - } + private static final Connections instance = new Connections(); - int getConnectionCount(String remote) - { - int cnt = 0; - synchronized(this.connections) - { - for(NNTPConnection conn : this.connections) - { - assert conn != null; - assert conn.getSocketChannel() != null; + /** + * @return Active Connections instance. + */ + public static Connections getInstance() + { + return Connections.instance; + } + private final List connections = new ArrayList(); + private final Map connByChannel = new HashMap(); - Socket socket = conn.getSocketChannel().socket(); - if(socket != null) - { - InetSocketAddress sockAddr = (InetSocketAddress)socket.getRemoteSocketAddress(); - if(sockAddr != null) - { - if(sockAddr.getHostName().equals(remote)) - { - cnt++; - } - } - } // if(socket != null) - } - } - return cnt; - } - - /** - * Run loops. Checks periodically for timed out connections and purged them - * from the lists. - */ - @Override - public void run() - { - while(isRunning()) - { - int timeoutMillis = 1000 * Config.inst().get(Config.TIMEOUT, 180); - - synchronized (this.connections) - { - final ListIterator iter = this.connections.listIterator(); - NNTPConnection conn; + private Connections() + { + setName("Connections"); + } - while (iter.hasNext()) - { - conn = iter.next(); - if((System.currentTimeMillis() - conn.getLastActivity()) > timeoutMillis - && conn.getBuffers().isOutputBufferEmpty()) - { - // A connection timeout has occurred so purge the connection - iter.remove(); + /** + * Adds the given NNTPConnection to the Connections management. + * @param conn + * @see org.sonews.daemon.NNTPConnection + */ + public void add(final NNTPConnection conn) + { + synchronized (this.connections) { + this.connections.add(conn); + this.connByChannel.put(conn.getSocketChannel(), conn); + } + } - // Close and remove the channel - SocketChannel channel = conn.getSocketChannel(); - connByChannel.remove(channel); - - try - { - assert channel != null; - assert channel.socket() != null; - - // Close the channel; implicitely cancels all selectionkeys - channel.close(); - Log.get().info("Disconnected: " + channel.socket().getRemoteSocketAddress() + - " (timeout)"); - } - catch(IOException ex) - { - Log.get().warning("Connections.run(): " + ex); - } + /** + * @param channel + * @return NNTPConnection instance that is associated with the given + * SocketChannel. + */ + public NNTPConnection get(final SocketChannel channel) + { + synchronized (this.connections) { + return this.connByChannel.get(channel); + } + } - // Recycle the used buffers - conn.getBuffers().recycleBuffers(); - - Stats.getInstance().clientDisconnect(); - } - } - } + int getConnectionCount(String remote) + { + int cnt = 0; + synchronized (this.connections) { + for (NNTPConnection conn : this.connections) { + assert conn != null; + assert conn.getSocketChannel() != null; - try - { - Thread.sleep(10000); // Sleep ten seconds - } - catch(InterruptedException ex) - { - Log.get().warning("Connections Thread was interrupted: " + ex.getMessage()); - } - } - } - + Socket socket = conn.getSocketChannel().socket(); + if (socket != null) { + InetSocketAddress sockAddr = (InetSocketAddress) socket.getRemoteSocketAddress(); + if (sockAddr != null) { + if (sockAddr.getHostName().equals(remote)) { + cnt++; + } + } + } // if(socket != null) + } + } + return cnt; + } + + /** + * Run loops. Checks periodically for timed out connections and purged them + * from the lists. + */ + @Override + public void run() + { + while (isRunning()) { + int timeoutMillis = 1000 * Config.inst().get(Config.TIMEOUT, 180); + + synchronized (this.connections) { + final ListIterator iter = this.connections.listIterator(); + NNTPConnection conn; + + while (iter.hasNext()) { + conn = iter.next(); + if ((System.currentTimeMillis() - conn.getLastActivity()) > timeoutMillis + && conn.getBuffers().isOutputBufferEmpty()) { + // A connection timeout has occurred so purge the connection + iter.remove(); + + // Close and remove the channel + SocketChannel channel = conn.getSocketChannel(); + connByChannel.remove(channel); + + try { + assert channel != null; + assert channel.socket() != null; + + // Close the channel; implicitely cancels all selectionkeys + channel.close(); + Log.get().info("Disconnected: " + channel.socket().getRemoteSocketAddress() + + " (timeout)"); + } catch (IOException ex) { + Log.get().warning("Connections.run(): " + ex); + } + + // Recycle the used buffers + conn.getBuffers().recycleBuffers(); + + Stats.getInstance().clientDisconnect(); + } + } + } + + try { + Thread.sleep(10000); // Sleep ten seconds + } catch (InterruptedException ex) { + Log.get().warning("Connections Thread was interrupted: " + ex.getMessage()); + } + } + } }