#544 fixed.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.daemon;
21 import org.sonews.config.Config;
22 import org.sonews.util.Log;
23 import java.io.IOException;
24 import java.net.InetSocketAddress;
25 import java.net.Socket;
26 import java.nio.channels.SocketChannel;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.ListIterator;
32 import org.sonews.util.Stats;
35 * Daemon thread collecting all NNTPConnection instances. The thread
36 * checks periodically if there are stale/timed out connections and
37 * removes and purges them properly.
38 * @author Christian Lins
41 public final class Connections extends AbstractDaemon
44 private static final Connections instance = new Connections();
47 * @return Active Connections instance.
49 public static Connections getInstance()
51 return Connections.instance;
54 private final List<NNTPConnection> connections
55 = new ArrayList<NNTPConnection>();
56 private final Map<SocketChannel, NNTPConnection> connByChannel
57 = new HashMap<SocketChannel, NNTPConnection>();
61 setName("Connections");
65 * Adds the given NNTPConnection to the Connections management.
67 * @see org.sonews.daemon.NNTPConnection
69 public void add(final NNTPConnection conn)
71 synchronized(this.connections)
73 this.connections.add(conn);
74 this.connByChannel.put(conn.getSocketChannel(), conn);
80 * @return NNTPConnection instance that is associated with the given
83 public NNTPConnection get(final SocketChannel channel)
85 synchronized(this.connections)
87 return this.connByChannel.get(channel);
91 int getConnectionCount(String remote)
94 synchronized(this.connections)
96 for(NNTPConnection conn : this.connections)
99 assert conn.getSocketChannel() != null;
101 Socket socket = conn.getSocketChannel().socket();
104 InetSocketAddress sockAddr = (InetSocketAddress)socket.getRemoteSocketAddress();
107 if(sockAddr.getHostName().equals(remote))
112 } // if(socket != null)
119 * Run loops. Checks periodically for timed out connections and purged them
127 int timeoutMillis = 1000 * Config.inst().get(Config.TIMEOUT, 180);
129 synchronized (this.connections)
131 final ListIterator<NNTPConnection> iter = this.connections.listIterator();
134 while (iter.hasNext())
137 if((System.currentTimeMillis() - conn.getLastActivity()) > timeoutMillis)
139 // A connection timeout has occurred so purge the connection
142 // Close and remove the channel
143 SocketChannel channel = conn.getSocketChannel();
144 connByChannel.remove(channel);
148 // Close the channel; implicitely cancels all selectionkeys
150 Log.get().info("Disconnected: " + channel.socket().getRemoteSocketAddress() +
153 catch(IOException ex)
155 Log.get().warning("Connections.run(): " + ex);
158 // Recycle the used buffers
159 conn.getBuffers().recycleBuffers();
161 Stats.getInstance().clientDisconnect();
168 Thread.sleep(10000); // Sleep ten seconds
170 catch(InterruptedException ex)
172 Log.get().warning("Connections Thread was interrupted: " + ex.getMessage());