Changeset 259:25cafbcbb963
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r255
|
r259
|
|
| 9 | 9 | |
| 10 | 10 | |
| 11 | | case class Read (callback: Boolean => Unit) |
| 12 | | case class Write(callback: Boolean => Unit) |
| 13 | | |
| 14 | 11 | /** |
| 15 | 12 | * Represents an open HTTP connection coming from a client. |
| 16 | 13 | */ |
| 17 | | class ClientConnection(val socketChannel: SocketChannel) extends Actor with WithSocketChannel { |
| | 14 | class ClientConnection(val channel: SocketChannel) extends Actor with WithSelectableChannel { |
| 18 | 15 | private var _isOpen = true |
| 19 | 16 | private var requestParser = new HttpRequestParser |
| … |
… |
|
| 25 | 22 | * Is this connection currentnly open? |
| 26 | 23 | */ |
| 27 | | def isOpen = _isOpen && socketChannel.isOpen && socketChannel.isConnected |
| | 24 | def isOpen = _isOpen && channel.isOpen && channel.isConnected |
| 28 | 25 | |
| 29 | 26 | /** |
| … |
… |
|
| 32 | 29 | def close { |
| 33 | 30 | _isOpen = false |
| 34 | | socketChannel.close |
| | 31 | channel.close |
| 35 | 32 | super.exit |
| 36 | 33 | } |
| … |
… |
|
| 87 | 84 | var numRead = -1 |
| 88 | 85 | try { |
| 89 | | numRead = socketChannel.read(readBuf) |
| | 86 | numRead = channel.read(readBuf) |
| 90 | 87 | } catch { |
| 91 | 88 | case ioe: IOException => |
| … |
… |
|
| 143 | 140 | if (writeBuffers.head.remaining > 0) { |
| 144 | 141 | try { |
| 145 | | socketChannel.write(writeBuffers.head) |
| | 142 | channel.write(writeBuffers.head) |
| 146 | 143 | } catch { |
| 147 | 144 | case ioe: IOException => |
-
|
r258
|
r259
|
|
| 18 | 18 | debugln ("PortListener initializing...") |
| 19 | 19 | |
| 20 | | connectionSelector.start |
| | 20 | acceptActor.start |
| 21 | 21 | |
| 22 | 22 | def stop { |
| 23 | | connectionSelector.stop |
| | 23 | acceptActor.stop |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | | private object connectionSelector extends Actor { |
| | 26 | private object acceptActor extends Actor { |
| 27 | 27 | val serverChannel = ServerSocketChannel.open |
| 28 | 28 | serverChannel.socket.bind(new InetSocketAddress(localAddress, port)) |
| … |
… |
|
| 59 | 59 | debugln("Connection Manager initializing...") |
| 60 | 60 | |
| 61 | | connectionSelector.start |
| | 61 | selectActor.start |
| 62 | 62 | |
| 63 | 63 | def stop { |
| 64 | | connectionSelector.stop |
| | 64 | selectActor.stop |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | | private object connectionSelector extends ConnectionSelector[ClientConnection] { |
| 68 | | val intersOpts = SelectionKey.OP_READ |
| | 67 | private object selectActor extends SelectActor[ClientConnection](SelectionKey.OP_READ) { |
| 69 | 68 | |
| 70 | 69 | def fireEvent(key: SelectionKey, conn: ClientConnection) { |
| … |
… |
|
| 86 | 85 | |
| 87 | 86 | val conn = new ClientConnection(socketChannel) |
| 88 | | connectionSelector.addListener(conn) |
| | 87 | selectActor.addListener(conn) |
| 89 | 88 | } |
| 90 | 89 | } |
| … |
… |
|
| 122 | 121 | debugln("Reply Sender initializing...") |
| 123 | 122 | |
| 124 | | connectionSelector.start |
| | 123 | selectActor.start |
| 125 | 124 | |
| 126 | 125 | def stop { |
| 127 | | connectionSelector.stop |
| | 126 | selectActor.stop |
| 128 | 127 | } |
| 129 | 128 | |
| 130 | | private object connectionSelector extends ConnectionSelector[ClientConnection] { |
| 131 | | val intersOpts = SelectionKey.OP_WRITE |
| | 129 | private object selectActor extends SelectActor[ClientConnection](SelectionKey.OP_WRITE) { |
| 132 | 130 | |
| 133 | 131 | def fireEvent(key: SelectionKey, conn: ClientConnection) { |
| … |
… |
|
| 151 | 149 | conn.send(reply) |
| 152 | 150 | debugln(" * Sending a reply.") |
| 153 | | connectionSelector.addListener(conn) |
| | 151 | selectActor.addListener(conn) |
| 154 | 152 | } |
| 155 | 153 | } |
-
|
r253
|
r259
|
|
| 10 | 10 | import scala.collection.JavaConversions._ |
| 11 | 11 | |
| 12 | | trait ConnectionSelector[T <: WithSocketChannel] extends Actor { |
| 13 | | val intersOpts: Int |
| | 12 | |
| | 13 | case class Read (callback: Boolean => Unit) |
| | 14 | case class Write(callback: Boolean => Unit) |
| | 15 | |
| | 16 | abstract class SelectActor[T <: WithSelectableChannel](ops: Int) extends Actor { |
| 14 | 17 | def fireEvent(key: SelectionKey, listerner: T) |
| 15 | 18 | |
| … |
… |
|
| 64 | 67 | for (listener <- listeners.iterator if listener.isOpen) { |
| 65 | 68 | try { |
| 66 | | listener.socketChannel.register(selector, intersOpts, listener) |
| | 69 | listener.channel.register(selector, ops, listener) |
| 67 | 70 | } catch {case ex: CancelledKeyException => ex.printStackTrace} |
| 68 | 71 | } |
| … |
… |
|
| 72 | 75 | } |
| 73 | 76 | |
| 74 | | trait WithSocketChannel { |
| | 77 | trait WithSelectableChannel extends Actor { |
| 75 | 78 | def isOpen: Boolean |
| 76 | | def socketChannel: SelectableChannel |
| | 79 | def channel: SelectableChannel |
| 77 | 80 | } |
-
|
r258
|
r259
|
|
| 70 | 70 | import org.aiotrade.httpd.Request |
| 71 | 71 | import org.aiotrade.httpd.ClientConnection |
| 72 | | import org.aiotrade.httpd.ConnectionSelector |
| | 72 | import org.aiotrade.httpd.SelectActor |
| 73 | 73 | import org.aiotrade.httpd.Debugger._ |
| 74 | 74 | import scala.actors.Actor |
| … |
… |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | | private object connectionSelector extends Actor { |
| | 90 | private object acceptActor extends Actor { |
| 91 | 91 | |
| 92 | 92 | /** |
| … |
… |
|
| 111 | 111 | debugln ("PortListener initializing...") |
| 112 | 112 | |
| 113 | | connectionSelector.start |
| | 113 | acceptActor.start |
| 114 | 114 | } |
| 115 | 115 | } |
| … |
… |
|
| 124 | 124 | def stop { |
| 125 | 125 | 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) { |
| 131 | 130 | |
| 132 | 131 | def fireEvent(key: SelectionKey, conn: ClientConnection) { |
| … |
… |
|
| 147 | 146 | debugln("Connection Manager initializing...") |
| 148 | 147 | |
| 149 | | connectionSelector.start |
| | 148 | selectActor.start |
| 150 | 149 | while (!done) { |
| 151 | 150 | val socketChannel = consume |
| 152 | 151 | val conn = new ClientConnection(socketChannel) |
| 153 | 152 | debugln(" * Got a requesting connection.") |
| 154 | | connectionSelector.addListener(conn) |
| | 153 | selectActor.addListener(conn) |
| 155 | 154 | } |
| 156 | 155 | } |
| … |
… |
|
| 201 | 200 | def stop { |
| 202 | 201 | 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) { |
| 207 | 206 | val intersOpts = SelectionKey.OP_WRITE |
| 208 | 207 | |
| … |
… |
|
| 226 | 225 | debugln("Reply Sender initializing...") |
| 227 | 226 | |
| 228 | | connectionSelector.start |
| | 227 | selectActor.start |
| 229 | 228 | while (!done) { |
| 230 | 229 | val reply = consume |
| … |
… |
|
| 232 | 231 | conn.send(reply) |
| 233 | 232 | debugln(" * Sending a reply.") |
| 234 | | connectionSelector.addListener(conn) |
| 235 | | } |
| 236 | | } |
| 237 | | } |
| | 233 | selectActor.addListener(conn) |
| | 234 | } |
| | 235 | } |
| | 236 | } |