Sha256: fe3e2d0bd617a261514dbf6bb8f4fedeec01632616d0017b47dee17fc3e65aaf

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

class EventMachine::SFlow::Datagram
  attr_reader :version, :agent, :sub_agent_id, :datagram_sequence, :uptime, :samples
  
  def initialize data
    data.extend EM::SFlow::BinaryString
    
    @samples = []

    @version, ip_version = data.unpack("NN")
    
    data.advance(8)

    if ip_version == 1
      @agent = IPAddr.new(data.unpack("N").first, Socket::AF_INET)
      data.advance(4)
    else
      ip_elements = data.unpack("NNNN")
      @agent = IPAddr.new((ip_elements[0] << 96) + (ip_elements[1] << 64) + (ip_elements[2] << 32) + ip_elements[3], Socket::AF_INET6)
      data.advance(16)
    end
    
    @sub_agent_id, @datagram_sequence, @uptime, sample_count = data.unpack("NNNN")
    
    data.advance(16)

    sample_count.times do
      enterprise_format, length = data.unpack("NN")
      enterprise = enterprise_format >> 12
      format = enterprise_format & (2 ** 12 - 1)
      
      data.advance(8)

      sample_data = data.advance(length)
      
      if enterprise == 0 && format == 1
        @samples << EM::SFlow::FlowSample.new(sample_data)
      elsif enterprise == 0 && format == 2  
        @samples << EM::SFlow::CounterSample.new(sample_data)
      elsif enterprise == 0 && format == 3
        @samples << EM::SFlow::ExpandedFlowSample.new(sample_data)
      elsif enterprise == 0 && format == 4
        # @samples << EM::SFlow::ExpandedCounterSample.new(sample_data)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
em-sflow-1.0.3 lib/em-sflow/packet/datagram.rb
em-sflow-1.0.2 lib/em-sflow/packet/datagram.rb