Module: HrrRbSftp::Protocol::Common::Packetable

Instance Attribute Summary

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Instance Method Details

#decode(payload) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/hrr_rb_sftp/protocol/common/packetable.rb', line 27

def decode payload
  payload_io = StringIO.new payload
  format = common_format
  decoded_packet = decode_recursively(payload_io).inject(Hash.new){ |h, (k, v)| h.update({k => v}) }
  log_debug { 'decoded packet: ' + decoded_packet.inspect }
  decoded_packet
end

#encode(packet) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hrr_rb_sftp/protocol/common/packetable.rb', line 13

def encode packet
  log_debug { 'encoding packet: ' + packet.inspect }
  format = common_format + conditional_format(packet)
  format.map{ |data_type, field_name|
    begin
      field_value = packet[field_name]
      data_type.encode field_value
    rescue => e
      log_debug { "'field_name', 'field_value': #{field_name.inspect}, #{field_value.inspect}" }
      raise e
    end
  }.join
end

#initialize(handles, logger: nil) ⇒ Object



7
8
9
10
11
# File 'lib/hrr_rb_sftp/protocol/common/packetable.rb', line 7

def initialize handles, logger: nil
  self.logger = logger

  @handles = handles
end