Changeset 40:ec5dde8a6ff6 in xplra


Ignore:
Timestamp:
02/14/13 18:21:47 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Implemented the client support for the new commands and updated the basic test programs with tests showing messages

Files:
13 edited

Legend:

Unmodified
Added
Removed
  • src/client/c/hu/varadiistvan/xplra/Exception.cc

    r14 r40  
    6161//------------------------------------------------------------------------------
    6262
    63 ProtocolException::ProtocolException(errorCode_t errorCode) throw() :
    64     errorCode(errorCode)
     63ProtocolException::ProtocolException(errorCode_t errorCode, bool hasParameter,
     64                                     long parameter) throw() :
     65    errorCode(errorCode),
     66    hasParameter(parameter),
     67    parameter(parameter)
    6568{
    6669    switch (errorCode) {
     
    7073      case UNKNOWN_DATAREF:
    7174        message = "xplra::ProtocolException: unknown dataref";
     75        if (hasParameter) {
     76            char buf[32];
     77            snprintf(buf, sizeof(buf), " (# %ld)", parameter);
     78            message += buf;
     79        }
    7280        break;
    7381      case INVALID_TYPE:
     
    8593      case INVALID_ID:
    8694        message = "xplra::ProtocolException: invalid ID";
     95        break;
     96      case INVALID_DURATION:
     97        message = "xplra::ProtocolException: invalid duration";
    8798        break;
    8899      case OTHER:
  • src/client/c/hu/varadiistvan/xplra/Exception.h

    r19 r40  
    143143        INVALID_ID = 7,
    144144
     145        /// Invalid duration
     146        INVALID_DURATION = 8,
     147
    145148        /// Other error
    146149        OTHER = 255
     
    153156    errorCode_t errorCode;
    154157
     158    /**
     159     * Indicate if the parameter is valid.
     160     */
     161    bool hasParameter;
     162
     163    /**
     164     * Parameter of the error.
     165     */
     166    long parameter;
     167
    155168public:
    156169    /**
    157170     * Construct the exception.
    158171     */
    159     ProtocolException(errorCode_t errorCode) throw();
     172    ProtocolException(errorCode_t errorCode,
     173                      bool hasParameter = false, long parameter = 0) throw();
    160174
    161175    /**
     
    163177     */
    164178    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();
    165184};
    166185
     
    246265{
    247266    return errorCode;
     267}
     268
     269//------------------------------------------------------------------------------
     270
     271inline bool ProtocolException::getParameter(long& param) const throw()
     272{
     273    if (hasParameter) param = parameter;
     274    return hasParameter;
    248275}
    249276
  • src/client/c/hu/varadiistvan/xplra/MultiBuffer.cc

    r33 r40  
    610610
    611611    xplane.stream->flush();
    612     xplane.checkResult();
     612    xplane.checkResult(true);
    613613}
    614614
  • src/client/c/hu/varadiistvan/xplra/MultiGetter.cc

    r33 r40  
    9797    xplane.stream->writeU32(registeredID);
    9898    xplane.stream->flush();
    99     xplane.checkResult();
     99    xplane.checkResult(true);
    100100
    101101    readValues();
  • src/client/c/hu/varadiistvan/xplra/MultiSetter.cc

    r33 r40  
    9999    }
    100100    xplane.stream->flush();
    101     xplane.checkResult();
     101    xplane.checkResult(true);
    102102}
    103103
     
    121121    }
    122122    xplane.stream->flush();
    123     xplane.checkResult();
     123    xplane.checkResult(true);
    124124}
    125125
  • src/client/c/hu/varadiistvan/xplra/XPlane.cc

    r36 r40  
    7373//------------------------------------------------------------------------------
    7474
    75 void XPlane::checkResult(uint8_t result) throw(ProtocolException)
     75void XPlane::checkResult(uint8_t result, bool hasParameter, long parameter)
     76    throw(ProtocolException)
    7677{
    7778    switch(result) {
     
    7980        return;
    8081      case Protocol::RESULT_INVALID_COMMAND:
    81         throw ProtocolException(ProtocolException::INVALID_COMMAND);
     82        throw ProtocolException(ProtocolException::INVALID_COMMAND,
     83                                hasParameter, parameter);
    8284      case Protocol::RESULT_UNKNOWN_DATAREF:
    83         throw ProtocolException(ProtocolException::UNKNOWN_DATAREF);
     85        throw ProtocolException(ProtocolException::UNKNOWN_DATAREF,
     86                                hasParameter, parameter);
    8487      case Protocol::RESULT_INVALID_TYPE:
    85         throw ProtocolException(ProtocolException::INVALID_TYPE);
     88        throw ProtocolException(ProtocolException::INVALID_TYPE,
     89                                hasParameter, parameter);
    8690      case Protocol::RESULT_INVALID_LENGTH:
    87         throw ProtocolException(ProtocolException::INVALID_LENGTH);
     91        throw ProtocolException(ProtocolException::INVALID_LENGTH,
     92                                hasParameter, parameter);
    8893      case Protocol::RESULT_INVALID_OFFSET:
    89         throw ProtocolException(ProtocolException::INVALID_OFFSET);
     94        throw ProtocolException(ProtocolException::INVALID_OFFSET,
     95                                hasParameter, parameter);
    9096      case Protocol::RESULT_INVALID_COUNT:
    91         throw ProtocolException(ProtocolException::INVALID_COUNT);
     97        throw ProtocolException(ProtocolException::INVALID_COUNT,
     98                                hasParameter, parameter);
    9299      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);
    94105      case Protocol::RESULT_OTHER_ERROR:
    95106      default:
    96         throw ProtocolException(ProtocolException::OTHER);
     107        throw ProtocolException(ProtocolException::OTHER,
     108                                hasParameter, parameter);
    97109    }
    98110}
     
    100112//------------------------------------------------------------------------------
    101113
    102 void XPlane::checkResult() throw(ProtocolException, IOException)
     114void XPlane::checkResult(bool multi) throw(ProtocolException, IOException)
    103115{
    104116    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);
    107127}
    108128
     
    205225
    206226    checkStream();
     227}
     228
     229//------------------------------------------------------------------------------
     230
     231void XPlane::reloadPlugins() throw(Exception)
     232{
     233    stream->writeU8(Protocol::COMMAND_RELOAD_PLUGINS);
     234    stream->flush();
     235    checkResult();
    207236}
    208237
     
    457486    }
    458487}
     488
     489//------------------------------------------------------------------------------
     490
     491void 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
    459500//------------------------------------------------------------------------------
    460501
  • src/client/c/hu/varadiistvan/xplra/XPlane.h

    r36 r40  
    150150
    151151    /**
     152     * Reload the plugins loaded in X-Plane. After this the connection
     153     * fails.
     154     */
     155    void reloadPlugins() throw(Exception);
     156
     157    /**
    152158     * Get the integer value of the dataref with the given name.
    153159     */
     
    271277                   size_t offset = 0) throw(Exception);
    272278
     279    /**
     280     * Show a textual message for a certain duration.
     281     */
     282    void showMessage(const char* message, float duration) throw(Exception);
     283
    273284private:
    274285    /**
     
    281292     * throw a ProtocolException with the correct error code.
    282293     */
    283     void checkResult(uint8_t result) throw(ProtocolException);
     294    void checkResult(uint8_t result, bool hasParameter = false,
     295                     long parameter = 0) throw(ProtocolException);
    284296
    285297    /**
     
    288300     * is some problem with the stream, an IOException is thrown.
    289301     */
    290     void checkResult() throw(ProtocolException, IOException);
     302    void checkResult(bool multi = false) throw(ProtocolException, IOException);
    291303
    292304    /**
  • src/client/c/hu/varadiistvan/xplra/xplra.cc

    r36 r40  
    515515//------------------------------------------------------------------------------
    516516
     517extern "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
    517532extern "C" int xplra_get_int(int* value, int connectionID, const char* name)
    518533{
     
    12671282    try {
    12681283        return connection->destroyMultiBuffer(bufferID) ? 0 : -1;
     1284    } catch(...) {
     1285        connection->handleException();
     1286        return -1;
     1287    }
     1288}
     1289
     1290//------------------------------------------------------------------------------
     1291//------------------------------------------------------------------------------
     1292
     1293extern "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;
    12691302    } catch(...) {
    12701303        connection->handleException();
  • src/client/c/hu/varadiistvan/xplra/xplra.h

    r36 r40  
    7979#define ERROR_PROTOCOL_INVALID_ID = 7
    8080
     81/** An invalid duration was specified */
     82#define ERROR_PROTOCOL_INVALID_DURATION = 8
     83
    8184/** Other protocol error  */
    82 #define ERROR_PROTOCOL_OTHER = 8
     85#define ERROR_PROTOCOL_OTHER = 255
    8386
    8487/** A function requiring a connection is called without a connection */
     
    137140                       int* xplaneVersion, int* xplmVersion,
    138141                       int* xplraVersion);
     142
     143/*----------------------------------------------------------------------------*/
     144
     145/**
     146 * Reload the plugins in X-Plane. After this the connection fails.
     147 */
     148int xplra_reload_plugins(int connectionID);
    139149
    140150/*----------------------------------------------------------------------------*/
     
    720730
    721731/**
    722  * Destruy a multi-dataref buffer for the given connection.
     732 * Destroy a multi-dataref buffer for the given connection.
    723733 *
    724734 * @param connectionID the ID of the connection for which the buffer
     
    729739int xplra_multi_destroy_buffer(int connectionID, int bufferID);
    730740
     741/*----------------------------------------------------------------------------*/
     742/*----------------------------------------------------------------------------*/
     743
     744/**
     745 * Show a message in the simulator window for the given duration.
     746 */
     747int xplra_show_message(int connectionID, const char* message, float duration);
     748
     749/*----------------------------------------------------------------------------*/
    731750/*----------------------------------------------------------------------------*/
    732751
  • src/client/python/xplra.py

    r37 r40  
    3030COMMAND_GET_VERSIONS = 0x31
    3131
     32COMMAND_RELOAD_PLUGINS = 0x32
     33
     34COMMAND_SHOW_MESSAGE = 0x41
     35
    3236TYPE_INT = 0x01
    3337
     
    5761
    5862RESULT_INVALID_ID = 0x07
     63
     64RESULT_INVALID_DURATION = 0x08
    5965
    6066RESULT_OTHER_ERROR = 0xff
     
    7177                 RESULT_INVALID_COUNT : "invalid count",
    7278                 RESULT_INVALID_ID : "invalid ID",
     79                 RESULT_INVALID_DURATION : "invalid duration",
    7380                 RESULT_OTHER_ERROR : "other error" }
    7481
     
    8188            return "unknown error code " + `resultCode`
    8289
    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)
    8697        self.resultCode = resultCode
     98        self.parameter = parameter
    8799
    88100#-------------------------------------------------------------------------------
     
    145157        return (self._readS32(), self._readS32(), self._readS32())
    146158
     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
    147167    def getInt(self, name):
    148168        """Get the value of the integer dataref with the given name."""
     
    212232        self.setByteArray(name, value, offset)
    213233
     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
    214244    def disconnect(self):
    215245        """Disconnect from X-Plane."""
     
    218248            self._stream = None
    219249
    220     def _checkResult(self, resultCode = None):
     250    def _checkResult(self, resultCode = None, parameter = None, multi = False):
    221251        """Check the given result code.
    222252
     
    227257        if resultCode is None:
    228258            resultCode = self._readU8()
     259            if multi and resultCode==RESULT_UNKNOWN_DATAREF:
     260                parameter = self._readU32()
     261
    229262        if resultCode!=RESULT_OK:
    230             raise ProtocolException(resultCode)
     263            raise ProtocolException(resultCode, parameter)
    231264
    232265    def _getSingle(self, name, type, length = None, offset = None):
     
    525558
    526559        self._xplane._flush()
    527         self._xplane._checkResult()
     560        self._xplane._checkResult(multi = True)
    528561
    529562    def __len__(self):
     
    591624        self._xplane._flush()
    592625
    593         self._xplane._checkResult()
     626        self._xplane._checkResult(multi = True)
    594627
    595628        self._readValues()
     
    623656        self._xplane._flush()
    624657
    625         self._xplane._checkResult()
     658        self._xplane._checkResult(multi = True)
    626659
    627660    def _executeUnregistered(self):
     
    644677
    645678        self._xplane._flush()
    646         self._xplane._checkResult()
    647 
    648 #-------------------------------------------------------------------------------
     679        self._xplane._checkResult(multi = True)
     680
     681#-------------------------------------------------------------------------------
  • test/basicctest.c

    r36 r40  
    3333#include <stdio.h>
    3434#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
     45void _sleep(int ms)
     46{
     47    Sleep(ms);
     48}
     49#else
     50void _sleep(int ms)
     51{
     52    usleep(ms*1000);
     53}
     54#endif
     55
    3556
    3657//------------------------------------------------------------------------------
     
    7495    }
    7596
     97    printf("Showing a message...\n");
     98    if (xplra_show_message(connectionID, "[basictest] Starting tests", 5.0)<0) goto error;
     99    printf("\n");
     100
    76101    printf("Querying the versions...\n");
    77102    if (xplra_get_versions(connectionID, &xplaneVersion,
     
    341366    printf("The tail number is: '%s'\n\n", (char*)tailNum);
    342367
     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
    343390    goto cleanup;
    344391error:
  • test/basictest.cc

    r36 r40  
    3131#include <hu/varadiistvan/xplra/XPlane.h>
    3232
     33#include <hu/varadiistvan/scpl/Thread.h>
     34
    3335#include <cstdio>
    3436#include <cstring>
     
    3941using hu::varadiistvan::xplra::Exception;
    4042using hu::varadiistvan::xplra::ProtocolException;
     43
     44using hu::varadiistvan::scpl::Thread;
    4145
    4246using std::string;
     
    5660        int xplmVersion = 0;
    5761        int xplraVersion = 0;
     62
     63        printf("Showing a message...\n");
     64        xplane.showMessage("[basictest] Starting tests", 5.0);
     65        printf("\n");
    5866
    5967        printf("Querying the versions...\n");
     
    242250        }
    243251
     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);
    244267
    245268        return 0;
  • test/basictest.py

    r36 r40  
    11# Basic test program for the Python client library
     2
     3#------------------------------------------------------------------------------
     4
     5import time
    26
    37#------------------------------------------------------------------------------
     
    1216        xplane.connect()
    1317        print "Connected to X-Plane."
     18        print
     19
     20        print "Showing a message..."
     21        xplane.showMessage("[basictest] Starting tests", 5.0)
    1422        print
    1523
     
    133141        print
    134142
     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)
    135158
    136159    except Exception as e:
Note: See TracChangeset for help on using the changeset viewer.