Changeset 7:bafe58db1509 in xplra for misc
- Timestamp:
- 01/04/13 12:50:45 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- misc
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
misc/client.py
r6 r7 16 16 "fa" : 0x11, 17 17 "ia" : 0x12, 18 "ba" : 0x13 } 18 "ba" : 0x13, 19 "s" : 0x13 } 20 21 @staticmethod 22 def _splitArgs(args): 23 """Split the given argument string into a list of words.""" 24 words = [] 25 word = "" 26 inQuote = False 27 for c in args: 28 if c.isspace() and not inQuote: 29 if word: 30 words.append(word) 31 word = "" 32 elif c=="\"" and (not word or inQuote): 33 inQuote = not inQuote 34 else: 35 word += c 36 if word: 37 words.append(word) 38 return words 19 39 20 40 @staticmethod … … 57 77 def do_get(self, args): 58 78 """Handle the 'get' command""" 59 words = args.split()79 words = self._splitArgs(args) 60 80 if len(words)<2: 61 81 print >> sys.stderr, "Missing parameters" … … 85 105 result = self._readU8() 86 106 if result==0: 87 value = None 88 if type=="i": 89 value = self._readS32() 90 elif type=="f": 91 value = self._readFloat() 92 elif type=="d": 93 value = self._readDouble() 94 elif type=="fa": 95 length = self._readS32() 96 if length>0: 97 value = [self._readFloat() for i in range(0, length)] 98 elif type=="ia": 99 length = self._readS32() 100 if length>0: 101 value = [self._readS32() for i in range(0, length)] 102 elif type=="ba": 103 length = self._readS32() 104 if length>0: 105 bytes = [self._readU8() for i in range(0, length)] 106 value = "" 107 for b in bytes: 108 if b==0: break 109 value += chr(b) 110 #value = bytes 111 112 print value 107 print self._readValue(type) 113 108 else: 114 print "Result code:", result 109 print "Error code:", result 110 111 def do_set(self, args): 112 """Handle the 'set' command""" 113 words = self._splitArgs(args) 114 if len(words)<3: 115 print >> sys.stderr, "Missing parameters" 116 return False 117 118 name = words[0] 119 120 type = words[1] 121 if type not in self._types: 122 print >> sys.stderr, "Invalid type" 123 return False 124 125 value = words[2] 126 127 length = None 128 offset = None 129 if len(words)>4: 130 length = int(words[3]) 131 offset = int(words[4]) 132 133 self._writeU8(0x02) 134 self._writeString(name) 135 self._writeU8(self._types[type]) 136 if length is not None: 137 self._writeS32(length) 138 self._writeS32(offset) 139 self._writeValue(type, value, length) 140 141 self._flush() 142 143 result = self._readU8() 144 if result!=0: 145 print "Error code:", result 115 146 116 147 def _writeU8(self, x): … … 121 152 """Write the given value as a signed, 32-bit value.""" 122 153 self._stream.write(struct.pack("i", x)) 154 155 def _writeFloat(self, x): 156 """Write the given value as a single-precision floating point.""" 157 self._stream.write(struct.pack("f", x)) 158 159 def _writeDouble(self, x): 160 """Write the given value as a double-precision floating point.""" 161 self._stream.write(struct.pack("d", x)) 123 162 124 163 def _writeLength(self, length): … … 164 203 return length 165 204 166 @staticmethod167 205 def _readString(self): 168 206 """Read a string from our stream.""" … … 170 208 return self._stream.read(length) 171 209 210 def _readValue(self, type): 211 """Read the value of the given type from the stream.""" 212 if type=="i": 213 return self._readS32() 214 elif type=="f": 215 return self._readFloat() 216 elif type=="d": 217 return self._readDouble() 218 elif type in ["fa", "ia", "ba", "s"]: 219 length = self._readS32() 220 if length>8192: 221 print "Very big length:", length 222 return None 223 elif length>0: 224 if type=="fa": 225 return [self._readFloat() for i in range(0, length)] 226 elif type=="ia": 227 return [self._readS32() for i in range(0, length)] 228 elif type=="ba" or type=="s": 229 bytes = [self._readU8() for i in range(0, length)] 230 if type=="ba": 231 return bytes 232 else: 233 string = "" 234 for b in bytes: 235 if b==0: break 236 string += chr(b) 237 return string 238 elif length==0: 239 return [] 240 else: 241 return None 242 243 def _writeValue(self, type, value, length = None): 244 """Write the given value of the given type to the stream.""" 245 if type=="i": 246 self._writeS32(int(value)) 247 elif type=="f": 248 self._writeFloat(float(value)) 249 elif type=="d": 250 self._writeDouble(float(value)) 251 elif type in ["fa", "ia", "ba", "s"]: 252 if type=="s": 253 values = [ord(c) for c in value] 254 else: 255 words = [word.strip() for word in value.split(",")] 256 values = [float(word) if type=="fa" else int(word) 257 for word in words] 258 if len(values)<length: 259 values += [0.0 if type=="fa" else 0] * (length-len(values)) 260 elif len(values)>length: 261 values = values[:length] 262 for value in values: 263 if type=="fa": 264 self._writeFloat(value) 265 elif type=="ia": 266 self._writeS32(value) 267 else: 268 self._writeU8(value) 269 172 270 #------------------------------------------------------------------------------ 173 271 -
misc/test1.sh
r6 r7 11 11 get sim/flightmodel/position/latitude d 12 12 get sim/flightmodel/position/longitude d 13 get sim/aircraft/view/acf_descrip ba-1 013 get sim/aircraft/view/acf_descrip s -1 0 14 14 get sim/aircraft/prop/acf_en_type ia -1 0 15 15 get sim/aircraft/prop/acf_prop_dir fa -1 0
Note:
See TracChangeset
for help on using the changeset viewer.