[3] | 1 | #!/usr/bin/env python
|
---|
| 2 |
|
---|
[6] | 3 | import sys
|
---|
[3] | 4 | import cmd
|
---|
| 5 | import struct
|
---|
| 6 | import socket
|
---|
| 7 | import os
|
---|
| 8 |
|
---|
| 9 | #------------------------------------------------------------------------------
|
---|
| 10 |
|
---|
| 11 | class CLI(cmd.Cmd):
|
---|
| 12 | """Simple command-line interface for the X-Plane Remote Access plugin."""
|
---|
| 13 | _types = { "i" : 0x01,
|
---|
| 14 | "f" : 0x02,
|
---|
| 15 | "d" : 0x03,
|
---|
| 16 | "fa" : 0x11,
|
---|
| 17 | "ia" : 0x12,
|
---|
| 18 | "ba" : 0x13 }
|
---|
| 19 |
|
---|
| 20 | @staticmethod
|
---|
| 21 | def _packLength(x):
|
---|
| 22 | """Pack the given integer as a variable-length value."""
|
---|
| 23 | s = ""
|
---|
| 24 | while True:
|
---|
| 25 | y = x&0x7f
|
---|
| 26 | x >>= 7
|
---|
| 27 | if x>0: y |= 0x80
|
---|
| 28 | s += struct.pack("B", y)
|
---|
| 29 | if x==0:
|
---|
| 30 | return s
|
---|
| 31 |
|
---|
| 32 | @staticmethod
|
---|
| 33 | def _packString(s):
|
---|
| 34 | """Pack the given string."""
|
---|
| 35 | return CLI._packLength(len(s)) + s
|
---|
| 36 |
|
---|
| 37 | def __init__(self, stream):
|
---|
| 38 | """Construct the CLI."""
|
---|
| 39 | cmd.Cmd.__init__(self)
|
---|
| 40 |
|
---|
| 41 | self._stream = stream
|
---|
| 42 |
|
---|
| 43 | self.use_rawinput = True
|
---|
| 44 | self.intro = "\nX-Plane Remote Access plugin command prompt\n"
|
---|
[6] | 45 | self.prompt = "XPLRA> "
|
---|
[3] | 46 |
|
---|
| 47 | self.daemon = True
|
---|
| 48 |
|
---|
| 49 | def default(self, line):
|
---|
| 50 | """Handle uhandled commands"""
|
---|
| 51 | if line=="EOF":
|
---|
| 52 | print
|
---|
[6] | 53 | return True
|
---|
[3] | 54 | else:
|
---|
| 55 | return super(CLI, self).default(line)
|
---|
| 56 |
|
---|
| 57 | def do_get(self, args):
|
---|
| 58 | """Handle the 'get' command"""
|
---|
| 59 | words = args.split()
|
---|
| 60 | if len(words)<2:
|
---|
[6] | 61 | print >> sys.stderr, "Missing parameters"
|
---|
[3] | 62 | return False
|
---|
| 63 |
|
---|
| 64 | name = words[0]
|
---|
| 65 |
|
---|
| 66 | type = words[1]
|
---|
| 67 | if type not in self._types:
|
---|
[6] | 68 | print >> sys.stderr, "Invalid type"
|
---|
[3] | 69 | return False
|
---|
| 70 |
|
---|
| 71 | length = None
|
---|
[6] | 72 | offset = None
|
---|
| 73 | if len(words)>3:
|
---|
[3] | 74 | length = int(words[2])
|
---|
[6] | 75 | offset = int(words[3])
|
---|
[3] | 76 |
|
---|
| 77 | self._writeU8(0x01)
|
---|
| 78 | self._writeString(name)
|
---|
| 79 | self._writeU8(self._types[type])
|
---|
| 80 | if length is not None:
|
---|
| 81 | self._writeS32(length)
|
---|
[6] | 82 | self._writeS32(offset)
|
---|
[3] | 83 | self._flush()
|
---|
| 84 |
|
---|
| 85 | result = self._readU8()
|
---|
| 86 | if result==0:
|
---|
[6] | 87 | value = None
|
---|
[3] | 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)
|
---|
[6] | 110 | #value = bytes
|
---|
[3] | 111 |
|
---|
[6] | 112 | print value
|
---|
| 113 | else:
|
---|
| 114 | print "Result code:", result
|
---|
[3] | 115 |
|
---|
| 116 | def _writeU8(self, x):
|
---|
| 117 | """Write the given value as an unsigned, 8-bit value."""
|
---|
| 118 | self._stream.write(struct.pack("B", x))
|
---|
| 119 |
|
---|
| 120 | def _writeS32(self, x):
|
---|
| 121 | """Write the given value as a signed, 32-bit value."""
|
---|
| 122 | self._stream.write(struct.pack("i", x))
|
---|
| 123 |
|
---|
| 124 | def _writeLength(self, length):
|
---|
| 125 | """Write the given value is a variable-length value into our stream."""
|
---|
| 126 | self._stream.write(self._packLength(length))
|
---|
| 127 |
|
---|
| 128 | def _writeString(self, str):
|
---|
| 129 | """Write the given string into the stream."""
|
---|
| 130 | self._stream.write(self._packString(str))
|
---|
| 131 |
|
---|
| 132 | def _flush(self):
|
---|
| 133 | """Flush our stream."""
|
---|
| 134 | self._stream.flush()
|
---|
| 135 |
|
---|
| 136 | def _readU8(self):
|
---|
| 137 | """Read an unsigned, 8-bit value from the stream."""
|
---|
| 138 | (value,) = struct.unpack("B", self._stream.read(1))
|
---|
| 139 | return value
|
---|
| 140 |
|
---|
| 141 | def _readS32(self):
|
---|
| 142 | """Read a signed, 32-bit value from the stream."""
|
---|
| 143 | (value,) = struct.unpack("i", self._stream.read(4))
|
---|
| 144 | return value
|
---|
| 145 |
|
---|
| 146 | def _readFloat(self):
|
---|
| 147 | """Read a single-precision floating point value from the stream."""
|
---|
| 148 | (value,) = struct.unpack("f", self._stream.read(4))
|
---|
| 149 | return value
|
---|
| 150 |
|
---|
| 151 | def _readDouble(self):
|
---|
| 152 | """Read a double-precision floating point value from the stream."""
|
---|
| 153 | (value,) = struct.unpack("d", self._stream.read(8))
|
---|
| 154 | return value
|
---|
| 155 |
|
---|
| 156 | def _readLength(self):
|
---|
| 157 | """Read a variable-length value from our stream."""
|
---|
| 158 | length = 0
|
---|
| 159 | while True:
|
---|
| 160 | (x,) = struct.unpack("B", self._stream.read(1))
|
---|
| 161 | length <<= 7
|
---|
| 162 | length |= x&0x7f
|
---|
| 163 | if x&0x80==0:
|
---|
| 164 | return length
|
---|
| 165 |
|
---|
| 166 | @staticmethod
|
---|
| 167 | def _readString(self):
|
---|
| 168 | """Read a string from our stream."""
|
---|
| 169 | length = self._readLength()
|
---|
| 170 | return self._stream.read(length)
|
---|
| 171 |
|
---|
| 172 | #------------------------------------------------------------------------------
|
---|
| 173 |
|
---|
| 174 | if __name__ == "__main__":
|
---|
| 175 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
---|
| 176 | s.connect("/tmp/xplra-" + os.environ["LOGNAME"])
|
---|
| 177 | CLI(s.makefile()).cmdloop()
|
---|