Class: Protobuf::Rpc::ServiceDirectory

Inherits:
Object
  • Object
show all
Includes:
Logging, Singleton
Defined in:
lib/protobuf/rpc/service_directory.rb

Defined Under Namespace

Classes: Listing

Constant Summary collapse

DEFAULT_ADDRESS =
'0.0.0.0'.freeze
DEFAULT_PORT =
53000
DEFAULT_TIMEOUT =
1

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

initialize_logger, #log_exception, #log_signature, #logger, #sign_message

Constructor Details

#initializeServiceDirectory

Instance Methods



87
88
89
# File 'lib/protobuf/rpc/service_directory.rb', line 87

def initialize
  reset
end

Class Attribute Details

.addressObject



67
68
69
# File 'lib/protobuf/rpc/service_directory.rb', line 67

def self.address
  @address ||= DEFAULT_ADDRESS
end

.portObject



71
72
73
# File 'lib/protobuf/rpc/service_directory.rb', line 71

def self.port
  @port ||= DEFAULT_PORT
end

Class Method Details

.start {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



75
76
77
78
# File 'lib/protobuf/rpc/service_directory.rb', line 75

def self.start
  yield(self) if block_given?
  instance.start
end

.stopObject



80
81
82
# File 'lib/protobuf/rpc/service_directory.rb', line 80

def self.stop
  instance.stop
end

Instance Method Details

#all_listings_for(service) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/protobuf/rpc/service_directory.rb', line 91

def all_listings_for(service)
  if running? && @listings_by_service.key?(service.to_s)
    start_listener_thread if listener_dead?
    @listings_by_service[service.to_s].entries.shuffle
  else
    []
  end
end

#each_listing(&block) ⇒ Object



100
101
102
103
# File 'lib/protobuf/rpc/service_directory.rb', line 100

def each_listing(&block)
  start_listener_thread if listener_dead?
  @listings_by_uuid.each_value(&block)
end

#listener_dead?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/protobuf/rpc/service_directory.rb', line 112

def listener_dead?
  @thread.nil? || !@thread.alive?
end

#lookup(service) ⇒ Object



105
106
107
108
109
110
# File 'lib/protobuf/rpc/service_directory.rb', line 105

def lookup(service)
  return unless running?
  start_listener_thread if listener_dead?
  return unless @listings_by_service.key?(service.to_s)
  @listings_by_service[service.to_s].entries.sample
end

#restartObject



116
117
118
119
# File 'lib/protobuf/rpc/service_directory.rb', line 116

def restart
  stop
  start
end

#running?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/protobuf/rpc/service_directory.rb', line 121

def running?
  !!@running
end

#startObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/protobuf/rpc/service_directory.rb', line 125

def start
  unless running?
    init_socket
    logger.info { sign_message("listening to udp://#{self.class.address}:#{self.class.port}") }
    @running = true
  end

  start_listener_thread if listener_dead?
  self
end

#start_listener_threadObject



136
137
138
139
# File 'lib/protobuf/rpc/service_directory.rb', line 136

def start_listener_thread
  return if @thread.try(:alive?)
  @thread = Thread.new { send(:run) }
end

#stopObject



141
142
143
144
145
146
147
148
149
# File 'lib/protobuf/rpc/service_directory.rb', line 141

def stop
  logger.info { sign_message("Stopping directory") }

  @running = false
  @thread.try(:kill).try(:join)
  @socket.try(:close)

  reset
end