diff -r ed84c8bdd87b -r 74139325d305 src/org/sonews/daemon/ConnectionWorker.java --- a/src/org/sonews/daemon/ConnectionWorker.java Sun Aug 29 17:28:58 2010 +0200 +++ b/src/org/sonews/daemon/ConnectionWorker.java Sun Aug 29 18:17:37 2010 +0200 @@ -31,72 +31,60 @@ class ConnectionWorker extends AbstractDaemon { - // 256 pending events should be enough - private static ArrayBlockingQueue pendingChannels - = new ArrayBlockingQueue(256, true); - - /** - * Registers the given channel for further event processing. - * @param channel - */ - public static void addChannel(SocketChannel channel) - throws InterruptedException - { - pendingChannels.put(channel); - } - - /** - * Processing loop. - */ - @Override - public void run() - { - while(isRunning()) - { - try - { - // Retrieve and remove if available, otherwise wait. - SocketChannel channel = pendingChannels.take(); + // 256 pending events should be enough + private static ArrayBlockingQueue pendingChannels = new ArrayBlockingQueue(256, true); - if(channel != null) - { - // Connections.getInstance().get() MAY return null - NNTPConnection conn = Connections.getInstance().get(channel); - - // Try to lock the connection object - if(conn != null && conn.tryReadLock()) - { - ByteBuffer buf = conn.getBuffers().nextInputLine(); - while(buf != null) // Complete line was received - { - final byte[] line = new byte[buf.limit()]; - buf.get(line); - ChannelLineBuffers.recycleBuffer(buf); - - // Here is the actual work done - conn.lineReceived(line); + /** + * Registers the given channel for further event processing. + * @param channel + */ + public static void addChannel(SocketChannel channel) + throws InterruptedException + { + pendingChannels.put(channel); + } - // Read next line as we could have already received the next line - buf = conn.getBuffers().nextInputLine(); - } - conn.unlockReadLock(); - } - else - { - addChannel(channel); - } - } - } - catch(InterruptedException ex) - { - Log.get().info("ConnectionWorker interrupted: " + ex); - } - catch(Exception ex) - { - Log.get().severe("Exception in ConnectionWorker: " + ex); - ex.printStackTrace(); - } - } // end while(isRunning()) - } - + /** + * Processing loop. + */ + @Override + public void run() + { + while (isRunning()) { + try { + // Retrieve and remove if available, otherwise wait. + SocketChannel channel = pendingChannels.take(); + + if (channel != null) { + // Connections.getInstance().get() MAY return null + NNTPConnection conn = Connections.getInstance().get(channel); + + // Try to lock the connection object + if (conn != null && conn.tryReadLock()) { + ByteBuffer buf = conn.getBuffers().nextInputLine(); + while (buf != null) // Complete line was received + { + final byte[] line = new byte[buf.limit()]; + buf.get(line); + ChannelLineBuffers.recycleBuffer(buf); + + // Here is the actual work done + conn.lineReceived(line); + + // Read next line as we could have already received the next line + buf = conn.getBuffers().nextInputLine(); + } + conn.unlockReadLock(); + } else { + addChannel(channel); + } + } + } catch (InterruptedException ex) { + Log.get().info("ConnectionWorker interrupted: " + ex); + } catch (Exception ex) { + Log.get().severe("Exception in ConnectionWorker: " + ex); + ex.printStackTrace(); + } + } // end while(isRunning()) + } }