Class: DEVp2p::Discovery::Service
- Defined in:
- lib/devp2p/discovery/service.rb
Instance Attribute Summary collapse
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Attributes inherited from Service
Instance Method Summary collapse
- #address ⇒ Object
-
#initialize(app) ⇒ Service
constructor
A new instance of Service.
- #receive_message(address, message) ⇒ Object
- #send_message(address, message) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Methods inherited from Service
Methods included from Configurable
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
#protocol ⇒ Object (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
#address ⇒ Object
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
145 146 147 148 |
# File 'lib/devp2p/discovery/service.rb', line 145 def (address, ) raise ArgumentError, 'address must be Address' unless address.instance_of?(Address) @protocol. address, end |
#send_message(address, message) ⇒ Object
150 151 152 |
# File 'lib/devp2p/discovery/service.rb', line 150 def (address, ) @sender.async. address, end |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |