Class: Serialization::Simple::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rswim/serialization/simple/deserializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(directory, my_id) ⇒ Deserializer

Returns a new instance of Deserializer.



6
7
8
9
# File 'lib/rswim/serialization/simple/deserializer.rb', line 6

def initialize(directory, my_id)
  @directory = directory
  @my_id = my_id
end

Instance Method Details

#deserialize(sender_host, wire_message) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rswim/serialization/simple/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