Class: DEVp2p::Discovery::Service

Inherits:
Service
  • Object
show all
Defined in:
lib/devp2p/discovery/service.rb

Instance Attribute Summary collapse

Attributes inherited from Service

#app

Instance Method Summary collapse

Methods inherited from Service

register_with_app, #to_s

Methods included from Configurable

#add_config

Constructor Details

#initialize(app) ⇒ Service

Returns a new instance of Service.



96
97
98
99
100
101
102
# File 'lib/devp2p/discovery/service.rb', line 96

def initialize(app)
  super(app)
  logger.info "Discovery service init"

  @socket = nil
  @protocol = Protocol.new app, self
end

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



94
95
96
# File 'lib/devp2p/discovery/service.rb', line 94

def protocol
  @protocol
end

Instance Method Details

#addressObject



139
140
141
142
143
# File 'lib/devp2p/discovery/service.rb', line 139

def address
  ip = @app.config[:discovery][:listen_host]
  port = @app.config[:discovery][:listen_port]
  Address.new ip, port
end

#receive_message(address, message) ⇒ Object

Raises:

  • (ArgumentError)


145
146
147
148
# File 'lib/devp2p/discovery/service.rb', line 145

def receive_message(address, message)
  raise ArgumentError, 'address must be Address' unless address.instance_of?(Address)
  @protocol.receive_message address, message
end

#send_message(address, message) ⇒ Object



150
151
152
# File 'lib/devp2p/discovery/service.rb', line 150

def send_message(address, message)
  @sender.async.send_message address, message
end

#startObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/devp2p/discovery/service.rb', line 104

def start
  logger.info 'starting discovery'

  ip = @app.config[:discovery][:listen_host]
  port = @app.config[:discovery][:listen_port]

  logger.info "starting udp listener", port: port, host: ip

  @socket = UDPSocket.new
  @socket.bind ip, port

  @receiver = Receiver.new self, @socket
  @sender = Sender.new self, @socket
  @receiver.async.start
  @sender.async.start

  nodes = @app.config[:discovery][:bootstrap_nodes] || []
  @protocol.bootstrap( nodes.map {|x| Node.from_uri(x) } )
rescue
  puts $!
  puts $!.backtrace[0,10].join("\n")
end

#stopObject



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/devp2p/discovery/service.rb', line 127

def stop
  logger.info "stopping discovery"

  @socket.close if @socket
  @sender.async.stop if @sender
  @receiver.async.stop if @receiver

  @socket = nil
  @sender = nil
  @receiver = nil
end