Sha256: 36174c72e9dca03172caae91755b4e23e26f5a1442cb670dbbdc0560a9800c74

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'strscan'

class PacketViaDMEM
  PACKET = /^(Received|Sent) \d+ byte parcel:.*\n/
  HEADER_SIZE = {
    :received => 6,
    :sent     => 33,  # FIXME: it's variable, we're dumb
  }
  class Error < StandardError; end

  def initialize opts={}
    @received = opts.delete :received
    @sent     = opts.delete :sent
    @received ||= HEADER_SIZE[:received]
    @sc = StringScanner.new ''
  end

  def parse str
    pkts = []
    @sc.string = str
    while @sc.scan_until PACKET
      match = @sc.matched.split(/\s+/)
      type = match.first.downcase.to_sym
      next if type == :received and (not @received or @received < 1)
      next if type == :sent     and (not @sent     or @sent < 1)
      @sc.scan_until(/\n/) if type == :received
      type = type == :received ? @received : @sent
      pkt = ''
      while @sc.match?(/^0x/)
        pkt << @sc.scan_until(/\n/).strip
      end
      pkt = parse_packet(pkt, type)
      pkts << pkt if pkt
    end
    pkts
  end

  private

  def parse_packet pkt, pop
    pkt = pkt.gsub(/0x/, '')
    pkt = pkt.gsub(/\s+/, '')
    pkt = pkt.scan(/../)
    '000000 ' + pkt[pop..-1].join(' ')
  rescue
    nil
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
packet_via_dmem-0.0.0 lib/packet_via_dmem.rb