Module: RBus::MarshalMixin

Defined in:
lib/rbus/message/marshal.rb

Overview

Mix in an array of values to gain the dbus_marshal method Generally, this is used on the arguments of a message, and an array of header values. See Message::Writer#send_message.

Instance Method Summary collapse

Instance Method Details

#dbus_marshal(signature) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rbus/message/marshal.rb', line 31

def dbus_marshal(signature)

  tokens = StringIO.new(signature)        
  ms = StringIO.new('')
  
  skip = lambda {|alignment| ms.write("\0" * (-ms.pos & alignment - 1))}
  
  parser = lambda {|element,sig|
    case sig
      when 'b'
        skip.call(4)
        if element && element != 0 && element != false
          ms.write([1].pack('I'))
        else
          ms.write([0].pack('I'))
        end
      when 'y'
        ms.write([element.to_i].pack('C'))
      when 'n'
        skip.call(2)
        ms.write([element.to_i].pack('s'))
      when 'q'
        skip.call(2)
        ms.write([element.to_i].pack('S'))
      when 'u'
        skip.call(4)
        ms.write([element.to_i].pack('I'))
      when 'i'
        skip.call(4)
        ms.write([element.to_i].pack('i'))
      when 'd'
        skip.call(8)
        ms.write([element.to_f].pack('D'))
      when 's','o'
        skip.call(4)
        str = element.to_s
        ms.write([str.length,str].pack('Ia*x'))
      when 'g'
        str = element.to_s
        ms.write([str.length,str].pack('Ca*x'))
      when 'v'
        parser.call(element.object_signature, 'g')
        parser.call(element.object, element.object_signature)
      when 'a'
        skip.call(4)
        next_token = tokens.read(1)
        t_pos = tokens.pos
        
        # Postion of array length int
        a_len_pos = ms.pos
        # ...which is here
        ms.write("\0\0\0\0")
        
        # Pad already here, to find correct end of padding...
        case next_token
          when 's','o','b','i','u'
            skip.call(4)
          when '{','(','a','d'
            skip.call(8)
          when 'q','n'
            skip.call(2)
        end
        pad_pos = ms.pos
                      
        if next_token == '{'
          skip.call(8)
          element.each do |key,value|
            tokens.pos = t_pos
            parser.call(key,tokens.read(1))
            parser.call(value,tokens.read(1))
          end
          while next_token != '}'
            next_token = tokens.read(1)
          end
        else
              
          element.each do |value|
            tokens.pos = t_pos
            parser.call(value,next_token)
          end
        end
                                  
        # Fill in array length              
        m_new_pos = ms.pos
        a_length = ms.pos - pad_pos
        ms.pos = a_len_pos
        parser.call(a_length, 'u')
        ms.pos = m_new_pos
        
      when '('
        skip.call(8)
        ret_val = ''
        element.each {|value|
          next_token = tokens.read(1)
          break if next_token == ')'
          parser.call(value, next_token)
        }
      else
        raise NotImplementedError, "can't marshal #{sig}"
    end
  }
  each do |el|
    parser.call(el,tokens.read(1))
  end

  ms.string
end