Class: Dcha::Peer

Inherits:
Object
  • Object
show all
Includes:
CanHeartbeat, HasBlockchain, HasTrie, RemoteExecutable, Observable
Defined in:
lib/dcha/peer.rb,
lib/dcha/peer/has_trie.rb,
lib/dcha/peer/can_heartbeat.rb,
lib/dcha/peer/has_blockchain.rb,
lib/dcha/peer/remote_executable.rb

Overview

:nodoc:

Defined Under Namespace

Modules: CanHeartbeat, HasBlockchain, HasTrie, RemoteExecutable

Constant Summary collapse

MULTICAST_ADDR =
'224.5.5.55'.freeze
BIND_ADDR =
'0.0.0.0'.freeze
PORT =
'5555'.freeze

Constants included from RemoteExecutable

RemoteExecutable::EXECUTABLE_OBJECT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CanHeartbeat

#ping, #pong

Methods included from HasBlockchain

#blocks, #chain, #create_block, #mine

Methods included from HasTrie

#read, #reset, #store_get, #store_set, #trie, #write

Methods included from RemoteExecutable

#execute, #execute_on, #pickup_object

Constructor Details

#initializePeer

Returns a new instance of Peer.



21
22
23
24
25
26
27
# File 'lib/dcha/peer.rb', line 21

def initialize
  @hostname = Socket.gethostname
  @ipaddr = Addrinfo.getaddrinfo(hostname, nil, :INET).first
  @peers = []
  @thread = nil
  @packets = PacketManager.new
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



19
20
21
# File 'lib/dcha/peer.rb', line 19

def hostname
  @hostname
end

#ipaddrObject (readonly)

Returns the value of attribute ipaddr.



19
20
21
# File 'lib/dcha/peer.rb', line 19

def ipaddr
  @ipaddr
end

#peersObject (readonly)

Returns the value of attribute peers.



19
20
21
# File 'lib/dcha/peer.rb', line 19

def peers
  @peers
end

Instance Method Details

#joinObject



29
30
31
32
33
# File 'lib/dcha/peer.rb', line 29

def join
  return if listening?
  listen
  ping
end

#listening?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dcha/peer.rb', line 35

def listening?
  @listening == true
end

#transmit(data) ⇒ Object



39
40
41
# File 'lib/dcha/peer.rb', line 39

def transmit(data)
  transmit_to MULTICAST_ADDR, data
end

#transmit_to(address, data) ⇒ Object



43
44
45
46
47
# File 'lib/dcha/peer.rb', line 43

def transmit_to(address, data)
  Chunk.split(data).each do |bytes|
    socket.send(bytes.pack('C*'), 0, address, PORT)
  end
end