Add some checks to prevent #13 happen.
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.util.Log;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.SocketChannel;
24 import java.util.concurrent.ArrayBlockingQueue;
27 * Does most of the work: parsing input, talking to client and Database.
28 * @author Christian Lins
31 class ConnectionWorker extends AbstractDaemon
34 // 256 pending events should be enough
35 private static ArrayBlockingQueue<SocketChannel> pendingChannels
36 = new ArrayBlockingQueue<SocketChannel>(256, true);
39 * Registers the given channel for further event processing.
42 public static void addChannel(SocketChannel channel)
43 throws InterruptedException
45 pendingChannels.put(channel);
58 // Retrieve and remove if available, otherwise wait.
59 SocketChannel channel = pendingChannels.take();
63 // Connections.getInstance().get() MAY return null
64 NNTPConnection conn = Connections.getInstance().get(channel);
66 // Try to lock the connection object
67 if(conn != null && conn.tryReadLock())
69 ByteBuffer buf = conn.getBuffers().nextInputLine();
70 while(buf != null) // Complete line was received
72 final byte[] line = new byte[buf.limit()];
74 ChannelLineBuffers.recycleBuffer(buf);
76 // Here is the actual work done
77 conn.lineReceived(line);
79 // Read next line as we could have already received the next line
80 buf = conn.getBuffers().nextInputLine();
82 conn.unlockReadLock();
90 catch(InterruptedException ex)
92 Log.get().info("ConnectionWorker interrupted: " + ex);
96 Log.get().severe("Exception in ConnectionWorker: " + ex);
99 } // end while(isRunning())