class RSwim::Integration::Deserializer

Public Class Methods

new(directory, my_id) click to toggle source
# File lib/rswim/integration/deserializer.rb, line 6
def initialize(directory, my_id)
  @directory = directory
  @my_id = my_id
end

Public Instance Methods

deserialize(sender_host, wire_message) click to toggle source
# File lib/rswim/integration/deserializer.rb, line 11
def deserialize(sender_host, wire_message)
  l, *ls = wire_message.split("\n")
  # First line is
  # type [target_id]
  ary = l.strip.split(' ')
  type = ary[0].gsub(/-/, '_').to_sym
  payload = type == :ping_req ? { target_id: @directory.id(ary[1]) } : {}
  payload[:updates] = parse_updates(ls)
  from = @directory.id(sender_host)
  RSwim::Message.new(@my_id, from, type, payload)
rescue StandardError => e
  logger.debug("Failed to parse line `#{l}`: #{e}")
  nil
end

Protected Instance Methods

logger() click to toggle source
# File lib/rswim/integration/deserializer.rb, line 28
def logger
  @_logger ||= begin
    RSwim::Logger.new(self.class, STDERR)
  end
end

Private Instance Methods

parse_updates(lines) click to toggle source
# File lib/rswim/integration/deserializer.rb, line 36
def parse_updates(lines)
  lines.map do |l|
    begin
      # host status incarnation_number
      host, status, incarnation_number = l.strip.split(' ')
      id = @directory.id(host)
      UpdateEntry.new(id, status.to_sym, incarnation_number.to_i)
    rescue StandardError => e
      logger.debug("Failed to parse line `#{l}`: #{e}")
      nil
    end
  end.tap(&:compact!)
end