class Stub::Server

Attributes

host[R]
info[RW]
logger[R]
port[R]
root[R]
status[R]

Public Class Methods

new(req_handler, options) click to toggle 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

delete_connection(conn) click to toggle 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
run() click to toggle 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
run_on_thread() click to toggle 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
start() click to toggle 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
stop() click to toggle source

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

# 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) click to toggle source
# File lib/uaa/stub/server.rb, line 252
def trace(msg = nil, &blk); logger.trace(msg, &blk) if logger.respond_to?(:trace) end
url() click to toggle source
# File lib/uaa/stub/server.rb, line 251
def url; "http://#{@host}:#{@port}" end

Private Instance Methods

done() click to toggle 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
initialize_connection(conn) click to toggle 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