Class: Stub::Server
- Inherits:
-
Object
- Object
- Stub::Server
- Defined in:
- lib/stub/server.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#info ⇒ Object
Returns the value of attribute info.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #delete_connection(conn) ⇒ Object
-
#initialize(req_handler, options) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #run_on_thread ⇒ Object
- #start ⇒ Object
-
#stop ⇒ Object
if on reactor thread, start shutting down but return if connections still in process, and let them disconnect when complete – server is not really done until it’s status is stopped.
- #trace(msg = nil, &blk) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(req_handler, options) ⇒ Server
Returns a new instance of Server.
247 248 249 250 251 252 253 254 255 |
# File 'lib/stub/server.rb', line 247 def initialize(req_handler, ) @req_handler = req_handler @logger = [:logger] || Logger.new($stdout) @info = [:info] @host = [:host] || "localhost" @init_port = [:port] || 0 @root = [:root] @connections, @status, @sig, @em_thread = [], :stopped, nil, nil end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
242 243 244 |
# File 'lib/stub/server.rb', line 242 def host @host end |
#info ⇒ Object
Returns the value of attribute info.
243 244 245 |
# File 'lib/stub/server.rb', line 243 def info @info end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
242 243 244 |
# File 'lib/stub/server.rb', line 242 def logger @logger end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
242 243 244 |
# File 'lib/stub/server.rb', line 242 def port @port end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
242 243 244 |
# File 'lib/stub/server.rb', line 242 def root @root end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
242 243 244 |
# File 'lib/stub/server.rb', line 242 def status @status end |
Instance Method Details
#delete_connection(conn) ⇒ Object
307 308 309 310 311 312 |
# File 'lib/stub/server.rb', line 307 def delete_connection(conn) logger.debug "deleting connection" fail unless EM.reactor_thread? @connections.delete(conn) done if @status != :running && @connections.empty? end |
#run ⇒ Object
288 289 290 291 292 293 |
# File 'lib/stub/server.rb', line 288 def run raise ArgumentError, "can't run, EventMachine already running" if EM.reactor_running? @em_thread = Thread.current EM.run { start } logger.debug "server and event machine done" end |
#run_on_thread ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/stub/server.rb', line 269 def run_on_thread raise ArgumentError, "can't run on thread, EventMachine already running" if EM.reactor_running? logger.debug { "starting eventmachine on thread" } cthred = Thread.current @em_thread = Thread.new do begin EM.run { start; cthred.run } logger.debug "server thread done" rescue Exception => e logger.debug { "unhandled exception on stub server thread: #{e.}" } trace { e.backtrace } raise end end Thread.stop logger.debug "running on thread" self end |
#start ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/stub/server.rb', line 257 def start raise ArgumentError, "attempt to start a server that's already running" unless @status == :stopped logger.debug "starting #{self.class} server #{@host}" EM.schedule do @sig = EM.start_server(@host, @init_port, Connection) { |c| initialize_connection(c) } @port = Socket.unpack_sockaddr_in(EM.get_sockname(@sig))[0] logger.info "#{self.class} server started at #{url}" end @status = :running self end |
#stop ⇒ Object
if on reactor thread, start shutting down but return if connections still in process, and let them disconnect when complete – server is not really done until it’s status is stopped. if not on reactor thread, wait until everything’s cleaned up and stopped
299 300 301 302 303 304 305 |
# File 'lib/stub/server.rb', line 299 def stop logger.debug "stopping server" @status = :stopping EM.stop_server @sig done if @connections.empty? sleep 0.1 while @status != :stopped unless EM.reactor_thread? end |
#trace(msg = nil, &blk) ⇒ Object
245 |
# File 'lib/stub/server.rb', line 245 def trace(msg = nil, &blk); logger.trace(msg, &blk) if logger.respond_to?(:trace) end |
#url ⇒ Object
244 |
# File 'lib/stub/server.rb', line 244 def url; "http://#{@host}:#{@port}" end |