Exemple #1
0
    def put_next(self, field_nbr, value):

        # getter has range check
        field_type = self._msg_spec.field_type_ndx(field_nbr)

        # puts through dispatch table -------------------------------
        if 0 <= field_type and field_type <= ftypes.F_BYTES32:
            # DEBUG
            print(
                "putNext: field type is %d (%s)" %
                (field_type, fstr.as_str(field_type)))
            sys.stdout.flush()
            # END
            T_PUT_FUNCS[field_type](self, value, field_nbr)
            # DEBUG
            if field_type < ftypes.L_STRING:
                print("putNext through dispatch table:\n"
                      "         field   %u\n"
                      "         fType   %u,  %s\n"
                      "         value   %d (0x%x)\n"
                      "         offset  %u" % (
                          field_nbr, field_type, fstr.as_str(field_type),
                          value, value, self._position))
            # END
            return
        else:
            print("unknown/unimplemented field type %s" % str(field_type))

        # -- NOW VESTIGIAL ------------------------------------------
        varint_ = None
Exemple #2
0
    def test_node_reg(self):
        c_types = CoreTypes()
        test_reg = NodeReg()

        # bootstrap() has loaded fieldTypes and coreTypes
        #       print "DEBUG: FieldTypes.MAX_NDX is %d" % FieldTypes.MAX_NDX        # is 17
        #       print "DEBUG: C.maxNdx is %d" % C.maxNdx        # is  5
        #       print "DEBUG: test_reg.nextRegID is %d test_reg.nextRegID
        self.assertEqual(FieldTypes.MAX_NDX + 1 + c_types.max_ndx + 1, test_reg.next_reg_id)

        # verify that all fieldTypes are defined in the registry, each
        # with the proper index (vBool through fBytes32 at FieldTypes.MAX_NDX)
        for i in range(FieldTypes.MAX_NDX + 1):
            name = test_reg[i].qual_name
            # DEBUG
            #           print '%2u %s' % (i, name)
            # END
            self.assertEqual(FieldStr.as_str(i), name)
            self.assertEqual(i, test_reg.name2reg_id(name))

        for i in range(FieldTypes.MAX_NDX + 1, FieldTypes.MAX_NDX + 1 + c_types.max_ndx + 1):
            name = test_reg[i].qual_name
            # DEBUG
            #           print '%2u %s' % (i, name)
            # END
            self.assertEqual(c_types.as_str(i - (FieldTypes.MAX_NDX + 1)), name)
            self.assertEqual(i, test_reg.name2reg_id(name))

        # F and C range from 0 to maxNdx
        self.assertEqual(FieldTypes.MAX_NDX + 1 + c_types.max_ndx + 1, len(test_reg))

        #       print "DEBUG: len(test_reg) is %u" % len(test_reg)
        #       print "DEBUG: nextRegID is %u"    % test_reg.nextRegID

        self.assertEqual(len(test_reg), test_reg.next_reg_id)
Exemple #3
0
 def field_type_name(self):
     if self._type >= 0 and self._type <= FieldTypes.MAX_NDX:
         return FieldStr.as_str(self._type)
     reg_id = self._reg.reg_id2name(self._type)
     if reg_id is None:
         # XXX parent must search upwards if not found
         reg_id = self._reg.parent.reg_id2name(self._type)
     return reg_id
Exemple #4
0
    def expect_field(self, msg_spec, line, indent, step):
        if not line.startswith(indent):
            raise ParserError(
                "wrong indent for field declaration: '%s'" %
                line)

        # DEBUG
        print("expectField: line = '%s'" % line)
        # END

        line = line[len(indent):]

        # from here we are very sloppy about the indent

        # accept NAME FTYPE(Q)? (@N)? (=DEFAULT)?

        words = line.split()
        word_count = len(words)

        # DEBUG
        print("             found %d tokens: %s" % (word_count, words))
        # END

        if word_count < 2:
            raise ParserError("too few tokens in field def '%s'" % line)
        if word_count > 5:
            raise ParserError("too many tokens in field def '%s'" % line)

        # -- field name -------------------------
        f_name = words[0]
        validate_simple_name(f_name)

        # -- quantifier -------------------------
        qqq = words[1][-1]
        if qqq == '?' or qqq == '*' or qqq == '+':
            words[1] = words[1][:-1]
            if qqq == '?':
                quantifier = Q_OPTIONAL
            elif qqq == '*':
                quantifier = Q_STAR
            else:
                quantifier = Q_PLUS
        else:
            quantifier = Q_REQUIRED

        # -- field type --------------------------
        type_name = words[1]
        validate_dotted_name(type_name)
        field_type = None

        # DEBUG ###
        print("             field '%s' type '%s' quant %d" % (
            f_name, type_name, quantifier))
        # END #####

        # first check against list of names of field types
        f_types = FieldStr()
        try:
            field_type = f_types.ndx(type_name)
            # DEBUG
            print(
                "LIST type_name is '%s', index is '%s'" %
                (type_name, field_type))
            # END
        except KeyError:
            # DEBUG
            print("NOT IN LIST type_name '%s'" % type_name)
            # END
            field_type = None

        if field_type is None:
            # check at the message level
            field_type = msg_spec.reg.name2reg_id(type_name)
            # DEBUG
            print(
                "MSG type_name is '%s', index is '%s'" %
                (type_name, field_type))
            # END

        if field_type is None:
            # ask the parent to resolve
            field_type = msg_spec.parent.reg.name2reg_id(type_name)
            # DEBUG
            print(
                "PARENT type_name is '%s', index is '%s'" %
                (type_name, field_type))
            # END

        if field_type is None:
            err_msg = "type_name = '%s'; can't determine field type in line: '%s'" % (
                type_name, line)
            raise ParserError(err_msg)

        # -- field number -----------------------
        field_nbr = -1
        if word_count > 2:
            if words[2].startswith('@'):
                field_nbr = int(words[2][1:])    # could use some validation
#               if fieldNbr < nextFieldNbr:
#                   raise ValueError('field number <= last field number')

        # -- default ----------------------------
        # XXX STUB - NOT IMPLEMENTED YET

        msg_spec.add_field(
            FieldSpec(msg_spec.reg, f_name, field_type, quantifier, field_nbr))
Exemple #5
0
    def test_writing_and_reading(self):
        writer = TFWriter.create(LE_MSG_SPEC, BUFSIZE)
        writer.clear()  # should not be necessary

        buf = writer.buffer
        # reader and writer share same buffer
        reader = TFReader(LE_MSG_SPEC, BUFSIZE, buf)

        tstamp = int(time.time())
        node_id = bytearray(20)         # 160 bit
        RNG.next_bytes(node_id)  # .... random value
        key = bytearray(20)         # 160 bit
        RNG.next_bytes(key)  # .... random value
        length = RNG.next_int32()
        by_ = RNG.next_file_name(16)
        path = 'path/to/' + RNG.next_file_name(16)

        nnn = 0                           # 0-based field number
        # write a log entry into the buffer
        writer.put_next(nnn, tstamp)
        nnn = nnn + 1
        writer.put_next(nnn, node_id)
        nnn = nnn + 1
        writer.put_next(nnn, key)
        nnn = nnn + 1
        writer.put_next(nnn, length)
        nnn = nnn + 1
        writer.put_next(nnn, by_)
        nnn = nnn + 1
        writer.put_next(nnn, path)

        # now read the buffer to see what actually was written
        self.assertEqual(0, reader.position)

        reader.get_next()
        self.assertEqual(0, reader.field_nbr)
        self.assertEqual('fuint32', FieldStr.as_str(reader.field_type))
        self.assertEqual(tstamp, reader.value)
        self.assertEqual(5, reader.position)

        reader.get_next()
        self.assertEqual(1, reader.field_nbr)
        self.assertEqual('fbytes20', FieldStr.as_str(reader.field_type))
        self.assertEqual(node_id, reader.value)
        self.assertEqual(26, reader.position)

        reader.get_next()
        self.assertEqual(2, reader.field_nbr)
        self.assertEqual('fbytes20', FieldStr.as_str(reader.field_type))
        self.assertEqual(key, reader.value)
        self.assertEqual(47, reader.position)

        reader.get_next()
        self.assertEqual(3, reader.field_nbr)
        self.assertEqual('vuint32', FieldStr.as_str(reader.field_type))
        self.assertEqual(length, reader.value)

        reader.get_next()
        self.assertEqual(4, reader.field_nbr)
        self.assertEqual('lstring', FieldStr.as_str(reader.field_type))
        self.assertEqual(by_, reader.value)

        reader.get_next()
        self.assertEqual(5, reader.field_nbr)
        self.assertEqual('lstring', FieldStr.as_str(reader.field_type))
        self.assertEqual(path, reader.value)