1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/daemon/ConnectionWorker.java Thu Aug 20 16:57:38 2009 +0200
1.3 @@ -0,0 +1,102 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.daemon;
1.23 +
1.24 +import org.sonews.util.Log;
1.25 +import java.nio.ByteBuffer;
1.26 +import java.nio.channels.SocketChannel;
1.27 +import java.util.concurrent.ArrayBlockingQueue;
1.28 +
1.29 +/**
1.30 + * Does most of the work: parsing input, talking to client and Database.
1.31 + * @author Christian Lins
1.32 + * @since sonews/0.5.0
1.33 + */
1.34 +class ConnectionWorker extends AbstractDaemon
1.35 +{
1.36 +
1.37 + // 256 pending events should be enough
1.38 + private static ArrayBlockingQueue<SocketChannel> pendingChannels
1.39 + = new ArrayBlockingQueue<SocketChannel>(256, true);
1.40 +
1.41 + /**
1.42 + * Registers the given channel for further event processing.
1.43 + * @param channel
1.44 + */
1.45 + public static void addChannel(SocketChannel channel)
1.46 + throws InterruptedException
1.47 + {
1.48 + pendingChannels.put(channel);
1.49 + }
1.50 +
1.51 + /**
1.52 + * Processing loop.
1.53 + */
1.54 + @Override
1.55 + public void run()
1.56 + {
1.57 + while(isRunning())
1.58 + {
1.59 + try
1.60 + {
1.61 + // Retrieve and remove if available, otherwise wait.
1.62 + SocketChannel channel = pendingChannels.take();
1.63 +
1.64 + if(channel != null)
1.65 + {
1.66 + // Connections.getInstance().get() MAY return null
1.67 + NNTPConnection conn = Connections.getInstance().get(channel);
1.68 +
1.69 + // Try to lock the connection object
1.70 + if(conn != null && conn.tryReadLock())
1.71 + {
1.72 + ByteBuffer buf = conn.getBuffers().nextInputLine();
1.73 + while(buf != null) // Complete line was received
1.74 + {
1.75 + final byte[] line = new byte[buf.limit()];
1.76 + buf.get(line);
1.77 + ChannelLineBuffers.recycleBuffer(buf);
1.78 +
1.79 + // Here is the actual work done
1.80 + conn.lineReceived(line);
1.81 +
1.82 + // Read next line as we could have already received the next line
1.83 + buf = conn.getBuffers().nextInputLine();
1.84 + }
1.85 + conn.unlockReadLock();
1.86 + }
1.87 + else
1.88 + {
1.89 + addChannel(channel);
1.90 + }
1.91 + }
1.92 + }
1.93 + catch(InterruptedException ex)
1.94 + {
1.95 + Log.msg("ConnectionWorker interrupted: " + ex, true);
1.96 + }
1.97 + catch(Exception ex)
1.98 + {
1.99 + Log.msg("Exception in ConnectionWorker: " + ex, false);
1.100 + ex.printStackTrace();
1.101 + }
1.102 + } // end while(isRunning())
1.103 + }
1.104 +
1.105 +}