Class: Serialization::Encrypted::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rswim/serialization/encrypted/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/encrypted/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
25
26
27
28
29
30
31
# File 'lib/rswim/serialization/encrypted/deserializer.rb', line 11

def deserialize(sender_host, wire_message)
  outer = JSON.parse(wire_message)
  cipher_text, salt = outer.values_at('message', 'salt').map { |s| Base64.decode64(s) }
  inner = JSON.parse(Encryption.decrypt(cipher_text, salt), symbolize_names: true)

  from = @directory.id(sender_host)
  payload = {}
  payload[:target_id] = @directory.id(inner[:target]) unless inner[:target].nil?
  payload[:updates] = inner[:updates].to_a.map do |u|
    UpdateEntry.new(
      @directory.id(u[:host]),
      u[:status].to_sym,
      u[:incarnation_number].to_i,
      u[:custom_state]
    )
  end
  Message.new(@my_id, from, inner[:type].to_sym, payload)
rescue StandardError => e
  logger.debug("Failed to parse wire message")
  nil
end