Class: XDR::Writer

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

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Writer

Returns a new instance of Writer.



182
183
184
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 182

def initialize(io)
    @io = io
end

Instance Method Details

#bool(val) ⇒ Object

A boolean value, encoded as a signed integer

Raises:

  • (ArgumentError)


215
216
217
218
219
220
# 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) ⇒ Object

Opaque data, padded to a multiple of 4 bytes



282
283
284
285
286
287
288
289
290
291
# 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) ⇒ Object

a 128-bit float, big-endian

Raises:

  • (NotImplementedError)


276
277
278
279
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 276

def float128(val)
    # Maybe some day
    raise NotImplementedError
end

#float32(val) ⇒ Object

A 32-bit float, big-endian

Raises:

  • (ArgumentError)


256
257
258
259
260
261
262
263
# 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) ⇒ Object

a 64-bit float, big-endian

Raises:

  • (ArgumentError)


266
267
268
269
270
271
272
273
# 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) ⇒ Object

A signed 32-bit integer, big-endian

Raises:

  • (ArgumentError)


187
188
189
190
191
192
193
194
195
196
197
198
199
# 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) ⇒ Object

A signed 64-bit integer, big-endian

Raises:

  • (ArgumentError)


226
227
228
229
230
231
232
233
234
235
236
237
# 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) ⇒ Object

A string, preceeded by its length

Raises:

  • (ArgumentError)


307
308
309
310
311
312
313
314
315
# 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) ⇒ Object

An unsigned 32-bit integer, big-endian

Raises:

  • (ArgumentError)


202
203
204
205
206
207
208
209
210
211
212
# 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) ⇒ Object

An unsigned 64-bit integer, big-endian

Raises:

  • (ArgumentError)


240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 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) ⇒ Object

Opaque data, preceeded by its length

Raises:

  • (ArgumentError)


294
295
296
297
298
299
300
301
302
303
304
# 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) ⇒ Object

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



318
319
320
321
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 318

def void(val)
    # Void does nothing
    self
end

#write(type) ⇒ Object



323
324
325
# File 'lib/logstash/inputs/ganglia/xdr.rb', line 323

def write(type)
    type.write(self)
end