Class: HttpServerActionHelper::GeneralServer
- Inherits:
-
Object
- Object
- HttpServerActionHelper::GeneralServer
- Defined in:
- lib/action/module/httpserver_action.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_HOST =
"127.0.0.1"
- @@services =
Hash of opened ports, i.e. services
{}
- @@servicesMutex =
Mutex.new
Instance Attribute Summary collapse
-
#audit ⇒ Object
Returns the value of attribute audit.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#maxConnections ⇒ Object
readonly
Returns the value of attribute maxConnections.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#stdlog ⇒ Object
Returns the value of attribute stdlog.
Class Method Summary collapse
Instance Method Summary collapse
- #connections ⇒ Object
-
#initialize(port, host = DEFAULT_HOST, maxConnections = 4, stdlog = $stderr, audit = false, debug = false) ⇒ GeneralServer
constructor
A new instance of GeneralServer.
- #join ⇒ Object
- #serve(io) ⇒ Object
- #shutdown ⇒ Object
- #start(maxConnections = -1,, &blk) ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize(port, host = DEFAULT_HOST, maxConnections = 4, stdlog = $stderr, audit = false, debug = false) ⇒ GeneralServer
Returns a new instance of GeneralServer.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/action/module/httpserver_action.rb', line 106 def initialize(port, host = DEFAULT_HOST, maxConnections = 4, stdlog = $stderr, audit = false, debug = false) @tcpServerThread = nil @port = port @host = host @maxConnections = maxConnections @connections = [] @connectionsMutex = Mutex.new @connectionsCV = ConditionVariable.new @stdlog = stdlog @audit = audit @debug = debug end |
Instance Attribute Details
#audit ⇒ Object
Returns the value of attribute audit.
66 67 68 |
# File 'lib/action/module/httpserver_action.rb', line 66 def audit @audit end |
#debug ⇒ Object
Returns the value of attribute debug.
66 67 68 |
# File 'lib/action/module/httpserver_action.rb', line 66 def debug @debug end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
65 66 67 |
# File 'lib/action/module/httpserver_action.rb', line 65 def host @host end |
#maxConnections ⇒ Object (readonly)
Returns the value of attribute maxConnections.
65 66 67 |
# File 'lib/action/module/httpserver_action.rb', line 65 def maxConnections @maxConnections end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
65 66 67 |
# File 'lib/action/module/httpserver_action.rb', line 65 def port @port end |
#stdlog ⇒ Object
Returns the value of attribute stdlog.
66 67 68 |
# File 'lib/action/module/httpserver_action.rb', line 66 def stdlog @stdlog end |
Class Method Details
.in_service?(port, host = DEFAULT_HOST) ⇒ Boolean
35 36 37 38 |
# File 'lib/action/module/httpserver_action.rb', line 35 def GeneralServer.in_service?(port, host = DEFAULT_HOST) @@services.has_key?(host) and @@services[host].has_key?(port) end |
.stop(port, host = DEFAULT_HOST) ⇒ Object
29 30 31 32 33 |
# File 'lib/action/module/httpserver_action.rb', line 29 def GeneralServer.stop(port, host = DEFAULT_HOST) @@servicesMutex.synchronize { @@services[host][port].stop } end |
Instance Method Details
#connections ⇒ Object
57 58 59 |
# File 'lib/action/module/httpserver_action.rb', line 57 def connections @connections.size end |
#join ⇒ Object
61 62 63 |
# File 'lib/action/module/httpserver_action.rb', line 61 def join @tcpServerThread.join if @tcpServerThread end |
#serve(io) ⇒ Object
22 23 24 |
# File 'lib/action/module/httpserver_action.rb', line 22 def serve(io) yield if block_given? end |
#shutdown ⇒ Object
53 54 55 |
# File 'lib/action/module/httpserver_action.rb', line 53 def shutdown @shutdown = true end |
#start(maxConnections = -1,, &blk) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/action/module/httpserver_action.rb', line 120 def start(maxConnections = -1, &blk) raise "running" if !stopped? @shutdown = false @maxConnections = maxConnections if maxConnections > 0 @@servicesMutex.synchronize { if GeneralServer.in_service?(@port,@host) raise "Port already in use: #{host}:#{@port}!" end @tcpServer = TCPServer.new(@host,@port) @port = @tcpServer.addr[1] @@services[@host] = {} unless @@services.has_key?(@host) @@services[@host][@port] = self; } @tcpServerThread = Thread.new { begin starting if @audit while !@shutdown @connectionsMutex.synchronize { while @connections.size >= @maxConnections @connectionsCV.wait(@connectionsMutex) end } client = @tcpServer.accept @connections << Thread.new(client) { |myClient| begin myPort = myClient.peeraddr[1] serve(myClient, &blk) if !@audit or connecting(myClient) rescue => detail error(detail) if @debug ensure begin myClient.close rescue end @connectionsMutex.synchronize { @connections.delete(Thread.current) @connectionsCV.signal } disconnecting(myPort) if @audit end } end rescue => detail error(detail) if @debug ensure begin @tcpServer.close rescue end if @shutdown @connectionsMutex.synchronize { while @connections.size > 0 @connectionsCV.wait(@connectionsMutex) end } else @connections.each { |c| c.raise "stop" } end @tcpServerThread = nil @@servicesMutex.synchronize { @@services[@host].delete(@port) } stopping if @audit end } self end |
#stop ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/action/module/httpserver_action.rb', line 40 def stop @connectionsMutex.synchronize { if @tcpServerThread @tcpServerThread.raise "stop" end } @tcpServerThread.join end |
#stopped? ⇒ Boolean
49 50 51 |
# File 'lib/action/module/httpserver_action.rb', line 49 def stopped? @tcpServerThread == nil end |