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"
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



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

def initialize
  reset
end

Class Attribute Details

.addressObject



59
60
61
# File 'lib/protobuf/rpc/service_directory.rb', line 59

def self.address
  @address ||= DEFAULT_ADDRESS
end

.portObject



63
64
65
# File 'lib/protobuf/rpc/service_directory.rb', line 63

def self.port
  @port ||= DEFAULT_PORT
end

Class Method Details

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

Yields:

  • (_self)

Yield Parameters:



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

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

.stopObject



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

def self.stop
  instance.stop
end

Instance Method Details

#all_listings_for(service) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/protobuf/rpc/service_directory.rb', line 83

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



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

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

#listener_dead?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/protobuf/rpc/service_directory.rb', line 104

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

#lookup(service) ⇒ Object



97
98
99
100
101
102
# File 'lib/protobuf/rpc/service_directory.rb', line 97

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



108
109
110
111
# File 'lib/protobuf/rpc/service_directory.rb', line 108

def restart
  stop
  start
end

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  !!@running
end

#startObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/protobuf/rpc/service_directory.rb', line 117

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



128
129
130
131
# File 'lib/protobuf/rpc/service_directory.rb', line 128

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

#stopObject



133
134
135
136
137
138
139
140
141
# File 'lib/protobuf/rpc/service_directory.rb', line 133

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

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

  reset
end