1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/daemon/ChannelReader.java Sun Aug 29 17:43:58 2010 +0200
1.3 @@ -0,0 +1,202 @@
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 java.io.IOException;
1.25 +import java.nio.ByteBuffer;
1.26 +import java.nio.channels.CancelledKeyException;
1.27 +import java.nio.channels.SelectionKey;
1.28 +import java.nio.channels.Selector;
1.29 +import java.nio.channels.SocketChannel;
1.30 +import java.util.Iterator;
1.31 +import java.util.Set;
1.32 +import java.util.logging.Level;
1.33 +import org.sonews.util.Log;
1.34 +
1.35 +/**
1.36 + * A Thread task listening for OP_READ events from SocketChannels.
1.37 + * @author Christian Lins
1.38 + * @since sonews/0.5.0
1.39 + */
1.40 +class ChannelReader extends AbstractDaemon
1.41 +{
1.42 +
1.43 + private static ChannelReader instance = new ChannelReader();
1.44 +
1.45 + /**
1.46 + * @return Active ChannelReader instance.
1.47 + */
1.48 + public static ChannelReader getInstance()
1.49 + {
1.50 + return instance;
1.51 + }
1.52 +
1.53 + private Selector selector = null;
1.54 +
1.55 + protected ChannelReader()
1.56 + {
1.57 + }
1.58 +
1.59 + /**
1.60 + * Sets the selector which is used by this reader to determine the channel
1.61 + * to read from.
1.62 + * @param selector
1.63 + */
1.64 + public void setSelector(final Selector selector)
1.65 + {
1.66 + this.selector = selector;
1.67 + }
1.68 +
1.69 + /**
1.70 + * Run loop. Blocks until some data is available in a channel.
1.71 + */
1.72 + @Override
1.73 + public void run()
1.74 + {
1.75 + assert selector != null;
1.76 +
1.77 + while(isRunning())
1.78 + {
1.79 + try
1.80 + {
1.81 + // select() blocks until some SelectableChannels are ready for
1.82 + // processing. There is no need to lock the selector as we have only
1.83 + // one thread per selector.
1.84 + selector.select();
1.85 +
1.86 + // Get list of selection keys with pending events.
1.87 + // Note: the selected key set is not thread-safe
1.88 + SocketChannel channel = null;
1.89 + NNTPConnection conn = null;
1.90 + final Set<SelectionKey> selKeys = selector.selectedKeys();
1.91 + SelectionKey selKey = null;
1.92 +
1.93 + synchronized (selKeys)
1.94 + {
1.95 + Iterator it = selKeys.iterator();
1.96 +
1.97 + // Process the first pending event
1.98 + while (it.hasNext())
1.99 + {
1.100 + selKey = (SelectionKey) it.next();
1.101 + channel = (SocketChannel) selKey.channel();
1.102 + conn = Connections.getInstance().get(channel);
1.103 +
1.104 + // Because we cannot lock the selKey as that would cause a deadlock
1.105 + // we lock the connection. To preserve the order of the received
1.106 + // byte blocks a selection key for a connection that has pending
1.107 + // read events is skipped.
1.108 + if (conn == null || conn.tryReadLock())
1.109 + {
1.110 + // Remove from set to indicate that it's being processed
1.111 + it.remove();
1.112 + if (conn != null)
1.113 + {
1.114 + break; // End while loop
1.115 + }
1.116 + }
1.117 + else
1.118 + {
1.119 + selKey = null;
1.120 + channel = null;
1.121 + conn = null;
1.122 + }
1.123 + }
1.124 + }
1.125 +
1.126 + // Do not lock the selKeys while processing because this causes
1.127 + // a deadlock in sun.nio.ch.SelectorImpl.lockAndDoSelect()
1.128 + if (selKey != null && channel != null && conn != null)
1.129 + {
1.130 + processSelectionKey(conn, channel, selKey);
1.131 + conn.unlockReadLock();
1.132 + }
1.133 +
1.134 + }
1.135 + catch(CancelledKeyException ex)
1.136 + {
1.137 + Log.get().warning("ChannelReader.run(): " + ex);
1.138 + Log.get().log(Level.INFO, "", ex);
1.139 + }
1.140 + catch(Exception ex)
1.141 + {
1.142 + ex.printStackTrace();
1.143 + }
1.144 +
1.145 + // Eventually wait for a register operation
1.146 + synchronized (NNTPDaemon.RegisterGate)
1.147 + {
1.148 + // Do nothing; FindBugs may warn about an empty synchronized
1.149 + // statement, but we cannot use a wait()/notify() mechanism here.
1.150 + // If we used something like RegisterGate.wait() we block here
1.151 + // until the NNTPDaemon calls notify(). But the daemon only
1.152 + // calls notify() if itself is NOT blocked in the listening socket.
1.153 + }
1.154 + } // while(isRunning())
1.155 + }
1.156 +
1.157 + private void processSelectionKey(final NNTPConnection connection,
1.158 + final SocketChannel socketChannel, final SelectionKey selKey)
1.159 + throws InterruptedException, IOException
1.160 + {
1.161 + assert selKey != null;
1.162 + assert selKey.isReadable();
1.163 +
1.164 + // Some bytes are available for reading
1.165 + if(selKey.isValid())
1.166 + {
1.167 + // Lock the channel
1.168 + //synchronized(socketChannel)
1.169 + {
1.170 + // Read the data into the appropriate buffer
1.171 + ByteBuffer buf = connection.getInputBuffer();
1.172 + int read = -1;
1.173 + try
1.174 + {
1.175 + read = socketChannel.read(buf);
1.176 + }
1.177 + catch(IOException ex)
1.178 + {
1.179 + // The connection was probably closed by the remote host
1.180 + // in a non-clean fashion
1.181 + Log.get().info("ChannelReader.processSelectionKey(): " + ex);
1.182 + }
1.183 + catch(Exception ex)
1.184 + {
1.185 + Log.get().warning("ChannelReader.processSelectionKey(): " + ex);
1.186 + }
1.187 +
1.188 + if(read == -1) // End of stream
1.189 + {
1.190 + selKey.cancel();
1.191 + }
1.192 + else if(read > 0) // If some data was read
1.193 + {
1.194 + ConnectionWorker.addChannel(socketChannel);
1.195 + }
1.196 + }
1.197 + }
1.198 + else
1.199 + {
1.200 + // Should not happen
1.201 + Log.get().severe("Should not happen: " + selKey.toString());
1.202 + }
1.203 + }
1.204 +
1.205 +}