chris@1: /*
chris@1: * SONEWS News Server
chris@1: * see AUTHORS for the list of contributors
chris@1: *
chris@1: * This program is free software: you can redistribute it and/or modify
chris@1: * it under the terms of the GNU General Public License as published by
chris@1: * the Free Software Foundation, either version 3 of the License, or
chris@1: * (at your option) any later version.
chris@1: *
chris@1: * This program is distributed in the hope that it will be useful,
chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@1: * GNU General Public License for more details.
chris@1: *
chris@1: * You should have received a copy of the GNU General Public License
chris@1: * along with this program. If not, see .
chris@1: */
chris@1:
chris@1: package org.sonews.daemon;
chris@1:
chris@1: import org.sonews.util.Log;
chris@1: import java.nio.ByteBuffer;
chris@1: import java.nio.channels.SocketChannel;
chris@1: import java.util.concurrent.ArrayBlockingQueue;
chris@1:
chris@1: /**
chris@1: * Does most of the work: parsing input, talking to client and Database.
chris@1: * @author Christian Lins
chris@1: * @since sonews/0.5.0
chris@1: */
chris@1: class ConnectionWorker extends AbstractDaemon
chris@1: {
chris@1:
cli@37: // 256 pending events should be enough
cli@37: private static ArrayBlockingQueue pendingChannels = new ArrayBlockingQueue(256, true);
chris@1:
cli@37: /**
cli@37: * Registers the given channel for further event processing.
cli@37: * @param channel
cli@37: */
cli@37: public static void addChannel(SocketChannel channel)
cli@37: throws InterruptedException
cli@37: {
cli@37: pendingChannels.put(channel);
cli@37: }
chris@1:
cli@37: /**
cli@37: * Processing loop.
cli@37: */
cli@37: @Override
cli@37: public void run()
cli@37: {
cli@37: while (isRunning()) {
cli@37: try {
cli@37: // Retrieve and remove if available, otherwise wait.
cli@37: SocketChannel channel = pendingChannels.take();
cli@37:
cli@37: if (channel != null) {
cli@37: // Connections.getInstance().get() MAY return null
cli@37: NNTPConnection conn = Connections.getInstance().get(channel);
cli@37:
cli@37: // Try to lock the connection object
cli@37: if (conn != null && conn.tryReadLock()) {
cli@37: ByteBuffer buf = conn.getBuffers().nextInputLine();
cli@37: while (buf != null) // Complete line was received
cli@37: {
cli@37: final byte[] line = new byte[buf.limit()];
cli@37: buf.get(line);
cli@37: ChannelLineBuffers.recycleBuffer(buf);
cli@37:
cli@37: // Here is the actual work done
cli@37: conn.lineReceived(line);
cli@37:
cli@37: // Read next line as we could have already received the next line
cli@37: buf = conn.getBuffers().nextInputLine();
cli@37: }
cli@37: conn.unlockReadLock();
cli@37: } else {
cli@37: addChannel(channel);
cli@37: }
cli@37: }
cli@37: } catch (InterruptedException ex) {
cli@37: Log.get().info("ConnectionWorker interrupted: " + ex);
cli@37: } catch (Exception ex) {
cli@37: Log.get().severe("Exception in ConnectionWorker: " + ex);
cli@37: ex.printStackTrace();
cli@37: }
cli@37: } // end while(isRunning())
cli@37: }
chris@1: }