Class: Stub::Server
- Inherits:
-
Object
- Object
- Stub::Server
- Defined in:
- lib/uaa/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.
254 255 256 257 258 259 260 261 262 |
# File 'lib/uaa/stub/server.rb', line 254 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.
249 250 251 |
# File 'lib/uaa/stub/server.rb', line 249 def host @host end |
#info ⇒ Object
Returns the value of attribute info.
250 251 252 |
# File 'lib/uaa/stub/server.rb', line 250 def info @info end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
249 250 251 |
# File 'lib/uaa/stub/server.rb', line 249 def logger @logger end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
249 250 251 |
# File 'lib/uaa/stub/server.rb', line 249 def port @port end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
249 250 251 |
# File 'lib/uaa/stub/server.rb', line 249 def root @root end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
249 250 251 |
# File 'lib/uaa/stub/server.rb', line 249 def status @status end |
Instance Method Details
#delete_connection(conn) ⇒ Object
314 315 316 317 318 319 |
# File 'lib/uaa/stub/server.rb', line 314 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
295 296 297 298 299 300 |
# File 'lib/uaa/stub/server.rb', line 295 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
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/uaa/stub/server.rb', line 276 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
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/uaa/stub/server.rb', line 264 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
306 307 308 309 310 311 312 |
# File 'lib/uaa/stub/server.rb', line 306 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
252 |
# File 'lib/uaa/stub/server.rb', line 252 def trace(msg = nil, &blk); logger.trace(msg, &blk) if logger.respond_to?(:trace) end |
#url ⇒ Object
251 |
# File 'lib/uaa/stub/server.rb', line 251 def url; "http://#{@host}:#{@port}" end |