Changeset 8:acc105036a41 in xplra for misc/client.py


Ignore:
Timestamp:
01/04/13 14:24:04 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added the registry of multiple-data queries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • misc/client.py

    r7 r8  
    3939
    4040    @staticmethod
     41    def _parseGetSpec(words):
     42        """Parse a data query specification from the given argument word lisst.
     43
     44        If there is some failure, it returns None.
     45
     46        Otherwise it returns a tuple of two items:
     47        - the parsed data, which is a tuple of these items:
     48          - the name of the dataref to query
     49          - the type of the dataref to query (it is a valid type, checked by
     50          the function)
     51          - the length of the dataref to query
     52          - the offset to query the dataref from
     53
     54        - the remainder of the argument list."""
     55        if len(words)<2:
     56            print >> sys.stderr, "Missing parameters"
     57            return None
     58
     59        nextIndex = 2
     60
     61        name = words[0]
     62        type = words[1]
     63        if type not in CLI._types:
     64            print >> sys.stderr, "Invalid type"
     65            return None
     66
     67        length = None
     68        offset = None
     69        if type in ["ia", "fa", "ba", "s"]:
     70            if len(words)<4:
     71                print >> sys.stderr, "Missing parameters"
     72                return None
     73            length = int(words[2])
     74            offset = int(words[3])
     75            nextIndex = 4
     76
     77        return ((name, type, length, offset), words[nextIndex:])
     78
     79    @staticmethod
    4180    def _packLength(x):
    4281        """Pack the given integer as a variable-length value."""
     
    6099
    61100        self._stream = stream
     101        self._multiGets = {}
    62102
    63103        self.use_rawinput = True
     
    78118        """Handle the 'get' command"""
    79119        words = self._splitArgs(args)
    80         if len(words)<2:
    81             print >> sys.stderr, "Missing parameters"
    82             return False
    83 
    84         name = words[0]
    85 
    86         type = words[1]
    87         if type not in self._types:
    88             print >> sys.stderr, "Invalid type"
    89             return False
    90 
    91         length = None
    92         offset = None
    93         if len(words)>3:
    94             length = int(words[2])
    95             offset = int(words[3])
     120        result = self._parseGetSpec(words)
     121        if result is None:
     122            return False
     123
     124        (name, type, length, offset) = result[0]
    96125
    97126        self._writeU8(0x01)
     
    107136            print self._readValue(type)
    108137        else:
    109             print "Error code:", result
     138            print >> sys.stderr, "Error code:", result
    110139
    111140    def do_set(self, args):
     
    143172        result = self._readU8()
    144173        if result!=0:
    145             print "Error code:", result
     174            print >> sys.stderr, "Error code:", result
     175
     176    def do_reg_multiget(self, args):
     177        """Handle the 'reg_multiget' command"""
     178        words = self._splitArgs(args)
     179        specs = []
     180        while words:
     181            result = self._parseGetSpec(words)
     182            if result is None:
     183                return False
     184            else:
     185                specs.append(result[0])
     186                words = result[1]
     187
     188        if not specs:
     189            return False
     190
     191        self._writeU8(0x11)
     192        self._writeU32(len(specs))
     193        for (name, type, length, offset) in specs:
     194            print name, type
     195            self._writeString(name)
     196            self._writeU8(self._types[type])
     197            if length is not None:
     198                self._writeS32(length)
     199                self._writeS32(offset)
     200        self._flush()
     201
     202        result = self._readU8()
     203        if result==0:
     204            id = self._readU32()
     205            self._multiGets[id] = \
     206                [type for (name, type, length, offset) in specs]
     207            print "ID:", id
     208        else:
     209            print >> sys.stderr, "Error code:", result
     210
     211    def do_unreg_multiget(self, args):
     212        """Handle the 'unreg_multiget' command"""
     213        words = self._splitArgs(args)
     214        if len(words)<1:
     215            print >> sys.stderr, "Missing parameter"
     216            return False
     217
     218        id = int(words[0])
     219
     220        self._writeU8(0x12)
     221        self._writeU32(id)
     222        self._flush()
     223
     224        result = self._readU8()
     225        if result!=0:
     226            print >> sys.stderr, "Error code:", result
     227
     228        if id in self._multiGets:
     229            del self._multiGets[id]
     230
     231    def do_exec_multiget(self, args):
     232        """Handle the 'unreg_multiget' command"""
     233        words = self._splitArgs(args)
     234        if len(words)<1:
     235            print >> sys.stderr, "Missing parameter"
     236            return False
     237
     238        id = int(words[0])
     239        if id not in self._multiGets:
     240            print >> sys.stderr, "Invalid ID"
     241            return False
     242
     243        self._writeU8(0x13)
     244        self._writeU32(id)
     245        self._flush()
     246
     247        result = self._readU8()
     248        if result==0:
     249            for type in self._multiGets[id]:
     250                value = self._readValue(type)
     251                print value
     252        elif result==0x02:
     253            index = self._readU32()
     254            print >> sys.stderr, "Invalid dataref at #%d" % (index,)
     255        else:
     256            print >> sys.stderr, "Error code:", result
    146257
    147258    def _writeU8(self, x):
     
    153264        self._stream.write(struct.pack("i", x))
    154265
     266    def _writeU32(self, x):
     267        """Write the given value as an unsigned, 32-bit value."""
     268        self._stream.write(struct.pack("I", x))
     269
    155270    def _writeFloat(self, x):
    156271        """Write the given value as a single-precision floating point."""
     
    181296        """Read a signed, 32-bit value from the stream."""
    182297        (value,) = struct.unpack("i", self._stream.read(4))
     298        return value
     299
     300    def _readU32(self):
     301        """Read an unsigned, 32-bit value from the stream."""
     302        (value,) = struct.unpack("I", self._stream.read(4))
    183303        return value
    184304
Note: See TracChangeset for help on using the changeset viewer.