Class: XDR::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/ganglia/xdr.rb

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Reader

Returns a new instance of Reader.



25
26
27
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 25

def initialize(io)
    @io = io
end

Instance Method Details

#_int16(typename) ⇒ Object



40
41
42
43
44
# 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) ⇒ Object



46
47
48
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 46

def _uint16(typename)
    _read_type(2, typename).unpack("n").first
end

#boolObject

A boolean value, encoded as a signed integer



63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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

#float128Object

a 128-bit float, big-endian

Raises:

  • (NotImplementedError)


100
101
102
103
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 100

def float128()
    # Maybe some day
    raise NotImplementedError
end

#float32Object

A 32-bit float, big-endian



90
91
92
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 90

def float32()
    _read_type(4, "float32").unpack("g").first
end

#float64Object

a 64-bit float, big-endian



95
96
97
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 95

def float64()
    _read_type(8, "float64").unpack("G").first
end

#int16Object



36
37
38
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 36

def int16()
    _int16("int16")
end

#int32Object

A signed 32-bit integer, big-endian



53
54
55
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 53

def int32()
    _int32("int32")
end

#int64Object

A signed 64-bit integer, big-endian



77
78
79
80
81
82
# 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) ⇒ Object



140
141
142
143
144
145
146
# 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

#stringObject

A string, preceeded by its length



130
131
132
133
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 130

def string()
    len = self.uint32()
    self.bytes(len)
end

#uint16Object

ADDED HERE -> need to return patch Short



32
33
34
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 32

def uint16()
    _uint16("uint16")
end

#uint32Object

An unsigned 32-bit integer, big-endian



58
59
60
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 58

def uint32()
    _uint32("uint32")
end

#uint64Object

An unsigned 64-bit integer, big-endian



85
86
87
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 85

def uint64()
    _uint64("uint64")
end

#var_bytesObject

Opaque data, preceeded by its length



124
125
126
127
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 124

def var_bytes()
    len = self.uint32()
    self.bytes(len)
end

#voidObject

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



136
137
138
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 136

def void()
    nil
end