class SpheroPwn::ChannelRecorder

Records all the bytes going to a channel.

Public Class Methods

new(channel, recording_path) click to toggle source

@param {Channel} the channel being recorded @param {String} recording_path the file where the recording will be saved

# File lib/sphero_pwn/channel_recorder.rb, line 5
def initialize(channel, recording_path)
  @channel = channel
  @file = File.open recording_path, 'wt'

  @is_receiving = false
end

Public Instance Methods

close() click to toggle source

@see {Channel#close}

# File lib/sphero_pwn/channel_recorder.rb, line 44
def close
  if @is_receiving
    @file.write "\n"
  end
  @file.close
  @channel.close
  self
end
recv_bytes(count) click to toggle source

@see {Channel#recv_bytes}

# File lib/sphero_pwn/channel_recorder.rb, line 13
def recv_bytes(count)
  bytes = @channel.recv_bytes count
  return bytes if bytes.nil?

  unless @is_receiving
    @file.write '<'
    @is_receiving = true
  end
  log_bytes bytes
  @file.flush

  bytes
end
send_bytes(bytes) click to toggle source

@see {Channel#send_bytes}

# File lib/sphero_pwn/channel_recorder.rb, line 28
def send_bytes(bytes)
  if @is_receiving
    @file.write "\n"
    @is_receiving = false
  end

  @file.write '>'
  log_bytes bytes
  @file.write "\n"
  @file.flush

  @channel.send_bytes bytes
  self
end

Private Instance Methods

log_bytes(bytes) click to toggle source

@param {String} bytes the bytes to be written to the output file

# File lib/sphero_pwn/channel_recorder.rb, line 54
def log_bytes(bytes)
  unless bytes.empty?
    @file.write bytes.unpack('C*').map { |byte| ' %02X' % byte }.join('')
  end
end