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.



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/uaa/stub/server.rb', line 267

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 = []
  @mutex = Mutex.new
  @status = :stopped
  @server_thread = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



262
263
264
# File 'lib/uaa/stub/server.rb', line 262

def host
  @host
end

#infoObject

Returns the value of attribute info.



263
264
265
# File 'lib/uaa/stub/server.rb', line 263

def info
  @info
end

#loggerObject (readonly)

Returns the value of attribute logger.



262
263
264
# File 'lib/uaa/stub/server.rb', line 262

def logger
  @logger
end

#portObject (readonly)

Returns the value of attribute port.



262
263
264
# File 'lib/uaa/stub/server.rb', line 262

def port
  @port
end

#rootObject (readonly)

Returns the value of attribute root.



262
263
264
# File 'lib/uaa/stub/server.rb', line 262

def root
  @root
end

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

#runObject

Start the server and run the accept loop on the calling thread (blocking).

Raises:

  • (ArgumentError)


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_threadObject

Start the server and run the accept loop on a background thread. Returns immediately; caller can use #url and #port right away.

Raises:

  • (ArgumentError)


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

#startObject

Raises:

  • (ArgumentError)


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

#stopObject

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

#urlObject



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

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