org/sonews/daemon/ConnectionWorker.java
author cli
Mon Aug 24 13:00:05 2009 +0200 (2009-08-24)
changeset 20 6ae5e4f8329b
parent 1 6fceb66e1ad7
permissions -rw-r--r--
Add stubs for org.sonews.acl and add method "impliedCapability" for org.sonews.command.Command interface.
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
chris@1
    34
  // 256 pending events should be enough
chris@1
    35
  private static ArrayBlockingQueue<SocketChannel> pendingChannels
chris@1
    36
    = new ArrayBlockingQueue<SocketChannel>(256, true);
chris@1
    37
  
chris@1
    38
  /**
chris@1
    39
   * Registers the given channel for further event processing.
chris@1
    40
   * @param channel
chris@1
    41
   */
chris@1
    42
  public static void addChannel(SocketChannel channel)
chris@1
    43
    throws InterruptedException
chris@1
    44
  {
chris@1
    45
    pendingChannels.put(channel);
chris@1
    46
  }
chris@1
    47
  
chris@1
    48
  /**
chris@1
    49
   * Processing loop.
chris@1
    50
   */
chris@1
    51
  @Override
chris@1
    52
  public void run()
chris@1
    53
  {
chris@1
    54
    while(isRunning())
chris@1
    55
    {
chris@1
    56
      try
chris@1
    57
      {
chris@1
    58
        // Retrieve and remove if available, otherwise wait.
chris@1
    59
        SocketChannel channel = pendingChannels.take();
chris@1
    60
chris@1
    61
        if(channel != null)
chris@1
    62
        {
chris@1
    63
          // Connections.getInstance().get() MAY return null
chris@1
    64
          NNTPConnection conn = Connections.getInstance().get(channel);
chris@1
    65
          
chris@1
    66
          // Try to lock the connection object
chris@1
    67
          if(conn != null && conn.tryReadLock())
chris@1
    68
          {
chris@1
    69
            ByteBuffer buf = conn.getBuffers().nextInputLine();
chris@1
    70
            while(buf != null) // Complete line was received
chris@1
    71
            {
chris@1
    72
              final byte[] line = new byte[buf.limit()];
chris@1
    73
              buf.get(line);
chris@1
    74
              ChannelLineBuffers.recycleBuffer(buf);
chris@1
    75
              
chris@1
    76
              // Here is the actual work done
chris@1
    77
              conn.lineReceived(line);
chris@1
    78
chris@1
    79
              // Read next line as we could have already received the next line
chris@1
    80
              buf = conn.getBuffers().nextInputLine();
chris@1
    81
            }
chris@1
    82
            conn.unlockReadLock();
chris@1
    83
          }
chris@1
    84
          else
chris@1
    85
          {
chris@1
    86
            addChannel(channel);
chris@1
    87
          }
chris@1
    88
        }
chris@1
    89
      }
chris@1
    90
      catch(InterruptedException ex)
chris@1
    91
      {
cli@15
    92
        Log.get().info("ConnectionWorker interrupted: " + ex);
chris@1
    93
      }
chris@1
    94
      catch(Exception ex)
chris@1
    95
      {
cli@15
    96
        Log.get().severe("Exception in ConnectionWorker: " + ex);
chris@1
    97
        ex.printStackTrace();
chris@1
    98
      }
chris@1
    99
    } // end while(isRunning())
chris@1
   100
  }
chris@1
   101
  
chris@1
   102
}