author | cli |
Wed, 04 May 2011 18:34:21 +0200 | |
changeset 41 | 11402b568081 |
parent 37 | 74139325d305 |
permissions | -rwxr-xr-x |
chris@1 | 1 |
/* |
chris@1 | 2 |
* SONEWS News Server |
chris@1 | 3 |
* see AUTHORS for the list of contributors |
chris@1 | 4 |
* |
chris@1 | 5 |
* This program is free software: you can redistribute it and/or modify |
chris@1 | 6 |
* it under the terms of the GNU General Public License as published by |
chris@1 | 7 |
* the Free Software Foundation, either version 3 of the License, or |
chris@1 | 8 |
* (at your option) any later version. |
chris@1 | 9 |
* |
chris@1 | 10 |
* This program is distributed in the hope that it will be useful, |
chris@1 | 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
chris@1 | 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
chris@1 | 13 |
* GNU General Public License for more details. |
chris@1 | 14 |
* |
chris@1 | 15 |
* You should have received a copy of the GNU General Public License |
chris@1 | 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
chris@1 | 17 |
*/ |
chris@1 | 18 |
|
chris@1 | 19 |
package org.sonews.daemon; |
chris@1 | 20 |
|
chris@1 | 21 |
import org.sonews.util.Log; |
chris@1 | 22 |
import java.nio.ByteBuffer; |
chris@1 | 23 |
import java.nio.channels.SocketChannel; |
chris@1 | 24 |
import java.util.concurrent.ArrayBlockingQueue; |
chris@1 | 25 |
|
chris@1 | 26 |
/** |
chris@1 | 27 |
* Does most of the work: parsing input, talking to client and Database. |
chris@1 | 28 |
* @author Christian Lins |
chris@1 | 29 |
* @since sonews/0.5.0 |
chris@1 | 30 |
*/ |
chris@1 | 31 |
class ConnectionWorker extends AbstractDaemon |
chris@1 | 32 |
{ |
chris@1 | 33 |
|
cli@37 | 34 |
// 256 pending events should be enough |
cli@37 | 35 |
private static ArrayBlockingQueue<SocketChannel> pendingChannels = new ArrayBlockingQueue<SocketChannel>(256, true); |
chris@1 | 36 |
|
cli@37 | 37 |
/** |
cli@37 | 38 |
* Registers the given channel for further event processing. |
cli@37 | 39 |
* @param channel |
cli@37 | 40 |
*/ |
cli@37 | 41 |
public static void addChannel(SocketChannel channel) |
cli@37 | 42 |
throws InterruptedException |
cli@37 | 43 |
{ |
cli@37 | 44 |
pendingChannels.put(channel); |
cli@37 | 45 |
} |
chris@1 | 46 |
|
cli@37 | 47 |
/** |
cli@37 | 48 |
* Processing loop. |
cli@37 | 49 |
*/ |
cli@37 | 50 |
@Override |
cli@37 | 51 |
public void run() |
cli@37 | 52 |
{ |
cli@37 | 53 |
while (isRunning()) { |
cli@37 | 54 |
try { |
cli@37 | 55 |
// Retrieve and remove if available, otherwise wait. |
cli@37 | 56 |
SocketChannel channel = pendingChannels.take(); |
cli@37 | 57 |
|
cli@37 | 58 |
if (channel != null) { |
cli@37 | 59 |
// Connections.getInstance().get() MAY return null |
cli@37 | 60 |
NNTPConnection conn = Connections.getInstance().get(channel); |
cli@37 | 61 |
|
cli@37 | 62 |
// Try to lock the connection object |
cli@37 | 63 |
if (conn != null && conn.tryReadLock()) { |
cli@37 | 64 |
ByteBuffer buf = conn.getBuffers().nextInputLine(); |
cli@37 | 65 |
while (buf != null) // Complete line was received |
cli@37 | 66 |
{ |
cli@37 | 67 |
final byte[] line = new byte[buf.limit()]; |
cli@37 | 68 |
buf.get(line); |
cli@37 | 69 |
ChannelLineBuffers.recycleBuffer(buf); |
cli@37 | 70 |
|
cli@37 | 71 |
// Here is the actual work done |
cli@37 | 72 |
conn.lineReceived(line); |
cli@37 | 73 |
|
cli@37 | 74 |
// Read next line as we could have already received the next line |
cli@37 | 75 |
buf = conn.getBuffers().nextInputLine(); |
cli@37 | 76 |
} |
cli@37 | 77 |
conn.unlockReadLock(); |
cli@37 | 78 |
} else { |
cli@37 | 79 |
addChannel(channel); |
cli@37 | 80 |
} |
cli@37 | 81 |
} |
cli@37 | 82 |
} catch (InterruptedException ex) { |
cli@37 | 83 |
Log.get().info("ConnectionWorker interrupted: " + ex); |
cli@37 | 84 |
} catch (Exception ex) { |
cli@37 | 85 |
Log.get().severe("Exception in ConnectionWorker: " + ex); |
cli@37 | 86 |
ex.printStackTrace(); |
cli@37 | 87 |
} |
cli@37 | 88 |
} // end while(isRunning()) |
cli@37 | 89 |
} |
chris@1 | 90 |
} |