Class: Grocer::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
15
# File 'lib/grocer/server.rb', line 10

def initialize(server)
  @server = server

  @clients = []
  @notifications = Queue.new
end

Instance Attribute Details

#notificationsObject (readonly)

Returns the value of attribute notifications.



8
9
10
# File 'lib/grocer/server.rb', line 8

def notifications
  @notifications
end

Instance Method Details

#acceptObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/grocer/server.rb', line 17

def accept
  Thread.new {
    begin
      @server.accept { |client|
        @clients << client

        Thread.new {
          begin
            NotificationReader.new(client).each(&notifications.method(:push))
          rescue Errno::EBADF, NoMethodError, OpenSSL::OpenSSLError
            # Expected when another thread closes the socket
          end
        }
      }
    rescue Errno::EBADF
      # Expected when another thread closes the socket
    end
  }
end

#closeObject



37
38
39
40
41
42
43
44
45
# File 'lib/grocer/server.rb', line 37

def close
  if @server
    @server.close
    @server = nil
  end

  @clients.each(&:close)
  @clients = []
end