Class: Pokan::RequestHandler

Inherits:
EM::Connection
  • Object
show all
Includes:
CollectivePeerOperations
Defined in:
lib/pokan/request_handler.rb

Overview

Gossiper module is the implementation of the push-pull-gossip protocol.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CollectivePeerOperations

#merge, #newer, #older, #retrieve

Class Attribute Details

.addressObject

Returns the value of attribute address.



12
13
14
# File 'lib/pokan/request_handler.rb', line 12

def address
  @address
end

.portObject

Returns the value of attribute port.



12
13
14
# File 'lib/pokan/request_handler.rb', line 12

def port
  @port
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



25
26
27
# File 'lib/pokan/request_handler.rb', line 25

def address
  @address
end

#portObject

Returns the value of attribute port.



25
26
27
# File 'lib/pokan/request_handler.rb', line 25

def port
  @port
end

Class Method Details

.new(sig, *args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/pokan/request_handler.rb', line 16

def new(sig, *args)
  rh = old_new(sig, *args)
  rh.address = address
  rh.port    = port

  rh
end

.old_newObject



14
# File 'lib/pokan/request_handler.rb', line 14

alias_method :old_new, :new

Instance Method Details

#pull_message(keys) ⇒ Object

Get a push message containing all status and keys based on the given keys following the structure: Given structure: {key: timestamp, … …}



74
75
76
77
78
79
80
81
82
# File 'lib/pokan/request_handler.rb', line 74

def pull_message(keys)
  { action: 'pull',
    data: {
      newer: newer(keys),
      older: older(keys)
    },
    origin:"#{address}:#{port}" 
  }.to_json
end

#push_message(keys) ⇒ Object

Get a pull message containing all status and keys based on the given keys following the structure: Given structure: [:key1, :key2, …]



67
68
69
# File 'lib/pokan/request_handler.rb', line 67

def push_message(keys)
  { action: 'push', data: retrieve(keys), origin:"#{address}:#{port}" }.to_json
end

#receive_data(json_data) ⇒ Object

Receives the gossip message and returns the apropriate message

  • digest message -> pull message

  • pull message -> push message and newer keys stored

  • push message -> newer keys stored

  • hello message -> peer created locally

  • goodbye message -> peer killed locally



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pokan/request_handler.rb', line 34

def receive_data(json_data)
  message = JSON.parse(json_data)
  data = message['data']

  case message['action']
  when 'digest'
    pull = pull_message(data)
    send_datagram(pull, *message['origin'].split(':'))

    pull
  when 'pull'
    response = push_message(data['older'])
    merge(data['newer'])
    send_datagram(response, *message['origin'].split(':'))

    response
  when 'push'
    merge(data)
  when 'hello'
    peer = Peer.new
    peer.id = message['origin']
    # peer.store(:role, message['role'])
    peer.save
  when 'goodbye'
    peer = Query.new(Peer).where(id: message['origin'])[0]
    peer.store(:status, 'dead', message['timestamp'])
    peer.save
  end
end