Class: DBus::PacketMarshaller

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus/marshall.rb

Overview

D-Bus packet marshaller class

Class that handles the conversion (unmarshalling) of Ruby objects to (binary) payload data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset = 0) ⇒ PacketMarshaller

Create a new marshaller, setting the current packet to the empty packet.



252
253
254
255
# File 'lib/dbus/marshall.rb', line 252

def initialize(offset = 0)
  @packet = ""
  @offset = offset          # for correct alignment of nested marshallers
end

Instance Attribute Details

#packetObject (readonly)

The current or result packet. FIXME: allow access only when marshalling is finished



248
249
250
# File 'lib/dbus/marshall.rb', line 248

def packet
  @packet
end

Instance Method Details

#align(a) ⇒ Object

Align the buffer with NULL (0) bytes on a byte length of a.



269
270
271
# File 'lib/dbus/marshall.rb', line 269

def align(a)
  @packet = @packet.ljust(num_align(@offset + @packet.length, a) - @offset, 0.chr)
end

#append(type, val) ⇒ Object

Append a value val to the packet based on its type.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/dbus/marshall.rb', line 311

def append(type, val)
  type = type.chr if type.kind_of?(Fixnum)
  type = Type::Parser.new(type).parse[0] if type.kind_of?(String)
  case type.sigtype
  when Type::BYTE
    @packet += val.chr
  when Type::UINT32
    align(4)
    @packet += [val].pack("L")
  when Type::UINT64
	align(8)
	@packet += [val].pack("Q")
  when Type::INT64
	align(8)
	@packet += [val].pack("q")
  when Type::INT32
    align(4)
    @packet += [val].pack("l")
  when Type::UINT16
    align(2)
    @packet += [val].pack("S")
  when Type::INT16
    align(2)
    @packet += [val].pack("s")
  when Type::DOUBLE
    align(8)
	@packet += [val].pack("d")
  when Type::BOOLEAN
    align(4)
    if val
      @packet += [1].pack("L")
    else
      @packet += [0].pack("L")
    end
  when Type::OBJECT_PATH
    append_string(val)
  when Type::STRING
    append_string(val)
  when Type::SIGNATURE
    append_signature(val)
  when Type::VARIANT
    if not val.kind_of?(Array)
      raise TypeException
    end
    vartype, vardata = val
    vartype = Type::Parser.new(vartype).parse[0] if vartype.kind_of?(String)
    append_signature(vartype.to_s)
    align(vartype.alignment)
    sub = PacketMarshaller.new(@offset + @packet.length)
    sub.append(vartype, vardata)
    @packet += sub.packet
  when Type::ARRAY
    if val.kind_of?(Hash)
      raise TypeException if type.child.sigtype != Type::DICT_ENTRY
      # Damn ruby rocks here
      val = val.to_a
    end
    if not val.kind_of?(Array)
      raise TypeException
    end
    array(type.child) do
      val.each do |elem|
        append(type.child, elem)
      end
    end
  when Type::STRUCT, Type::DICT_ENTRY
    raise TypeException if not val.kind_of?(Array)
    if type.sigtype == Type::DICT_ENTRY and val.size != 2
      raise TypeException
    end
    struct do
      idx = 0
      while val[idx] != nil
        type.members.each do |subtype|
          raise TypeException if val[idx] == nil
          append(subtype, val[idx])
          idx += 1
        end
      end
    end
  else
    raise NotImplementedError
  end
end

#append_signature(str) ⇒ Object

Append the the signature signature itself to the packet.



280
281
282
# File 'lib/dbus/marshall.rb', line 280

def append_signature(str)
  @packet += str.length.chr + str + "\0"
end

#append_simple_string(s) ⇒ Object

Append a string of bytes without type.



306
307
308
# File 'lib/dbus/marshall.rb', line 306

def append_simple_string(s)
  @packet += s + "\0"
end

#append_string(str) ⇒ Object

Append the the string str itself to the packet.



274
275
276
277
# File 'lib/dbus/marshall.rb', line 274

def append_string(str)
  align(4)
  @packet += [str.length].pack("L") + str + "\0"
end

#array(type) ⇒ Object

Append the array type type to the packet and allow for appending the child elements.



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/dbus/marshall.rb', line 286

def array(type)
  # Thanks to Peter Rullmann for this line
  align(4)
  sizeidx = @packet.size
  @packet += "ABCD"
  align(type.alignment)
  contentidx = @packet.size
  yield
  sz = @packet.size - contentidx
  raise InvalidPacketException if sz > 67108864
  @packet[sizeidx...sizeidx + 4] = [sz].pack("L")
end

#num_align(n, a) ⇒ Object

Round n up to the specified power of two, a



258
259
260
261
262
263
264
265
266
# File 'lib/dbus/marshall.rb', line 258

def num_align(n, a)
  case a
  when 1, 2, 4, 8
    bits = a - 1
    n + bits & ~bits
  else
    raise "Unsupported alignment"
  end
end

#structObject

Align and allow for appending struct fields.



300
301
302
303
# File 'lib/dbus/marshall.rb', line 300

def struct
  align(8)
  yield
end