Changeset 14:bb6484eceb1e in xplra for src


Ignore:
Timestamp:
01/27/13 13:26:56 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Very basic client code works

Location:
src/client/c/hu/varadiistvan/xplra
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/client/c/hu/varadiistvan/xplra/Makefile.am

    r13 r14  
     1AM_CXXFLAGS=$(VSCPL_CFLAGS) -I$(top_srcdir)/src/plugin/src
    12
    23lib_LTLIBRARIES=libxplra.la
    34
    45libxplra_la_SOURCES=\
    5         XPlane.cc
     6        XPlane.cc       \
     7        Exception.cc    \
     8        xplra.cc
     9
     10libxplra_la_LIBADD=$(VSCPL_LIBS)
    611
    712if TARGET_API_WIN32
     
    1116include_xplradir=$(includedir)/hu/varadiistvan/xplra
    1217include_xplra_HEADERS=\
    13         XPlane.h
     18        XPlane.h        \
     19        Exception.h     \
     20        xplra.h
  • src/client/c/hu/varadiistvan/xplra/XPlane.cc

    r13 r14  
    3131#include "XPlane.h"
    3232
     33#include <hu/varadiistvan/scpl/io/LocalClientSocket.h>
     34#include <hu/varadiistvan/scpl/io/DataStream.h>
     35
     36#include <xplra/Protocol.h>
     37
     38#include <memory>
     39
    3340//------------------------------------------------------------------------------
    3441
    3542using hu::varadiistvan::xplra::XPlane;
    3643
     44using hu::varadiistvan::scpl::io::LocalClientSocket;
     45using hu::varadiistvan::scpl::io::LocalConnector;
     46using hu::varadiistvan::scpl::io::DataStream;
     47
     48using xplra::Protocol;
     49
     50using std::auto_ptr;
     51
    3752//------------------------------------------------------------------------------
     53
     54inline void XPlane::checkStream() throw(NotConnectedException, IOException)
     55{
     56    if (stream==0) {
     57        throw NotConnectedException();
     58    } else if (stream->failed()) {
     59        throw IOException(stream->getErrorCode());
     60    }
     61}
     62
     63//------------------------------------------------------------------------------
     64
     65void XPlane::checkResult(uint8_t result) throw(ProtocolException)
     66{
     67    switch(result) {
     68      case Protocol::RESULT_OK:
     69        return;
     70      case Protocol::RESULT_INVALID_COMMAND:
     71        throw ProtocolException(ProtocolException::INVALID_COMMAND);
     72      case Protocol::RESULT_UNKNOWN_DATAREF:
     73        throw ProtocolException(ProtocolException::UNKNOWN_DATAREF);
     74      case Protocol::RESULT_INVALID_TYPE:
     75        throw ProtocolException(ProtocolException::INVALID_TYPE);
     76      case Protocol::RESULT_INVALID_LENGTH:
     77        throw ProtocolException(ProtocolException::INVALID_LENGTH);
     78      case Protocol::RESULT_INVALID_OFFSET:
     79        throw ProtocolException(ProtocolException::INVALID_OFFSET);
     80      case Protocol::RESULT_INVALID_COUNT:
     81        throw ProtocolException(ProtocolException::INVALID_COUNT);
     82      case Protocol::RESULT_INVALID_ID:
     83        throw ProtocolException(ProtocolException::INVALID_ID);
     84      case Protocol::RESULT_OTHER_ERROR:
     85      default:
     86        throw ProtocolException(ProtocolException::OTHER);
     87    }
     88}
     89
     90//------------------------------------------------------------------------------
     91//------------------------------------------------------------------------------
     92
     93XPlane::~XPlane() throw()
     94{
     95    disconnect();
     96}
     97
     98//------------------------------------------------------------------------------
     99
     100void XPlane::connect() throw(IOException)
     101{
     102    if (socket!=0) return;
     103
     104    auto_ptr<LocalClientSocket> clientSocket(new LocalClientSocket("xplra",
     105                                                                   &waiter));
     106    LocalConnector& connector = clientSocket->getConnector();
     107
     108    while (!connector.connect()) {
     109        if (connector.failed()) {
     110            throw IOException(connector.getErrorCode());
     111        }
     112        waiter.wait();
     113        if (waiter.failed()) {
     114            throw IOException(waiter.getErrorCode());
     115        }
     116    }
     117
     118    socket = clientSocket.release();
     119    stream = new DataStream(*socket);
     120}
     121
     122//------------------------------------------------------------------------------
     123
     124void XPlane::disconnect() throw()
     125{
     126    if (socket==0) return;
     127
     128    delete stream; stream = 0;
     129    delete socket; socket = 0;
     130}
     131
     132//------------------------------------------------------------------------------
     133
     134int XPlane::getInt(const char* name) throw(Exception)
     135{
     136    checkStream();
     137
     138    stream->writeU8(Protocol::COMMAND_GET_SINGLE);
     139    stream->writeString(name, strlen(name));
     140    stream->writeU8(Protocol::TYPE_INT);
     141    if (!stream->flush()) checkStream();
     142
     143    uint8_t result = stream->readU8();
     144    checkStream();
     145    checkResult(result);
     146
     147    int value = stream->readS32();
     148    checkStream();
     149    return value;
     150}
     151
    38152//------------------------------------------------------------------------------
    39153
  • src/client/c/hu/varadiistvan/xplra/XPlane.h

    r13 r14  
    2929#ifndef HU_VARADIISTVAN_XPLRA_XPLANE_H
    3030#define HU_VARADIISTVAN_XPLRA_XPLANE_H
     31//-----------------------------------------------------------------------------
     32
     33#include "Exception.h"
     34
     35#include <hu/varadiistvan/scpl/io/Waiter.h>
     36
     37#include <inttypes.h>
     38
     39//-----------------------------------------------------------------------------
     40
     41namespace hu { namespace varadiistvan { namespace scpl { namespace io {
     42
     43class LocalClientSocket;
     44class DataStream;
     45
     46} /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
     47
    3148//------------------------------------------------------------------------------
    3249
     
    3552//------------------------------------------------------------------------------
    3653
     54/**
     55 * The main class to access X-Plane.
     56 *
     57 * The calls are synchronous and not thread-safe.
     58 */
    3759class XPlane
    3860{
     61private:
     62    /**
     63     * The waiter to use.
     64     */
     65    scpl::io::Waiter waiter;
     66
     67    /**
     68     * The local client socket over which we are communicating with X-Plane.
     69     */
     70    scpl::io::LocalClientSocket* socket;
     71
     72    /**
     73     * The data stream to handle the data conversions.
     74     */
     75    scpl::io::DataStream* stream;
     76
     77public:
     78    /**
     79     * Construct the object. It will not be connected to the simulator
     80     * yet.
     81     */
     82    XPlane() throw();
     83
     84    /**
     85     * Destroy the object. If connected, the connection will be
     86     * closed.
     87     */
     88    ~XPlane() throw();
     89
     90    /**
     91     * Connect to the simulator.
     92     */
     93    void connect() throw(IOException);
     94
     95    /**
     96     * Check if we are connected to the simulator.
     97     */
     98    bool isConnected() const throw();
     99
     100    /**
     101     * Disconnect from the simulator.
     102     */
     103    void disconnect() throw();
     104
     105    /**
     106     * Get the integer value of the dataref with the given name.
     107     */
     108    int getInt(const char* name) throw(Exception);
     109
     110private:
     111    /**
     112     * Check if we have a stream and if it has not failed.
     113     */
     114    void checkStream() throw(NotConnectedException, IOException);
     115
     116    /**
     117     * Check the given protocol result. If it signifies an error,
     118     * throw a ProtocolException with the correct error code.
     119     */
     120    void checkResult(uint8_t result) throw(ProtocolException);
    39121};
     122
     123//------------------------------------------------------------------------------
     124// Inline definitions
     125//------------------------------------------------------------------------------
     126
     127inline XPlane::XPlane() throw() :
     128    socket(0),
     129    stream(0)
     130{
     131}
     132
     133//------------------------------------------------------------------------------
     134
     135inline bool XPlane::isConnected() const throw()
     136{
     137    return socket!=0;
     138}
    40139
    41140//------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.