Class: Stub::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/uaa/stub/server.rb

Overview


Direct Known Subclasses

CF::UAA::StubUAA

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



249
250
251
# File 'lib/uaa/stub/server.rb', line 249

def host
  @host
end

#infoObject

Returns the value of attribute info.



250
251
252
# File 'lib/uaa/stub/server.rb', line 250

def info
  @info
end

#loggerObject (readonly)

Returns the value of attribute logger.



249
250
251
# File 'lib/uaa/stub/server.rb', line 249

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



249
250
251
# File 'lib/uaa/stub/server.rb', line 249

def port
  @port
end

#rootObject (readonly)

Returns the value of attribute root.



249
250
251
# File 'lib/uaa/stub/server.rb', line 249

def root
  @root
end

#statusObject (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

#runObject

Raises:

  • (ArgumentError)


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_threadObject

Raises:

  • (ArgumentError)


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.message}" }
      trace { e.backtrace }
      raise
    end
  end
  Thread.stop
  logger.debug "running on thread"
  self
end

#startObject

Raises:

  • (ArgumentError)


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

#stopObject

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

#urlObject



251
# File 'lib/uaa/stub/server.rb', line 251

def url; "http://#{@host}:#{@port}" end