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.


8
9
10
11
# File 'lib/dizby/worker/server.rb', line 8

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

Instance Method Details

#accept_connection (private)


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

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

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

#join


13
14
15
# File 'lib/dizby/worker/server.rb', line 13

def join
  @thread.join if @thread
end

#run (private)


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

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