Class: Dizby::ServiceWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/dizby/worker/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ ServiceWorker

Returns a new instance of ServiceWorker.


6
7
8
9
# File 'lib/dizby/worker/server.rb', line 6

def initialize(server)
  @server = server
  @thread = Thread.start { run }
end

Instance Method Details

#accept_connection (private)


31
32
33
34
35
36
37
# File 'lib/dizby/worker/server.rb', line 31

def accept_connection
  connection = @server.accept
  return nil unless connection

  @server.add_uri_alias connection.remote_uri
  ConnectionWorker.new(@server, connection)
end

#join


11
12
13
# File 'lib/dizby/worker/server.rb', line 11

def join
  @thread.join if @thread
end

#run (private)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dizby/worker/server.rb', line 17

def run
  connections = []
  loop do
    conn = accept_connection
    connections << conn if conn
  end
rescue LocalServerShutdown
  @server.log.debug('Server shutdown')
ensure
  @server.close if @server.alive?

  connections.each(&:close)
end