Changeset 40:ec5dde8a6ff6 in xplra
- Timestamp:
- 02/14/13 18:21:47 (12 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
src/client/c/hu/varadiistvan/xplra/Exception.cc
r14 r40 61 61 //------------------------------------------------------------------------------ 62 62 63 ProtocolException::ProtocolException(errorCode_t errorCode) throw() : 64 errorCode(errorCode) 63 ProtocolException::ProtocolException(errorCode_t errorCode, bool hasParameter, 64 long parameter) throw() : 65 errorCode(errorCode), 66 hasParameter(parameter), 67 parameter(parameter) 65 68 { 66 69 switch (errorCode) { … … 70 73 case UNKNOWN_DATAREF: 71 74 message = "xplra::ProtocolException: unknown dataref"; 75 if (hasParameter) { 76 char buf[32]; 77 snprintf(buf, sizeof(buf), " (# %ld)", parameter); 78 message += buf; 79 } 72 80 break; 73 81 case INVALID_TYPE: … … 85 93 case INVALID_ID: 86 94 message = "xplra::ProtocolException: invalid ID"; 95 break; 96 case INVALID_DURATION: 97 message = "xplra::ProtocolException: invalid duration"; 87 98 break; 88 99 case OTHER: -
src/client/c/hu/varadiistvan/xplra/Exception.h
r19 r40 143 143 INVALID_ID = 7, 144 144 145 /// Invalid duration 146 INVALID_DURATION = 8, 147 145 148 /// Other error 146 149 OTHER = 255 … … 153 156 errorCode_t errorCode; 154 157 158 /** 159 * Indicate if the parameter is valid. 160 */ 161 bool hasParameter; 162 163 /** 164 * Parameter of the error. 165 */ 166 long parameter; 167 155 168 public: 156 169 /** 157 170 * Construct the exception. 158 171 */ 159 ProtocolException(errorCode_t errorCode) throw(); 172 ProtocolException(errorCode_t errorCode, 173 bool hasParameter = false, long parameter = 0) throw(); 160 174 161 175 /** … … 163 177 */ 164 178 errorCode_t getErrorCode() const throw(); 179 180 /** 181 * Get the value of the parameter and whether it is valid or not. 182 */ 183 bool getParameter(long& param) const throw(); 165 184 }; 166 185 … … 246 265 { 247 266 return errorCode; 267 } 268 269 //------------------------------------------------------------------------------ 270 271 inline bool ProtocolException::getParameter(long& param) const throw() 272 { 273 if (hasParameter) param = parameter; 274 return hasParameter; 248 275 } 249 276 -
src/client/c/hu/varadiistvan/xplra/MultiBuffer.cc
r33 r40 610 610 611 611 xplane.stream->flush(); 612 xplane.checkResult( );612 xplane.checkResult(true); 613 613 } 614 614 -
src/client/c/hu/varadiistvan/xplra/MultiGetter.cc
r33 r40 97 97 xplane.stream->writeU32(registeredID); 98 98 xplane.stream->flush(); 99 xplane.checkResult( );99 xplane.checkResult(true); 100 100 101 101 readValues(); -
src/client/c/hu/varadiistvan/xplra/MultiSetter.cc
r33 r40 99 99 } 100 100 xplane.stream->flush(); 101 xplane.checkResult( );101 xplane.checkResult(true); 102 102 } 103 103 … … 121 121 } 122 122 xplane.stream->flush(); 123 xplane.checkResult( );123 xplane.checkResult(true); 124 124 } 125 125 -
src/client/c/hu/varadiistvan/xplra/XPlane.cc
r36 r40 73 73 //------------------------------------------------------------------------------ 74 74 75 void XPlane::checkResult(uint8_t result) throw(ProtocolException) 75 void XPlane::checkResult(uint8_t result, bool hasParameter, long parameter) 76 throw(ProtocolException) 76 77 { 77 78 switch(result) { … … 79 80 return; 80 81 case Protocol::RESULT_INVALID_COMMAND: 81 throw ProtocolException(ProtocolException::INVALID_COMMAND); 82 throw ProtocolException(ProtocolException::INVALID_COMMAND, 83 hasParameter, parameter); 82 84 case Protocol::RESULT_UNKNOWN_DATAREF: 83 throw ProtocolException(ProtocolException::UNKNOWN_DATAREF); 85 throw ProtocolException(ProtocolException::UNKNOWN_DATAREF, 86 hasParameter, parameter); 84 87 case Protocol::RESULT_INVALID_TYPE: 85 throw ProtocolException(ProtocolException::INVALID_TYPE); 88 throw ProtocolException(ProtocolException::INVALID_TYPE, 89 hasParameter, parameter); 86 90 case Protocol::RESULT_INVALID_LENGTH: 87 throw ProtocolException(ProtocolException::INVALID_LENGTH); 91 throw ProtocolException(ProtocolException::INVALID_LENGTH, 92 hasParameter, parameter); 88 93 case Protocol::RESULT_INVALID_OFFSET: 89 throw ProtocolException(ProtocolException::INVALID_OFFSET); 94 throw ProtocolException(ProtocolException::INVALID_OFFSET, 95 hasParameter, parameter); 90 96 case Protocol::RESULT_INVALID_COUNT: 91 throw ProtocolException(ProtocolException::INVALID_COUNT); 97 throw ProtocolException(ProtocolException::INVALID_COUNT, 98 hasParameter, parameter); 92 99 case Protocol::RESULT_INVALID_ID: 93 throw ProtocolException(ProtocolException::INVALID_ID); 100 throw ProtocolException(ProtocolException::INVALID_ID, 101 hasParameter, parameter); 102 case Protocol::RESULT_INVALID_DURATION: 103 throw ProtocolException(ProtocolException::INVALID_DURATION, 104 hasParameter, parameter); 94 105 case Protocol::RESULT_OTHER_ERROR: 95 106 default: 96 throw ProtocolException(ProtocolException::OTHER); 107 throw ProtocolException(ProtocolException::OTHER, 108 hasParameter, parameter); 97 109 } 98 110 } … … 100 112 //------------------------------------------------------------------------------ 101 113 102 void XPlane::checkResult( ) throw(ProtocolException, IOException)114 void XPlane::checkResult(bool multi) throw(ProtocolException, IOException) 103 115 { 104 116 uint8_t result = stream->readU8(); 105 checkStream(); 106 checkResult(result); 117 bool hasParameter = false; 118 long parameter = 0; 119 if (multi) { 120 if (result==Protocol::RESULT_UNKNOWN_DATAREF) { 121 parameter = stream->readU32(); 122 hasParameter = true; 123 } 124 } 125 checkStream(); 126 checkResult(result, hasParameter, parameter); 107 127 } 108 128 … … 205 225 206 226 checkStream(); 227 } 228 229 //------------------------------------------------------------------------------ 230 231 void XPlane::reloadPlugins() throw(Exception) 232 { 233 stream->writeU8(Protocol::COMMAND_RELOAD_PLUGINS); 234 stream->flush(); 235 checkResult(); 207 236 } 208 237 … … 457 486 } 458 487 } 488 489 //------------------------------------------------------------------------------ 490 491 void XPlane::showMessage(const char* message, float duration) throw(Exception) 492 { 493 stream->writeU8(Protocol::COMMAND_SHOW_MESSAGE); 494 stream->writeString(message); 495 stream->writeFloat(duration); 496 stream->flush(); 497 checkResult(); 498 } 499 459 500 //------------------------------------------------------------------------------ 460 501 -
src/client/c/hu/varadiistvan/xplra/XPlane.h
r36 r40 150 150 151 151 /** 152 * Reload the plugins loaded in X-Plane. After this the connection 153 * fails. 154 */ 155 void reloadPlugins() throw(Exception); 156 157 /** 152 158 * Get the integer value of the dataref with the given name. 153 159 */ … … 271 277 size_t offset = 0) throw(Exception); 272 278 279 /** 280 * Show a textual message for a certain duration. 281 */ 282 void showMessage(const char* message, float duration) throw(Exception); 283 273 284 private: 274 285 /** … … 281 292 * throw a ProtocolException with the correct error code. 282 293 */ 283 void checkResult(uint8_t result) throw(ProtocolException); 294 void checkResult(uint8_t result, bool hasParameter = false, 295 long parameter = 0) throw(ProtocolException); 284 296 285 297 /** … … 288 300 * is some problem with the stream, an IOException is thrown. 289 301 */ 290 void checkResult( ) throw(ProtocolException, IOException);302 void checkResult(bool multi = false) throw(ProtocolException, IOException); 291 303 292 304 /** -
src/client/c/hu/varadiistvan/xplra/xplra.cc
r36 r40 515 515 //------------------------------------------------------------------------------ 516 516 517 extern "C" int xplra_reload_plugins(int connectionID) 518 { 519 Connection* connection = ConnectionSlot::getValue(connectionID); 520 if (connection==0) return -1; 521 try { 522 connection->reloadPlugins(); 523 return 0; 524 } catch (...) { 525 connection->handleException(); 526 return -1; 527 } 528 } 529 530 //------------------------------------------------------------------------------ 531 517 532 extern "C" int xplra_get_int(int* value, int connectionID, const char* name) 518 533 { … … 1267 1282 try { 1268 1283 return connection->destroyMultiBuffer(bufferID) ? 0 : -1; 1284 } catch(...) { 1285 connection->handleException(); 1286 return -1; 1287 } 1288 } 1289 1290 //------------------------------------------------------------------------------ 1291 //------------------------------------------------------------------------------ 1292 1293 extern "C" int xplra_show_message(int connectionID, 1294 const char* message, float duration) 1295 { 1296 Connection* connection = ConnectionSlot::getValue(connectionID); 1297 if (connection==0) return -1; 1298 1299 try { 1300 connection->showMessage(message, duration); 1301 return 0; 1269 1302 } catch(...) { 1270 1303 connection->handleException(); -
src/client/c/hu/varadiistvan/xplra/xplra.h
r36 r40 79 79 #define ERROR_PROTOCOL_INVALID_ID = 7 80 80 81 /** An invalid duration was specified */ 82 #define ERROR_PROTOCOL_INVALID_DURATION = 8 83 81 84 /** Other protocol error */ 82 #define ERROR_PROTOCOL_OTHER = 885 #define ERROR_PROTOCOL_OTHER = 255 83 86 84 87 /** A function requiring a connection is called without a connection */ … … 137 140 int* xplaneVersion, int* xplmVersion, 138 141 int* xplraVersion); 142 143 /*----------------------------------------------------------------------------*/ 144 145 /** 146 * Reload the plugins in X-Plane. After this the connection fails. 147 */ 148 int xplra_reload_plugins(int connectionID); 139 149 140 150 /*----------------------------------------------------------------------------*/ … … 720 730 721 731 /** 722 * Destr uy a multi-dataref buffer for the given connection.732 * Destroy a multi-dataref buffer for the given connection. 723 733 * 724 734 * @param connectionID the ID of the connection for which the buffer … … 729 739 int xplra_multi_destroy_buffer(int connectionID, int bufferID); 730 740 741 /*----------------------------------------------------------------------------*/ 742 /*----------------------------------------------------------------------------*/ 743 744 /** 745 * Show a message in the simulator window for the given duration. 746 */ 747 int xplra_show_message(int connectionID, const char* message, float duration); 748 749 /*----------------------------------------------------------------------------*/ 731 750 /*----------------------------------------------------------------------------*/ 732 751 -
src/client/python/xplra.py
r37 r40 30 30 COMMAND_GET_VERSIONS = 0x31 31 31 32 COMMAND_RELOAD_PLUGINS = 0x32 33 34 COMMAND_SHOW_MESSAGE = 0x41 35 32 36 TYPE_INT = 0x01 33 37 … … 57 61 58 62 RESULT_INVALID_ID = 0x07 63 64 RESULT_INVALID_DURATION = 0x08 59 65 60 66 RESULT_OTHER_ERROR = 0xff … … 71 77 RESULT_INVALID_COUNT : "invalid count", 72 78 RESULT_INVALID_ID : "invalid ID", 79 RESULT_INVALID_DURATION : "invalid duration", 73 80 RESULT_OTHER_ERROR : "other error" } 74 81 … … 81 88 return "unknown error code " + `resultCode` 82 89 83 def __init__(self, resultCode): 84 super(ProtocolException, self).__init__("xplra.ProtocolException: " + 85 self.getMessage(resultCode)) 90 def __init__(self, resultCode, parameter = None): 91 message = "xplra.ProtocolException: " + self.getMessage(resultCode) 92 if parameter is not None: 93 if resultCode==RESULT_UNKNOWN_DATAREF: 94 message += " (# %d)" % (parameter,) 95 96 super(ProtocolException, self).__init__(message) 86 97 self.resultCode = resultCode 98 self.parameter = parameter 87 99 88 100 #------------------------------------------------------------------------------- … … 145 157 return (self._readS32(), self._readS32(), self._readS32()) 146 158 159 def reloadPlugins(self): 160 """Reload the plugins in X-Plane. 161 162 After this, this connection becomes invalid.""" 163 self._writeU8(COMMAND_RELOAD_PLUGINS) 164 self._flush() 165 self._checkResult(); 166 147 167 def getInt(self, name): 148 168 """Get the value of the integer dataref with the given name.""" … … 212 232 self.setByteArray(name, value, offset) 213 233 234 def showMessage(self, message, duration): 235 """Show a message in the simulator window for the given duration. 236 237 The duration is a floating-point number denoting seconds.""" 238 self._writeU8(COMMAND_SHOW_MESSAGE) 239 self._writeString(message) 240 self._writeFloat(duration) 241 self._flush() 242 self._checkResult() 243 214 244 def disconnect(self): 215 245 """Disconnect from X-Plane.""" … … 218 248 self._stream = None 219 249 220 def _checkResult(self, resultCode = None ):250 def _checkResult(self, resultCode = None, parameter = None, multi = False): 221 251 """Check the given result code. 222 252 … … 227 257 if resultCode is None: 228 258 resultCode = self._readU8() 259 if multi and resultCode==RESULT_UNKNOWN_DATAREF: 260 parameter = self._readU32() 261 229 262 if resultCode!=RESULT_OK: 230 raise ProtocolException(resultCode )263 raise ProtocolException(resultCode, parameter) 231 264 232 265 def _getSingle(self, name, type, length = None, offset = None): … … 525 558 526 559 self._xplane._flush() 527 self._xplane._checkResult( )560 self._xplane._checkResult(multi = True) 528 561 529 562 def __len__(self): … … 591 624 self._xplane._flush() 592 625 593 self._xplane._checkResult( )626 self._xplane._checkResult(multi = True) 594 627 595 628 self._readValues() … … 623 656 self._xplane._flush() 624 657 625 self._xplane._checkResult( )658 self._xplane._checkResult(multi = True) 626 659 627 660 def _executeUnregistered(self): … … 644 677 645 678 self._xplane._flush() 646 self._xplane._checkResult( )647 648 #------------------------------------------------------------------------------- 679 self._xplane._checkResult(multi = True) 680 681 #------------------------------------------------------------------------------- -
test/basicctest.c
r36 r40 33 33 #include <stdio.h> 34 34 #include <string.h> 35 36 #ifdef _WIN32 37 #include <windows.h> 38 #else 39 #include <unistd.h> 40 #endif 41 42 //------------------------------------------------------------------------------ 43 44 #ifdef _WIN32 45 void _sleep(int ms) 46 { 47 Sleep(ms); 48 } 49 #else 50 void _sleep(int ms) 51 { 52 usleep(ms*1000); 53 } 54 #endif 55 35 56 36 57 //------------------------------------------------------------------------------ … … 74 95 } 75 96 97 printf("Showing a message...\n"); 98 if (xplra_show_message(connectionID, "[basictest] Starting tests", 5.0)<0) goto error; 99 printf("\n"); 100 76 101 printf("Querying the versions...\n"); 77 102 if (xplra_get_versions(connectionID, &xplaneVersion, … … 341 366 printf("The tail number is: '%s'\n\n", (char*)tailNum); 342 367 368 printf("Preparing for the message tests, sleeping for 5 seconds...\n"); 369 _sleep(5*1000); 370 371 printf("Showing a message for 10 seconds...\n"); 372 if (xplra_show_message(connectionID, "[basictest] this message appears for 10 seconds", 10.0)<0) { 373 goto error; 374 } 375 376 printf("Sleeping for 3 seconds...\n"); 377 _sleep(3*1000); 378 379 printf("Showing another message interrupting the previous one for 3 seconds"); 380 if (xplra_show_message(connectionID, "[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0)<0) { 381 goto error; 382 } 383 384 printf("Sleeping for 5 seconds...\n"); 385 _sleep(5*1000); 386 if (xplra_show_message(connectionID, "[basictest] and the tests come to an end!", 5.0)<0) { 387 goto error; 388 } 389 343 390 goto cleanup; 344 391 error: -
test/basictest.cc
r36 r40 31 31 #include <hu/varadiistvan/xplra/XPlane.h> 32 32 33 #include <hu/varadiistvan/scpl/Thread.h> 34 33 35 #include <cstdio> 34 36 #include <cstring> … … 39 41 using hu::varadiistvan::xplra::Exception; 40 42 using hu::varadiistvan::xplra::ProtocolException; 43 44 using hu::varadiistvan::scpl::Thread; 41 45 42 46 using std::string; … … 56 60 int xplmVersion = 0; 57 61 int xplraVersion = 0; 62 63 printf("Showing a message...\n"); 64 xplane.showMessage("[basictest] Starting tests", 5.0); 65 printf("\n"); 58 66 59 67 printf("Querying the versions...\n"); … … 242 250 } 243 251 252 printf("Preparing for the message tests, sleeping for 5 seconds...\n"); 253 Thread::sleep(5*1000); 254 255 printf("Showing a message for 10 seconds...\n"); 256 xplane.showMessage("[basictest] this message appears for 10 seconds", 10.0); 257 258 printf("Sleeping for 3 seconds...\n"); 259 Thread::sleep(3*1000); 260 261 printf("Showing another message interrupting the previous one for 3 seconds\n"); 262 xplane.showMessage("[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0); 263 264 printf("Sleeping for 5 seconds...\n"); 265 Thread::sleep(5*1000); 266 xplane.showMessage("[basictest] and the tests come to an end!", 5.0); 244 267 245 268 return 0; -
test/basictest.py
r36 r40 1 1 # Basic test program for the Python client library 2 3 #------------------------------------------------------------------------------ 4 5 import time 2 6 3 7 #------------------------------------------------------------------------------ … … 12 16 xplane.connect() 13 17 print "Connected to X-Plane." 18 print 19 20 print "Showing a message..." 21 xplane.showMessage("[basictest] Starting tests", 5.0) 14 22 print 15 23 … … 133 141 print 134 142 143 print "Preparing for the message tests, sleeping for 5 seconds..." 144 time.sleep(5.0) 145 146 print "Showing a message for 10 seconds..." 147 xplane.showMessage("[basictest] this message appears for 10 seconds", 10.0) 148 149 print "Sleeping for 3 seconds..." 150 time.sleep(3.0) 151 152 print "Showing another message interrupting the previous one for 3 seconds" 153 xplane.showMessage("[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0) 154 155 print "Sleeping for 5 seconds..." 156 time.sleep(5.0) 157 xplane.showMessage("[basictest] and the tests come to an end!", 5.0) 135 158 136 159 except Exception as e:
Note:
See TracChangeset
for help on using the changeset viewer.