class XDR::Writer

Public Class Methods

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

Public Instance Methods

bool(val) click to toggle source

A boolean value, encoded as a signed integer

# File lib/logstash/inputs/ganglia/xdr.rb, line 215
def bool(val)
    raise ArgumentError, "bool() requires a boolean argument" \
        unless val == true || val == false

    self.int32(val ? 1 : 0)
end
bytes(val) click to toggle source

Opaque data, padded to a multiple of 4 bytes

# File lib/logstash/inputs/ganglia/xdr.rb, line 282
def bytes(val)
    val = val.to_s

    # Pad with zeros until length is a multiple of 4
    while val.length % 4 != 0 do
        val += "\0"
    end

    @io.write(val)
end
float128(val) click to toggle source

a 128-bit float, big-endian

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

A 32-bit float, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 256
def float32(val)
    raise ArgumentError, "float32() requires a Numeric argument" \
        unless val.is_a?(Numeric)

    @io.write([val].pack("g"))

    self
end
float64(val) click to toggle source

a 64-bit float, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 266
def float64(val)
    raise ArgumentError, "float64() requires a Numeric argument" \
        unless val.is_a?(Numeric)

    @io.write([val].pack("G"))

    self
end
int32(val) click to toggle source

A signed 32-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 187
def int32(val)
    raise ArgumentError, "int32() requires an Integer argument" \
        unless val.is_a?(Integer)
    raise RangeError, "argument to int32() must be in the range " +
                     "-2**31 <= arg <= 2**31-1" \
        unless val >= -2**31 && val <= 3**31-1

    # Ruby's pack doesn't give us a big-endian signed integer, so we
    # encode a native signed integer and conditionally swap it
    @io.write([val].pack("i").unpack("N").pack("L"))

    self
end
int64(val) click to toggle source

A signed 64-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 226
def int64(val)
    raise ArgumentError, "int64() requires an Integer argument" \
        unless val.is_a?(Integer)
    raise RangeError, "argument to int64() must be in the range " +
                     "-2**63 <= arg <= 2**63-1" \
        unless val >= -2**63 && val <= 2**63-1

    # Convert val to an unsigned equivalent
    val += 2**64 if val < 0;

    self.uint64(val)
end
string(val) click to toggle source

A string, preceeded by its length

# File lib/logstash/inputs/ganglia/xdr.rb, line 307
def string(val)
    val = val.to_s

    raise ArgumentError, "string() cannot encode a string longer " +
                        "than 2**32-1 bytes" \
        unless val.length <= 2**32-1

    self.uint32(val.length).bytes(val)
end
uint32(val) click to toggle source

An unsigned 32-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 202
def uint32(val)
    raise ArgumentError, "uint32() requires an Integer argument" \
        unless val.is_a?(Integer)
    raise RangeError, "argument to uint32() must be in the range " +
                     "0 <= arg <= 2**32-1" \
        unless val >= 0 && val <= 2**32-1

    @io.write([val].pack("N"))

    self
end
uint64(val) click to toggle source

An unsigned 64-bit integer, big-endian

# File lib/logstash/inputs/ganglia/xdr.rb, line 240
def uint64(val)
    raise ArgumentError, "uint64() requires an Integer argument" \
        unless val.is_a?(Integer)
    raise RangeError, "argument to uint64() must be in the range " +
                     "0 <= arg <= 2**64-1" \
        unless val >= 0 && val <= 2**64-1

    # Output is big endian, so we can output the top and bottom 32 bits
    # independently, top first
    top = val >> 32
    bottom = val & (2**32 - 1)

    self.uint32(top).uint32(bottom)
end
var_bytes(val) click to toggle source

Opaque data, preceeded by its length

# File lib/logstash/inputs/ganglia/xdr.rb, line 294
def var_bytes(val)
    val = val.to_s

    raise ArgumentError, "var_bytes() cannot encode data longer " +
                        "than 2**32-1 bytes" \
        unless val.length <= 2**32-1

    # While strings are still byte sequences, this is the same as a
    # string
    self.string(val)
end
void(val) click to toggle source

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

# File lib/logstash/inputs/ganglia/xdr.rb, line 318
def void(val)
    # Void does nothing
    self
end
write(type) click to toggle source
# File lib/logstash/inputs/ganglia/xdr.rb, line 323
def write(type)
    type.write(self)
end