Changeset 40:ec5dde8a6ff6 in xplra for src/client/c


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

Location:
src/client/c/hu/varadiistvan/xplra
Files:
9 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
Note: See TracChangeset for help on using the changeset viewer.