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
Start the server and run the accept loop on the calling thread (blocking).
-
#run_on_thread ⇒ Object
Start the server and run the accept loop on a background thread.
- #start ⇒ Object
-
#stop ⇒ Object
Stop accepting new connections, close the listening socket, and wait for the accept loop thread to mark status :stopped.
- #trace(msg = nil, &blk) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(req_handler, options) ⇒ Server
Returns a new instance of Server.
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/uaa/stub/server.rb', line 267 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 = [] @mutex = Mutex.new @status = :stopped @server_thread = nil end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
262 263 264 |
# File 'lib/uaa/stub/server.rb', line 262 def host @host end |
#info ⇒ Object
Returns the value of attribute info.
263 264 265 |
# File 'lib/uaa/stub/server.rb', line 263 def info @info end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
262 263 264 |
# File 'lib/uaa/stub/server.rb', line 262 def logger @logger end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
262 263 264 |
# File 'lib/uaa/stub/server.rb', line 262 def port @port end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
262 263 264 |
# File 'lib/uaa/stub/server.rb', line 262 def root @root end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
262 263 264 |
# File 'lib/uaa/stub/server.rb', line 262 def status @status end |
Instance Method Details
#delete_connection(conn) ⇒ Object
319 320 321 322 |
# File 'lib/uaa/stub/server.rb', line 319 def delete_connection(conn) logger.debug "deleting connection" @mutex.synchronize { @connections.delete(conn) } end |
#run ⇒ Object
Start the server and run the accept loop on the calling thread (blocking).
302 303 304 305 306 307 308 |
# File 'lib/uaa/stub/server.rb', line 302 def run raise ArgumentError, "can't run, server already running" if @status == :running @server_thread = Thread.current start accept_loop logger.debug "server and event loop done" end |
#run_on_thread ⇒ Object
Start the server and run the accept loop on a background thread. Returns immediately; caller can use #url and #port right away.
292 293 294 295 296 297 298 299 |
# File 'lib/uaa/stub/server.rb', line 292 def run_on_thread raise ArgumentError, "can't run on thread, server already running" if @status == :running logger.debug "starting server on thread" start @server_thread = Thread.new { accept_loop } logger.debug "running on thread" self end |
#start ⇒ Object
280 281 282 283 284 285 286 287 288 |
# File 'lib/uaa/stub/server.rb', line 280 def start raise ArgumentError, "attempt to start a server that's already running" unless @status == :stopped logger.debug "starting #{self.class} server #{@host}" @tcp_server = TCPServer.new(@host, @init_port) @port = @tcp_server.addr[1] logger.info "#{self.class} server started at #{url}" @status = :running self end |
#stop ⇒ Object
Stop accepting new connections, close the listening socket, and wait for the accept loop thread to mark status :stopped.
312 313 314 315 316 317 |
# File 'lib/uaa/stub/server.rb', line 312 def stop logger.debug "stopping server" @status = :stopping @tcp_server.close rescue nil sleep 0.05 while @status != :stopped end |
#trace(msg = nil, &blk) ⇒ Object
265 |
# File 'lib/uaa/stub/server.rb', line 265 def trace(msg = nil, &blk); logger.trace(msg, &blk) if logger.respond_to?(:trace) end |
#url ⇒ Object
264 |
# File 'lib/uaa/stub/server.rb', line 264 def url; "http://#{@host}:#{@port}" end |