Changeset 259:25cafbcbb963

Show
Ignore:
Timestamp:
04/30/2010 07:52:30 AM (4 months ago)
Author:
dcaoyuan
Branch:
default
Message:

Name refactoring

Location:
blogbird/src/main/scala/org/aiotrade/httpd
Files:
3 modified
1 moved

Legend:

Unmodified
Added
Removed
  • blogbird/src/main/scala/org/aiotrade/httpd/ClientConnection.scala

    r255 r259  
    99 
    1010 
    11 case class Read (callback: Boolean => Unit) 
    12 case class Write(callback: Boolean => Unit) 
    13  
    1411/** 
    1512 * Represents an open HTTP connection coming from a client. 
    1613 */ 
    17 class ClientConnection(val socketChannel: SocketChannel) extends Actor with WithSocketChannel { 
     14class ClientConnection(val channel: SocketChannel) extends Actor with WithSelectableChannel { 
    1815  private var _isOpen = true 
    1916  private var requestParser = new HttpRequestParser 
     
    2522   * Is this connection currentnly open? 
    2623   */ 
    27   def isOpen = _isOpen && socketChannel.isOpen && socketChannel.isConnected 
     24  def isOpen = _isOpen && channel.isOpen && channel.isConnected 
    2825 
    2926  /** 
     
    3229  def close { 
    3330    _isOpen = false 
    34     socketChannel.close 
     31    channel.close 
    3532    super.exit 
    3633  } 
     
    8784    var numRead = -1 
    8885    try { 
    89       numRead = socketChannel.read(readBuf) 
     86      numRead = channel.read(readBuf) 
    9087    } catch { 
    9188      case ioe: IOException => 
     
    143140      if (writeBuffers.head.remaining > 0) { 
    144141        try { 
    145           socketChannel.write(writeBuffers.head) 
     142          channel.write(writeBuffers.head) 
    146143        } catch { 
    147144          case ioe: IOException => 
  • blogbird/src/main/scala/org/aiotrade/httpd/HttpdActors.scala

    r258 r259  
    1818  debugln ("PortListener initializing...") 
    1919 
    20   connectionSelector.start 
     20  acceptActor.start 
    2121 
    2222  def stop { 
    23     connectionSelector.stop 
     23    acceptActor.stop 
    2424  } 
    2525 
    26   private object connectionSelector extends Actor { 
     26  private object acceptActor extends Actor { 
    2727    val serverChannel = ServerSocketChannel.open 
    2828    serverChannel.socket.bind(new InetSocketAddress(localAddress, port)) 
     
    5959  debugln("Connection Manager initializing...") 
    6060   
    61   connectionSelector.start 
     61  selectActor.start 
    6262 
    6363  def stop { 
    64     connectionSelector.stop 
     64    selectActor.stop 
    6565  } 
    6666 
    67   private object connectionSelector extends ConnectionSelector[ClientConnection] { 
    68     val intersOpts = SelectionKey.OP_READ 
     67  private object selectActor extends SelectActor[ClientConnection](SelectionKey.OP_READ) { 
    6968 
    7069    def fireEvent(key: SelectionKey, conn: ClientConnection) { 
     
    8685 
    8786    val conn = new ClientConnection(socketChannel) 
    88     connectionSelector.addListener(conn) 
     87    selectActor.addListener(conn) 
    8988  } 
    9089} 
     
    122121  debugln("Reply Sender initializing...") 
    123122 
    124   connectionSelector.start 
     123  selectActor.start 
    125124 
    126125  def stop { 
    127     connectionSelector.stop 
     126    selectActor.stop 
    128127  } 
    129128 
    130   private object connectionSelector extends ConnectionSelector[ClientConnection] { 
    131     val intersOpts = SelectionKey.OP_WRITE 
     129  private object selectActor extends SelectActor[ClientConnection](SelectionKey.OP_WRITE) { 
    132130 
    133131    def fireEvent(key: SelectionKey, conn: ClientConnection) { 
     
    151149    conn.send(reply) 
    152150    debugln(" * Sending a reply.") 
    153     connectionSelector.addListener(conn) 
     151    selectActor.addListener(conn) 
    154152  } 
    155153} 
  • blogbird/src/main/scala/org/aiotrade/httpd/SelectActor.scala

    r253 r259  
    1010import scala.collection.JavaConversions._ 
    1111 
    12 trait ConnectionSelector[T <: WithSocketChannel] extends Actor { 
    13   val intersOpts: Int 
     12 
     13case class Read (callback: Boolean => Unit) 
     14case class Write(callback: Boolean => Unit) 
     15 
     16abstract class SelectActor[T <: WithSelectableChannel](ops: Int) extends Actor { 
    1417  def fireEvent(key: SelectionKey, listerner: T) 
    1518 
     
    6467      for (listener <- listeners.iterator if listener.isOpen) { 
    6568        try { 
    66           listener.socketChannel.register(selector, intersOpts, listener) 
     69          listener.channel.register(selector, ops, listener) 
    6770        } catch {case ex: CancelledKeyException => ex.printStackTrace} 
    6871      } 
     
    7275} 
    7376 
    74 trait WithSocketChannel { 
     77trait WithSelectableChannel extends Actor { 
    7578  def isOpen: Boolean 
    76   def socketChannel: SelectableChannel 
     79  def channel: SelectableChannel 
    7780} 
  • blogbird/src/main/scala/org/aiotrade/httpd/coroutine/HttpdRoutines.scala

    r258 r259  
    7070import org.aiotrade.httpd.Request 
    7171import org.aiotrade.httpd.ClientConnection 
    72 import org.aiotrade.httpd.ConnectionSelector 
     72import org.aiotrade.httpd.SelectActor 
    7373import org.aiotrade.httpd.Debugger._ 
    7474import scala.actors.Actor 
     
    8888  } 
    8989 
    90   private object connectionSelector extends Actor { 
     90  private object acceptActor extends Actor { 
    9191 
    9292    /** 
     
    111111    debugln ("PortListener initializing...") 
    112112 
    113     connectionSelector.start 
     113    acceptActor.start 
    114114  } 
    115115} 
     
    124124  def stop { 
    125125    done = true 
    126     connectionSelector.stop 
    127   } 
    128  
    129   private object connectionSelector extends ConnectionSelector[ClientConnection] { 
    130     val intersOpts = SelectionKey.OP_READ 
     126    selectActor.stop 
     127  } 
     128 
     129  private object selectActor extends SelectActor[ClientConnection](SelectionKey.OP_READ) { 
    131130 
    132131    def fireEvent(key: SelectionKey, conn: ClientConnection) { 
     
    147146    debugln("Connection Manager initializing...") 
    148147     
    149     connectionSelector.start 
     148    selectActor.start 
    150149    while (!done) { 
    151150      val socketChannel = consume 
    152151      val conn = new ClientConnection(socketChannel) 
    153152      debugln(" * Got a requesting connection.") 
    154       connectionSelector.addListener(conn) 
     153      selectActor.addListener(conn) 
    155154    } 
    156155  } 
     
    201200  def stop { 
    202201    done = true 
    203     connectionSelector.stop 
    204   } 
    205  
    206   private object connectionSelector extends ConnectionSelector[ClientConnection] { 
     202    selectActor.stop 
     203  } 
     204 
     205  private object selectActor extends SelectActor[ClientConnection](SelectionKey.OP_WRITE) { 
    207206    val intersOpts = SelectionKey.OP_WRITE 
    208207 
     
    226225    debugln("Reply Sender initializing...") 
    227226 
    228     connectionSelector.start 
     227    selectActor.start 
    229228    while (!done) { 
    230229      val reply = consume 
     
    232231      conn.send(reply) 
    233232      debugln(" * Sending a reply.") 
    234       connectionSelector.addListener(conn) 
    235     } 
    236   } 
    237 } 
     233      selectActor.addListener(conn) 
     234    } 
     235  } 
     236}