Class: ChefZero::SocketlessServerMap

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chef_zero/socketless_server_map.rb

Constant Summary collapse

MUTEX =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSocketlessServerMap


44
45
46
# File 'lib/chef_zero/socketless_server_map.rb', line 44

def initialize()
  reset!
end

Class Method Details

.request(port, request_env) ⇒ Object


32
33
34
# File 'lib/chef_zero/socketless_server_map.rb', line 32

def self.request(port, request_env)
  instance.request(port, request_env)
end

.server_on_port(port) ⇒ Object


36
37
38
# File 'lib/chef_zero/socketless_server_map.rb', line 36

def self.server_on_port(port)
  instance.server_on_port(port)
end

Instance Method Details

#deregister(port) ⇒ Object


78
79
80
81
82
# File 'lib/chef_zero/socketless_server_map.rb', line 78

def deregister(port)
  MUTEX.synchronize do
    @servers_by_port.delete(port)
  end
end

#has_server_on_port?(port) ⇒ Boolean


70
71
72
# File 'lib/chef_zero/socketless_server_map.rb', line 70

def has_server_on_port?(port)
  @servers_by_port.key?(port)
end

#register_no_listen_server(server) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chef_zero/socketless_server_map.rb', line 58

def register_no_listen_server(server)
  MUTEX.synchronize do
    1.upto(1000) do |port|
      unless @servers_by_port.key?(port)
        @servers_by_port[port] = server
        return port
      end
    end
    raise NoSocketlessPortAvailable, "No socketless ports left to register"
  end
end

#register_port(port, server) ⇒ Object


52
53
54
55
56
# File 'lib/chef_zero/socketless_server_map.rb', line 52

def register_port(port, server)
  MUTEX.synchronize do
    @servers_by_port[port] = server
  end
end

#request(port, request_env) ⇒ Object

Raises:


84
85
86
87
88
# File 'lib/chef_zero/socketless_server_map.rb', line 84

def request(port, request_env)
  server = @servers_by_port[port]
  raise ServerNotFound, "No socketless chef-zero server on given port #{port.inspect}" unless server
  server.handle_socketless_request(request_env)
end

#reset!Object


48
49
50
# File 'lib/chef_zero/socketless_server_map.rb', line 48

def reset!
  @servers_by_port = {}
end

#server_on_port(port) ⇒ Object


74
75
76
# File 'lib/chef_zero/socketless_server_map.rb', line 74

def server_on_port(port)
  @servers_by_port[port]
end