Sha256: cb5c3a11f588286499faf13a0bf33d686f423a6c63c11d2770dfa85c69a9351f

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'strscan'

class PacketViaDMEM
  PACKET = /^(Received|Sent) \d+ byte parcel:.*\n/
  FAKE = {
    :dmac  => %w( 22 22 22 22 22 22 ),
    :smac  => %w( 66 66 66 66 66 66 ),
    :etype_mpls  => %w( 88 47 ),
    :etype_ipv4  => %w( 08 00 ),
    :ipv4        => %w( 45 ),
  }
  HEADER_SIZE = {
    :received => 6,
    :sent     => 13,
  }
  class Error < StandardError; end

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

  def parse str
    packets = Packets.new @debug
    @sc.string = str
    while @sc.scan_until PACKET
      match = @sc.matched.split(/\s+/)
      type = match.first.downcase.to_sym
      @sc.scan_until(/\n/) if type == :received
      pkt = ''
      while @sc.match?(/^0x/)
        pkt << @sc.scan_until(/\n/).strip
      end
      pkt = parse_packet pkt
      packets.add pkt, type
    end
    packets
  end

  private

  def parse_packet pkt
    pkt = pkt.gsub(/0x/, '')
    pkt = pkt.gsub(/\s+/, '')
    pkt = pkt.scan(/../)
    pkt
  end
end

require_relative 'packet_via_dmem/packets'

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
packet_via_dmem-0.0.12 lib/packet_via_dmem.rb
packet_via_dmem-0.0.11 lib/packet_via_dmem.rb
packet_via_dmem-0.0.10 lib/packet_via_dmem.rb
packet_via_dmem-0.0.8 lib/packet_via_dmem.rb