class Pokan::RequestHandler

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

Attributes

address[RW]
port[RW]
address[RW]
port[RW]

Public Class Methods

new(sig, *args) click to toggle source
# 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
Also aliased as: old_new
old_new(sig, *args)
Alias for: new

Public Instance Methods

pull_message(keys) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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