Class: Pokan::RequestHandler
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Pokan::RequestHandler
- 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
-
.address ⇒ Object
Returns the value of attribute address.
-
.port ⇒ Object
Returns the value of attribute port.
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#port ⇒ Object
Returns the value of attribute port.
Class Method Summary collapse
Instance Method Summary collapse
-
#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, … …}.
-
#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, …].
-
#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.
Methods included from CollectivePeerOperations
#merge, #newer, #older, #retrieve
Class Attribute Details
.address ⇒ Object
Returns the value of attribute address.
12 13 14 |
# File 'lib/pokan/request_handler.rb', line 12 def address @address end |
.port ⇒ Object
Returns the value of attribute port.
12 13 14 |
# File 'lib/pokan/request_handler.rb', line 12 def port @port end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
25 26 27 |
# File 'lib/pokan/request_handler.rb', line 25 def address @address end |
#port ⇒ Object
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_new ⇒ Object
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 (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 (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) = JSON.parse(json_data) data = ['data'] case ['action'] when 'digest' pull = (data) send_datagram(pull, *['origin'].split(':')) pull when 'pull' response = (data['older']) merge(data['newer']) send_datagram(response, *['origin'].split(':')) response when 'push' merge(data) when 'hello' peer = Peer.new peer.id = ['origin'] # peer.store(:role, message['role']) peer.save when 'goodbye' peer = Query.new(Peer).where(id: ['origin'])[0] peer.store(:status, 'dead', ['timestamp']) peer.save end end |