class XDR::Reader

Public Class Methods

new(io) click to toggle source
# File lib/logstash/inputs/ganglia/xdr.rb, line 25
def initialize(io)
    @io = io
end

Public Instance Methods

_int16(typename) click to toggle source
# File lib/logstash/inputs/ganglia/xdr.rb, line 40
def _int16(typename)
    # Ruby's unpack doesn't give us a big-endian signed integer, so we
    # decode a native signed integer and conditionally swap it
    _read_type(4, typename).unpack("n").pack("L").unpack("l").first
end
_uint16(typename) click to toggle source
# File lib/logstash/inputs/ganglia/xdr.rb, line 46
def _uint16(typename)
    _read_type(2, typename).unpack("n").first
end
bool() click to toggle source

A boolean value, encoded as a signed integer

# File lib/logstash/inputs/ganglia/xdr.rb, line 63
def bool()
    val = _int32("bool")

    case val
    when 0
        false
    when 1
        true
    else
        raise ArgumentError, "Invalid value for bool: #{val}"
    end
end
bytes(n) click to toggle source

Opaque data of length n, padded to a multiple of 4 bytes

# File lib/logstash/inputs/ganglia/xdr.rb, line 106
def bytes(n)
    # Data length is n padded to a multiple of 4
    align = n % 4
    if align == 0 then
        len = n
    else
        len = n + (4-align)
    end

    bytes = _read_type(len, "opaque of length #{n}")

    # Remove padding if required
    (1..(4-align)).each { bytes.chop! } if align != 0

    bytes
end
float128() click to toggle source

a 128-bit float, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 100
def float128()
    # Maybe some day
    raise NotImplementedError
end
float32() click to toggle source

A 32-bit float, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 90
def float32()
    _read_type(4, "float32").unpack("g").first
end
float64() click to toggle source

a 64-bit float, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 95
def float64()
    _read_type(8, "float64").unpack("G").first
end
int16() click to toggle source
# File lib/logstash/inputs/ganglia/xdr.rb, line 36
def int16()
    _int16("int16")
end
int32() click to toggle source

A signed 32-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 53
def int32()
    _int32("int32")
end
int64() click to toggle source

A signed 64-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 77
def int64()
    # Read an unsigned value, then convert it to signed
    val = _uint64("int64")

    val >= 2**63 ? -(2**64 - val): val
end
read(type) click to toggle source
# File lib/logstash/inputs/ganglia/xdr.rb, line 140
def read(type)
    # For syntactic niceness, instantiate a new object of class 'type'
    # if type is a class
    type = type.new() if type.is_a?(Class)
    type.read(self)
    type
end
string() click to toggle source

A string, preceeded by its length

# File lib/logstash/inputs/ganglia/xdr.rb, line 130
def string()
    len = self.uint32()
    self.bytes(len)
end
uint16() click to toggle source

ADDED HERE -> need to return patch Short

# File lib/logstash/inputs/ganglia/xdr.rb, line 32
def uint16()
    _uint16("uint16")
end
uint32() click to toggle source

An unsigned 32-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 58
def uint32()
    _uint32("uint32")
end
uint64() click to toggle source

An unsigned 64-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 85
def uint64()
    _uint64("uint64")
end
var_bytes() click to toggle source

Opaque data, preceeded by its length

# File lib/logstash/inputs/ganglia/xdr.rb, line 124
def var_bytes()
    len = self.uint32()
    self.bytes(len)
end
void() click to toggle source

Void doesn't require a representation. Included only for completeness.

# File lib/logstash/inputs/ganglia/xdr.rb, line 136
def void()
    nil
end

Private Instance Methods

_int32(typename) click to toggle source

Read a signed int, but report typename if raising an error

# File lib/logstash/inputs/ganglia/xdr.rb, line 161
def _int32(typename)
    # Ruby's unpack doesn't give us a big-endian signed integer, so we
    # decode a native signed integer and conditionally swap it
    _read_type(4, typename).unpack("N").pack("L").unpack("l").first
end
_read_type(length, typename) click to toggle source

Read length bytes from the input. Return an error if we failed.

# File lib/logstash/inputs/ganglia/xdr.rb, line 151
def _read_type(length, typename)
    bytes = @io.read(length)

    raise EOFError, "Unexpected EOF reading #{typename}" \
        if bytes.nil? || bytes.length != length

    bytes
end
_uint32(typename) click to toggle source

Read an unsigned int, but report typename if raising an error

# File lib/logstash/inputs/ganglia/xdr.rb, line 168
def _uint32(typename)
    _read_type(4, typename).unpack("N").first
end
_uint64(typename) click to toggle source

Read a uint64, but report typename if raising an error

# File lib/logstash/inputs/ganglia/xdr.rb, line 173
def _uint64(typename)
    top = _uint32(typename)
    bottom = _uint32(typename)

    (top << 32) + bottom
end