Class: Bitcoin::Connection

Inherits:
EM::Connection
  • Object
show all
Includes:
ConnectionHandler
Defined in:
lib/bitcoin/connection.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_tx, #on_verack, #on_version, #query_blocks

Constructor Details

#initialize(host, port, connections) ⇒ Connection

Returns a new instance of Connection.



83
84
85
86
87
# File 'lib/bitcoin/connection.rb', line 83

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

Class Method Details

.connect(host, port, connections) ⇒ Object



103
104
105
# File 'lib/bitcoin/connection.rb', line 103

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

.connect_random_from_dns(connections) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/bitcoin/connection.rb', line 107

def self.connect_random_from_dns(connections)
  seeds = Bitcoin.network[:dns_seeds]
  if seeds.any?
    host = `nslookup #{seeds.sample}`.scan(/Address\: (.+)$/).flatten.sample
    connect(host, Bitcoin::network[:default_port], connections)
  else
    raise "No DNS seeds available. Provide IP, configure seeds, or use different network."
  end
end

Instance Method Details

#post_initObject



89
90
91
92
# File 'lib/bitcoin/connection.rb', line 89

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

#receive_data(data) ⇒ Object



94
95
96
# File 'lib/bitcoin/connection.rb', line 94

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

#unbindObject



98
99
100
101
# File 'lib/bitcoin/connection.rb', line 98

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