Class: EventMachine::Bitcoin::Connection

Inherits:
EM::Connection
  • Object
show all
Includes:
ConnectionHandler
Defined in:
lib/em_bitcoin.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConnectionHandler

#on_addr, #on_block, #on_get_block, #on_get_transaction, #on_handshake_begin, #on_handshake_complete, #on_inv_block, #on_inv_transaction, #on_ping, #on_tx, #on_verack, #on_version, #query_blocks

Constructor Details

#initialize(host, port) ⇒ Connection

Returns a new instance of Connection.



80
81
82
83
# File 'lib/em_bitcoin.rb', line 80

def initialize(host, port)
  @sockaddr = [host, port]
  @parser = Bitcoin::Protocol::Parser.new( self )
end

Class Method Details

.connect(host, port) ⇒ Object



99
100
101
# File 'lib/em_bitcoin.rb', line 99

def self.connect(host, port)
  EM.connect(host, port, self, host, port)
end

.connect_random_from_dnsObject



103
104
105
106
107
108
109
110
111
# File 'lib/em_bitcoin.rb', line 103

def self.connect_random_from_dns
  seeds = Bitcoin.network[:dns_seeds]
  if seeds.any?
    host = Resolv::DNS.new.getaddresses(seeds.sample).map {|a| a.to_s}.sample
    connect(host, Bitcoin::network[:default_port])
  else
    raise "No DNS seeds available. Provide IP, configure seeds, or use different network."
  end
end

Instance Method Details

#post_initObject



85
86
87
88
# File 'lib/em_bitcoin.rb', line 85

def post_init
  p ['connected', @sockaddr]
  EM.schedule{ on_handshake_begin }
end

#receive_data(data) ⇒ Object



90
91
92
# File 'lib/em_bitcoin.rb', line 90

def receive_data(data)
  @parser.parse(data)
end

#unbindObject



94
95
96
97
# File 'lib/em_bitcoin.rb', line 94

def unbind
  p ['disconnected', @sockaddr]
  self.class.connect_random_from_dns
end