1.1 --- a/src/org/sonews/daemon/Connections.java Sun Aug 29 17:43:58 2010 +0200
1.2 +++ b/src/org/sonews/daemon/Connections.java Sun Aug 29 18:17:37 2010 +0200
1.3 @@ -41,141 +41,120 @@
1.4 public final class Connections extends AbstractDaemon
1.5 {
1.6
1.7 - private static final Connections instance = new Connections();
1.8 -
1.9 - /**
1.10 - * @return Active Connections instance.
1.11 - */
1.12 - public static Connections getInstance()
1.13 - {
1.14 - return Connections.instance;
1.15 - }
1.16 -
1.17 - private final List<NNTPConnection> connections
1.18 - = new ArrayList<NNTPConnection>();
1.19 - private final Map<SocketChannel, NNTPConnection> connByChannel
1.20 - = new HashMap<SocketChannel, NNTPConnection>();
1.21 -
1.22 - private Connections()
1.23 - {
1.24 - setName("Connections");
1.25 - }
1.26 -
1.27 - /**
1.28 - * Adds the given NNTPConnection to the Connections management.
1.29 - * @param conn
1.30 - * @see org.sonews.daemon.NNTPConnection
1.31 - */
1.32 - public void add(final NNTPConnection conn)
1.33 - {
1.34 - synchronized(this.connections)
1.35 - {
1.36 - this.connections.add(conn);
1.37 - this.connByChannel.put(conn.getSocketChannel(), conn);
1.38 - }
1.39 - }
1.40 -
1.41 - /**
1.42 - * @param channel
1.43 - * @return NNTPConnection instance that is associated with the given
1.44 - * SocketChannel.
1.45 - */
1.46 - public NNTPConnection get(final SocketChannel channel)
1.47 - {
1.48 - synchronized(this.connections)
1.49 - {
1.50 - return this.connByChannel.get(channel);
1.51 - }
1.52 - }
1.53 + private static final Connections instance = new Connections();
1.54
1.55 - int getConnectionCount(String remote)
1.56 - {
1.57 - int cnt = 0;
1.58 - synchronized(this.connections)
1.59 - {
1.60 - for(NNTPConnection conn : this.connections)
1.61 - {
1.62 - assert conn != null;
1.63 - assert conn.getSocketChannel() != null;
1.64 + /**
1.65 + * @return Active Connections instance.
1.66 + */
1.67 + public static Connections getInstance()
1.68 + {
1.69 + return Connections.instance;
1.70 + }
1.71 + private final List<NNTPConnection> connections = new ArrayList<NNTPConnection>();
1.72 + private final Map<SocketChannel, NNTPConnection> connByChannel = new HashMap<SocketChannel, NNTPConnection>();
1.73
1.74 - Socket socket = conn.getSocketChannel().socket();
1.75 - if(socket != null)
1.76 - {
1.77 - InetSocketAddress sockAddr = (InetSocketAddress)socket.getRemoteSocketAddress();
1.78 - if(sockAddr != null)
1.79 - {
1.80 - if(sockAddr.getHostName().equals(remote))
1.81 - {
1.82 - cnt++;
1.83 - }
1.84 - }
1.85 - } // if(socket != null)
1.86 - }
1.87 - }
1.88 - return cnt;
1.89 - }
1.90 -
1.91 - /**
1.92 - * Run loops. Checks periodically for timed out connections and purged them
1.93 - * from the lists.
1.94 - */
1.95 - @Override
1.96 - public void run()
1.97 - {
1.98 - while(isRunning())
1.99 - {
1.100 - int timeoutMillis = 1000 * Config.inst().get(Config.TIMEOUT, 180);
1.101 -
1.102 - synchronized (this.connections)
1.103 - {
1.104 - final ListIterator<NNTPConnection> iter = this.connections.listIterator();
1.105 - NNTPConnection conn;
1.106 + private Connections()
1.107 + {
1.108 + setName("Connections");
1.109 + }
1.110
1.111 - while (iter.hasNext())
1.112 - {
1.113 - conn = iter.next();
1.114 - if((System.currentTimeMillis() - conn.getLastActivity()) > timeoutMillis
1.115 - && conn.getBuffers().isOutputBufferEmpty())
1.116 - {
1.117 - // A connection timeout has occurred so purge the connection
1.118 - iter.remove();
1.119 + /**
1.120 + * Adds the given NNTPConnection to the Connections management.
1.121 + * @param conn
1.122 + * @see org.sonews.daemon.NNTPConnection
1.123 + */
1.124 + public void add(final NNTPConnection conn)
1.125 + {
1.126 + synchronized (this.connections) {
1.127 + this.connections.add(conn);
1.128 + this.connByChannel.put(conn.getSocketChannel(), conn);
1.129 + }
1.130 + }
1.131
1.132 - // Close and remove the channel
1.133 - SocketChannel channel = conn.getSocketChannel();
1.134 - connByChannel.remove(channel);
1.135 -
1.136 - try
1.137 - {
1.138 - assert channel != null;
1.139 - assert channel.socket() != null;
1.140 -
1.141 - // Close the channel; implicitely cancels all selectionkeys
1.142 - channel.close();
1.143 - Log.get().info("Disconnected: " + channel.socket().getRemoteSocketAddress() +
1.144 - " (timeout)");
1.145 - }
1.146 - catch(IOException ex)
1.147 - {
1.148 - Log.get().warning("Connections.run(): " + ex);
1.149 - }
1.150 + /**
1.151 + * @param channel
1.152 + * @return NNTPConnection instance that is associated with the given
1.153 + * SocketChannel.
1.154 + */
1.155 + public NNTPConnection get(final SocketChannel channel)
1.156 + {
1.157 + synchronized (this.connections) {
1.158 + return this.connByChannel.get(channel);
1.159 + }
1.160 + }
1.161
1.162 - // Recycle the used buffers
1.163 - conn.getBuffers().recycleBuffers();
1.164 -
1.165 - Stats.getInstance().clientDisconnect();
1.166 - }
1.167 - }
1.168 - }
1.169 + int getConnectionCount(String remote)
1.170 + {
1.171 + int cnt = 0;
1.172 + synchronized (this.connections) {
1.173 + for (NNTPConnection conn : this.connections) {
1.174 + assert conn != null;
1.175 + assert conn.getSocketChannel() != null;
1.176
1.177 - try
1.178 - {
1.179 - Thread.sleep(10000); // Sleep ten seconds
1.180 - }
1.181 - catch(InterruptedException ex)
1.182 - {
1.183 - Log.get().warning("Connections Thread was interrupted: " + ex.getMessage());
1.184 - }
1.185 - }
1.186 - }
1.187 -
1.188 + Socket socket = conn.getSocketChannel().socket();
1.189 + if (socket != null) {
1.190 + InetSocketAddress sockAddr = (InetSocketAddress) socket.getRemoteSocketAddress();
1.191 + if (sockAddr != null) {
1.192 + if (sockAddr.getHostName().equals(remote)) {
1.193 + cnt++;
1.194 + }
1.195 + }
1.196 + } // if(socket != null)
1.197 + }
1.198 + }
1.199 + return cnt;
1.200 + }
1.201 +
1.202 + /**
1.203 + * Run loops. Checks periodically for timed out connections and purged them
1.204 + * from the lists.
1.205 + */
1.206 + @Override
1.207 + public void run()
1.208 + {
1.209 + while (isRunning()) {
1.210 + int timeoutMillis = 1000 * Config.inst().get(Config.TIMEOUT, 180);
1.211 +
1.212 + synchronized (this.connections) {
1.213 + final ListIterator<NNTPConnection> iter = this.connections.listIterator();
1.214 + NNTPConnection conn;
1.215 +
1.216 + while (iter.hasNext()) {
1.217 + conn = iter.next();
1.218 + if ((System.currentTimeMillis() - conn.getLastActivity()) > timeoutMillis
1.219 + && conn.getBuffers().isOutputBufferEmpty()) {
1.220 + // A connection timeout has occurred so purge the connection
1.221 + iter.remove();
1.222 +
1.223 + // Close and remove the channel
1.224 + SocketChannel channel = conn.getSocketChannel();
1.225 + connByChannel.remove(channel);
1.226 +
1.227 + try {
1.228 + assert channel != null;
1.229 + assert channel.socket() != null;
1.230 +
1.231 + // Close the channel; implicitely cancels all selectionkeys
1.232 + channel.close();
1.233 + Log.get().info("Disconnected: " + channel.socket().getRemoteSocketAddress()
1.234 + + " (timeout)");
1.235 + } catch (IOException ex) {
1.236 + Log.get().warning("Connections.run(): " + ex);
1.237 + }
1.238 +
1.239 + // Recycle the used buffers
1.240 + conn.getBuffers().recycleBuffers();
1.241 +
1.242 + Stats.getInstance().clientDisconnect();
1.243 + }
1.244 + }
1.245 + }
1.246 +
1.247 + try {
1.248 + Thread.sleep(10000); // Sleep ten seconds
1.249 + } catch (InterruptedException ex) {
1.250 + Log.get().warning("Connections Thread was interrupted: " + ex.getMessage());
1.251 + }
1.252 + }
1.253 + }
1.254 }