Ejemplo n.º 1
0
    def _marshal(self, newSerial=True):
        """
        Encodes the message into binary format. The resulting binary message is
        stored in C{self.rawMessage}
        """
        flags = 0

        if not self.expectReply:
            flags |= 0x1

        if not self.autoStart:
            flags |= 0x2
        
        self.headers = list()
        
        for attr_name, code, is_required in self._headerAttrs:
            hval = getattr(self, attr_name, None)
            
            if hval is not None:
                if attr_name == 'path':
                    hval = marshal.ObjectPath(hval)
                elif attr_name == 'signature':
                    hval = marshal.Signature(hval)
                    
                self.headers.append( [code, hval] )

        if self.signature:
            binBody = ''.join( marshal.marshal( self.signature, self.body )[1] )
        else:
            binBody = ''

        self.bodyLength = len(binBody)

        if newSerial:
            self.serial = DBusMessage._nextSerial

            DBusMessage._nextSerial += 1
        
        binHeader = ''.join(marshal.marshal(_headerFormat,
                                            [self.endian,
                                             self._messageType,
                                             flags,
                                             self._protocolVersion,
                                             self.bodyLength,
                                             self.serial,
                                             self.headers],
                                            lendian = self.endian == ord('l') )[1])
        
        headerPadding = marshal.pad['header']( len(binHeader) )

        self.rawHeader  = binHeader
        self.rawPadding = headerPadding
        self.rawBody    = binBody
        
        self.rawMessage = ''.join( [binHeader, headerPadding, binBody] )

        if len(self.rawMessage) > self._maxMsgLen:
            raise error.MarshallingError('Marshalled message exceeds maximum message size of %d' %
                                         (self._maxMsgLen,))
Ejemplo n.º 2
0
    def _marshal(self, newSerial=True):
        """
        Encodes the message into binary format. The resulting binary message is
        stored in C{self.rawMessage}
        """
        flags = 0

        if not self.expectReply:
            flags |= 0x1

        if not self.autoStart:
            flags |= 0x2
        
        self.headers = list()
        
        for attr_name, code, is_required in self._headerAttrs:
            hval = getattr(self, attr_name, None)
            
            if hval is not None:
                if attr_name == 'path':
                    hval = marshal.ObjectPath(hval)
                elif attr_name == 'signature':
                    hval = marshal.Signature(hval)
                    
                self.headers.append( [code, hval] )

        if self.signature:
            binBody = b''.join( marshal.marshal( self.signature, self.body )[1] )
        else:
            binBody = b''

        self.bodyLength = len(binBody)

        if newSerial:
            self.serial = DBusMessage._nextSerial

            DBusMessage._nextSerial += 1
        
        binHeader = b''.join(marshal.marshal(_headerFormat,
                                            [self.endian,
                                             self._messageType,
                                             flags,
                                             self._protocolVersion,
                                             self.bodyLength,
                                             self.serial,
                                             self.headers],
                                            lendian = self.endian == ord('l') )[1])
        
        headerPadding = marshal.pad['header']( len(binHeader) )

        self.rawHeader  = binHeader
        self.rawPadding = headerPadding
        self.rawBody    = binBody
        
        self.rawMessage = b''.join( [binHeader, headerPadding, binBody] )

        if len(self.rawMessage) > self._maxMsgLen:
            raise error.MarshallingError('Marshalled message exceeds maximum message size of %d' %
                                         (self._maxMsgLen,))
Ejemplo n.º 3
0
 def check(self, sig, var_list, expected_encoding, little_endian=True):
     if not isinstance(var_list, list):
         var_list = [var_list]
     nbytes, chunks = m.marshal( sig, var_list, 0, little_endian  )
     bin_str = b''.join(chunks)
     self.assertEquals( nbytes, len(expected_encoding), "Byte length mismatch. Expected %d. Got %d" % (len(expected_encoding), nbytes) )
     self.assertEquals( bin_str, expected_encoding, "Binary encoding differs from expected value" )
Ejemplo n.º 4
0
 def check(self, sig, var_list, expected_encoding, little_endian=True):
     if not isinstance(var_list, list):
         var_list = [var_list]
     nbytes, chunks = m.marshal(sig, var_list, 0, little_endian)
     bin_str = ''.join(chunks)
     self.assertEquals(
         nbytes, len(expected_encoding),
         "Byte length mismatch. Expected %d. Got %d" %
         (len(expected_encoding), nbytes))
     self.assertEquals(bin_str, expected_encoding,
                       "Binary encoding differs from expected value")
Ejemplo n.º 5
0
    def _marshal(self, newSerial=True, oobFDs=None):
        """
        Encodes the message into binary format. The resulting binary message is
        stored in C{self.rawMessage}
        """
        flags = 0

        if not self.expectReply:
            flags |= 0x1

        if not self.autoStart:
            flags |= 0x2

        # may be overriden below, depending on oobFDs
        _headerAttrs = self._headerAttrs

        # marshal body before headers to know if the 'unix_fd' header is needed
        if self.signature:
            binBody = b''.join(
                marshal.marshal(
                    self.signature,
                    self.body,
                    oobFDs=oobFDs
                )[1]
            )
            if oobFDs:
                # copy class based _headerAttrs to add a unix_fds header this
                # time
                _headerAttrs = list(self._headerAttrs)
                _headerAttrs.append(('unix_fds', 9, False))
                self.unix_fds = len(oobFDs)
        else:
            binBody = b''

        self.headers = []

        for attr_name, code, _ in _headerAttrs:
            hval = getattr(self, attr_name, None)

            if hval is not None:
                if attr_name == 'path':
                    hval = marshal.ObjectPath(hval)
                elif attr_name == 'signature':
                    hval = marshal.Signature(hval)
                elif attr_name == 'unix_fds':
                    hval = marshal.UInt32(hval)

                self.headers.append([code, hval])

        self.bodyLength = len(binBody)

        if newSerial:
            self.serial = DBusMessage._nextSerial

            DBusMessage._nextSerial += 1

        binHeader = b''.join(marshal.marshal(
            _headerFormat,
            [
                self.endian,
                self._messageType,
                flags,
                self._protocolVersion,
                self.bodyLength,
                self.serial,
                self.headers
            ],
            lendian=self.endian == ord('l')
        )[1])

        headerPadding = marshal.pad['header'](len(binHeader))

        self.rawHeader = binHeader
        self.rawPadding = headerPadding
        self.rawBody = binBody

        self.rawMessage = b''.join([binHeader, headerPadding, binBody])

        if len(self.rawMessage) > self._maxMsgLen:
            raise error.MarshallingError(
                'Marshalled message exceeds maximum message size of %d' %
                (self._maxMsgLen,),
            )
Ejemplo n.º 6
0
    def _marshal(self, newSerial=True, oobFDs=None):
        """
        Encodes the message into binary format. The resulting binary message is
        stored in C{self.rawMessage}
        """
        flags = 0

        if not self.expectReply:
            flags |= 0x1

        if not self.autoStart:
            flags |= 0x2

        # may be overriden below, depending on oobFDs
        _headerAttrs = self._headerAttrs

        # marshal body before headers to know if the 'unix_fd' header is needed
        if self.signature:
            binBody = b''.join(
                marshal.marshal(
                    self.signature,
                    self.body,
                    oobFDs=oobFDs
                )[1]
            )
            if oobFDs:
                # copy class based _headerAttrs to add a unix_fds header this
                # time
                _headerAttrs = list(self._headerAttrs)
                _headerAttrs.append(('unix_fds', 9, False))
                self.unix_fds = len(oobFDs)
        else:
            binBody = b''

        self.headers = []

        for attr_name, code, is_required in _headerAttrs:
            hval = getattr(self, attr_name, None)

            if hval is not None:
                if attr_name == 'path':
                    hval = marshal.ObjectPath(hval)
                elif attr_name == 'signature':
                    hval = marshal.Signature(hval)
                elif attr_name == 'unix_fds':
                    hval = marshal.UInt32(hval)

                self.headers.append([code, hval])

        self.bodyLength = len(binBody)

        if newSerial:
            self.serial = DBusMessage._nextSerial

            DBusMessage._nextSerial += 1

        binHeader = b''.join(marshal.marshal(
            _headerFormat,
            [
                self.endian,
                self._messageType,
                flags,
                self._protocolVersion,
                self.bodyLength,
                self.serial,
                self.headers
            ],
            lendian=self.endian == ord('l')
        )[1])

        headerPadding = marshal.pad['header'](len(binHeader))

        self.rawHeader = binHeader
        self.rawPadding = headerPadding
        self.rawBody = binBody

        self.rawMessage = b''.join([binHeader, headerPadding, binBody])

        if len(self.rawMessage) > self._maxMsgLen:
            raise error.MarshallingError(
                'Marshalled message exceeds maximum message size of %d' %
                (self._maxMsgLen,),
            )