class Stub::Server
Attributes
Public Class Methods
Source
# File lib/uaa/stub/server.rb, line 254 def initialize(req_handler, options) @req_handler = req_handler @logger = options[:logger] || Logger.new($stdout) @info = options[:info] @host = options[:host] || "localhost" @init_port = options[:port] || 0 @root = options[:root] @connections, @status, @sig, @em_thread = [], :stopped, nil, nil end
Public Instance Methods
Source
# 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
Source
# 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
Source
# 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.message}" } trace { e.backtrace } raise end end Thread.stop logger.debug "running on thread" self end
Source
# 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
Source
# 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
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
Source
# File lib/uaa/stub/server.rb, line 252 def trace(msg = nil, &blk); logger.trace(msg, &blk) if logger.respond_to?(:trace) end
Private Instance Methods
Source
# File lib/uaa/stub/server.rb, line 232 def done fail unless @connections.empty? EM.stop if @em_thread && EM.reactor_running? @connections, @status, @sig, @em_thread = [], :stopped, nil, nil sleep 0.1 unless EM.reactor_thread? # give EM a chance to stop logger.debug EM.reactor_running?? "server done but EM still running": "server really done" end
Source
# File lib/uaa/stub/server.rb, line 240 def initialize_connection(conn) logger.debug "starting connection" fail unless EM.reactor_thread? @connections << conn conn.req_handler, conn.comm_inactivity_timeout = @req_handler.new(self), 30 end