Changeset 9:953cd606427c in xplra


Ignore:
Timestamp:
01/04/13 16:12:21 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 3 'hg:/home/ivaradi/xplane/hg/xplra' '/'>, 'public')
Message:

Registering multi-data update requests works

Files:
3 added
8 edited

Legend:

Unmodified
Added
Removed
  • misc/client.py

    r8 r9  
    100100        self._stream = stream
    101101        self._multiGets = {}
     102        self._multiSets = {}
    102103
    103104        self.use_rawinput = True
     
    230231
    231232    def do_exec_multiget(self, args):
    232         """Handle the 'unreg_multiget' command"""
     233        """Handle the 'exec_multiget' command"""
    233234        words = self._splitArgs(args)
    234235        if len(words)<1:
     
    254255            print >> sys.stderr, "Invalid dataref at #%d" % (index,)
    255256        else:
     257            print >> sys.stderr, "Error code:", result
     258
     259    def do_reg_multiset(self, args):
     260        """Handle the 'reg_multiset' command"""
     261        words = self._splitArgs(args)
     262        specs = []
     263        while words:
     264            result = self._parseGetSpec(words)
     265            if result is None:
     266                return False
     267            else:
     268                specs.append(result[0])
     269                words = result[1]
     270
     271        if not specs:
     272            return False
     273
     274        self._writeU8(0x21)
     275        self._writeU32(len(specs))
     276        for (name, type, length, offset) in specs:
     277            print name, type
     278            self._writeString(name)
     279            self._writeU8(self._types[type])
     280            if length is not None:
     281                self._writeS32(length)
     282                self._writeS32(offset)
     283        self._flush()
     284
     285        result = self._readU8()
     286        if result==0:
     287            id = self._readU32()
     288            self._multiSets[id] = \
     289                [(type, length) for (name, type, length, offset) in specs]
     290            print "ID:", id
     291        else:
     292            print >> sys.stderr, "Error code:", result
     293
     294    def do_unreg_multiset(self, args):
     295        """Handle the 'unreg_multiset' command"""
     296        words = self._splitArgs(args)
     297        if len(words)<1:
     298            print >> sys.stderr, "Missing parameter"
     299            return False
     300
     301        id = int(words[0])
     302
     303        self._writeU8(0x22)
     304        self._writeU32(id)
     305        self._flush()
     306
     307        result = self._readU8()
     308        if result!=0:
     309            print >> sys.stderr, "Error code:", result
     310
     311        if id in self._multiSets:
     312            del self._multiSets[id]
     313
     314    def do_exec_multiset(self, args):
     315        """Handle the 'exec_multiget' command"""
     316        words = self._splitArgs(args)
     317        if len(words)<1:
     318            print >> sys.stderr, "Missing parameter"
     319            return False
     320
     321        id = int(words[0])
     322        if id not in self._multiSets:
     323            print >> sys.stderr, "Invalid ID"
     324            return False
     325
     326        words = words[1:]
     327        types = self._multiSets[id]
     328        if len(words)<len(types):
     329            print >> sys.stderr, "Missing parameter"
     330            return False
     331
     332
     333        self._writeU8(0x23)
     334        self._writeU32(id)
     335        for ((type, length), word) in zip(types, words):
     336            self._writeValue(type, word, length)
     337        self._flush()
     338
     339        result = self._readU8()
     340        if result==0x02:
     341            index = self._readU32()
     342            print >> sys.stderr, "Invalid dataref at #%d" % (index,)
     343        elif result!=0:
    256344            print >> sys.stderr, "Error code:", result
    257345
  • src/xplra/Makefile.am

    r8 r9  
    3333        SetDataRefTask.cc               \
    3434        MultiTaskRequest.cc             \
    35         GetMultiDataRefRequest.cc
     35        GetMultiDataRefRequest.cc       \
     36        SetMultiDataRefRequest.cc
    3637
    3738libxplra_la_LIBADD=$(LIBXPLCOMMON_LIBS)
     
    4950        SetDataRefTask.h                \
    5051        MultiTaskRequest.h              \
    51         GetMultiDataRefRequest.h
     52        GetMultiDataRefRequest.h        \
     53        SetMultiDataRefRequest.h
  • src/xplra/Protocol.h

    r8 r9  
    6565     */
    6666    static const uint8_t COMMAND_EXECUTE_GET_MULTI = 0x13;
     67
     68    /**
     69     * Command: register a multiple-data update request
     70     */
     71    static const uint8_t COMMAND_REGISTER_SET_MULTI = 0x21;
     72
     73    /**
     74     * Command: unregister a multiple-data update request
     75     */
     76    static const uint8_t COMMAND_UNREGISTER_SET_MULTI = 0x22;
     77
     78    /**
     79     * Command: execute a registered multiple-data update request
     80     */
     81    static const uint8_t COMMAND_EXECUTE_SET_MULTI = 0x23;
    6782
    6883    /**
  • src/xplra/ServerThread.cc

    r8 r9  
    3737#include "TaskRequest.h"
    3838#include "GetMultiDataRefRequest.h"
     39#include "SetMultiDataRefRequest.h"
    3940
    4041#include <xplcommon/Util.h>
     
    7677    bufferedStream(acceptor.getSocket(&waiter)),
    7778    stream(*bufferedStream),
    78     nextGetMultiRequestID(1)
     79    nextGetMultiRequestID(1),
     80    nextSetMultiRequestID(1)
    7981{
    8082    instancesMutex.lock();
     
    9193    for(getMultiRequests_t::iterator i = getMultiRequests.begin();
    9294        i!=getMultiRequests.end(); ++i)
     95    {
     96        delete i->second;
     97    }
     98
     99    for(setMultiRequests_t::iterator i = setMultiRequests.begin();
     100        i!=setMultiRequests.end(); ++i)
    93101    {
    94102        delete i->second;
     
    129137        } else if (command==Protocol::COMMAND_EXECUTE_GET_MULTI) {
    130138            if (!handleExecuteGetMulti()) break;
     139        } else if (command==Protocol::COMMAND_REGISTER_SET_MULTI) {
     140            if (!handleRegisterSetMulti()) break;
     141        } else if (command==Protocol::COMMAND_UNREGISTER_SET_MULTI) {
     142            if (!handleUnregisterSetMulti()) break;
     143        } else if (command==Protocol::COMMAND_EXECUTE_SET_MULTI) {
     144            if (!handleExecuteSetMulti()) break;
    131145        } else {
    132146            stream.writeU8(Protocol::RESULT_INVALID_COMMAND);
     
    177191        stream.writeU8(result);
    178192        return true;
     193    }
     194
     195    task->readValue(stream);
     196    if (!stream) {
     197        delete task;
     198        return false;
    179199    }
    180200
     
    258278//------------------------------------------------------------------------------
    259279
     280bool ServerThread::handleRegisterSetMulti()
     281{
     282    uint8_t result = Protocol::RESULT_OK;
     283    SetMultiDataRefRequest* request =
     284        new SetMultiDataRefRequest(result, stream);
     285    if (result!=Protocol::RESULT_OK || !stream) {
     286        delete request;
     287        stream.writeU8(result);
     288        return stream;
     289    }
     290
     291    size_t id = nextSetMultiRequestID++;
     292    setMultiRequests[id] = request;
     293
     294    stream.writeU8(Protocol::RESULT_OK);
     295    stream.writeU32(id);
     296
     297    return true;
     298}
     299
     300//------------------------------------------------------------------------------
     301
     302bool ServerThread::handleUnregisterSetMulti()
     303{
     304    uint32_t id = stream.readU32();
     305    if (!stream) return false;
     306
     307    setMultiRequests_t::iterator i = setMultiRequests.find(id);
     308    if (i==setMultiRequests.end()) {
     309        stream.writeU8(Protocol::RESULT_INVALID_ID);
     310    } else {
     311        SetMultiDataRefRequest* request = i->second;
     312        setMultiRequests.erase(i);
     313        delete request;
     314        stream.writeU8(Protocol::RESULT_OK);
     315    }
     316
     317    return true;
     318}
     319
     320//------------------------------------------------------------------------------
     321
     322bool ServerThread::handleExecuteSetMulti()
     323{
     324    uint32_t id = stream.readU32();
     325    if (!stream) return false;
     326
     327    setMultiRequests_t::iterator i = setMultiRequests.find(id);
     328    if (i==setMultiRequests.end()) {
     329        stream.writeU8(Protocol::RESULT_INVALID_ID);
     330    } else {
     331        SetMultiDataRefRequest* request = i->second;
     332        request->readValues(stream);
     333        if (!stream || !requestQueue.execute(request)) return false;
     334        request->writeResult(stream);
     335    }
     336
     337    return true;
     338}
     339
     340//------------------------------------------------------------------------------
     341
    260342// Local Variables:
    261343// mode: C++
  • src/xplra/ServerThread.h

    r8 r9  
    5252
    5353class GetMultiDataRefRequest;
     54class SetMultiDataRefRequest;
    5455
    5556//------------------------------------------------------------------------------
     
    6768
    6869    /**
    69      * Type for the registered multiple-data requests.
     70     * Type for the registered multiple-data query requests.
    7071     */
    7172    typedef std::map<size_t, GetMultiDataRefRequest*> getMultiRequests_t;
    7273
    7374    /**
     75     * Type for the registered multiple-data update requests.
     76     */
     77    typedef std::map<size_t, SetMultiDataRefRequest*> setMultiRequests_t;
     78
     79    /**
    7480     * A mutex to protect the collection of server threads.
    7581     */
     
    117123     */
    118124    getMultiRequests_t getMultiRequests;
     125
     126    /**
     127     * The ID of the next multiple-data update request.
     128     */
     129    size_t nextSetMultiRequestID;
     130
     131    /**
     132     * The registered multiple-data query requests.
     133     */
     134    setMultiRequests_t setMultiRequests;
    119135
    120136public:
     
    176192     */
    177193    bool handleExecuteGetMulti();
     194
     195    /**
     196     * Handle the COMMAND_REGISTER_SET_MULTI command
     197     *
     198     * @return true, if we can continue, false if the thread should quit
     199     */
     200    bool handleRegisterSetMulti();
     201
     202    /**
     203     * Handle the COMMAND_UNREGISTER_SET_MULTI command
     204     *
     205     * @return true, if we can continue, false if the thread should quit
     206     */
     207    bool handleUnregisterSetMulti();
     208
     209    /**
     210     * Handle the COMMAND_EXECUTE_SET_MULTI command
     211     *
     212     * @return true, if we can continue, false if the thread should quit
     213     */
     214    bool handleExecuteSetMulti();
    178215};
    179216
  • src/xplra/SetDataRefTask.cc

    r7 r9  
    5555
    5656    if (type==Protocol::TYPE_INT) {
    57         int value = stream.readS32();
    58         if (stream) return new SetIntDataRefTask(name, value);
     57        return new SetIntDataRefTask(name);
    5958    } else if (type==Protocol::TYPE_FLOAT) {
    60         float value = stream.readFloat();
    61         if (stream) return new SetFloatDataRefTask(name, value);
     59        return new SetFloatDataRefTask(name);
    6260    } else if (type==Protocol::TYPE_DOUBLE) {
    63         double value = stream.readDouble();
    64         if (stream) return new SetDoubleDataRefTask(name, value);
     61        return new SetDoubleDataRefTask(name);
    6562    } else if (type==Protocol::TYPE_FLOAT_ARRAY ||
    6663               type==Protocol::TYPE_INT_ARRAY ||
     
    7976        }
    8077
    81         SetDataRefTask* task = 0;
    8278        if (type==Protocol::TYPE_INT_ARRAY) {
    83             task = new SetIntArrayDataRefTask(name, length, offset);
     79            return new SetIntArrayDataRefTask(name, length, offset);
    8480        } else if (type==Protocol::TYPE_FLOAT_ARRAY) {
    85             task = new SetFloatArrayDataRefTask(name, length, offset);
     81            return new SetFloatArrayDataRefTask(name, length, offset);
    8682        } else {
    87             task = new SetByteArrayDataRefTask(name, length, offset);
    88         }
    89 
    90         task->readValue(stream);
    91 
    92         if (stream) {
    93             return task;
    94         } else {
    95             delete task;
     83            return new SetByteArrayDataRefTask(name, length, offset);
    9684        }
    9785    } else {
  • src/xplra/SetDataRefTask.h

    r7 r9  
    107107     * dataref with the given name.
    108108     */
    109     SetScalarDataRefTask(const std::string& name, T value);
     109    SetScalarDataRefTask(const std::string& name);
    110110
    111111    /**
     
    113113     * dataref.
    114114     */
    115     SetScalarDataRefTask(XPLMDataRef dataRef, T value);
     115    SetScalarDataRefTask(XPLMDataRef dataRef);
    116116
    117117protected:
     
    140140     * Construct the task for the dataref with the given name.
    141141     */
    142     SetIntDataRefTask(const std::string& name, int value);
     142    SetIntDataRefTask(const std::string& name);
    143143
    144144    /**
    145145     * Construct the task for the given dataref.
    146146     */
    147     SetIntDataRefTask(XPLMDataRef dataRef, int value);
     147    SetIntDataRefTask(XPLMDataRef dataRef);
    148148
    149149    /**
     
    171171     * Construct the task for the dataref with the given name.
    172172     */
    173     SetFloatDataRefTask(const std::string& name, float value);
     173    SetFloatDataRefTask(const std::string& name);
    174174
    175175    /**
    176176     * Construct the task for the given dataref.
    177177     */
    178     SetFloatDataRefTask(XPLMDataRef dataRef, float value);
     178    SetFloatDataRefTask(XPLMDataRef dataRef);
    179179
    180180    /**
     
    202202     * Construct the task for the dataref with the given name.
    203203     */
    204     SetDoubleDataRefTask(const std::string& name, double value);
     204    SetDoubleDataRefTask(const std::string& name);
    205205
    206206    /**
    207207     * Construct the task for the given dataref.
    208208     */
    209     SetDoubleDataRefTask(XPLMDataRef dataRef, double value);
     209    SetDoubleDataRefTask(XPLMDataRef dataRef);
    210210
    211211    /**
     
    365365template <typename T, class ConcreteClass>
    366366inline SetScalarDataRefTask<T, ConcreteClass>::
    367 SetScalarDataRefTask(const std::string& name, T value) :
    368     SetDataRefTask(name),
    369     value(value)
     367SetScalarDataRefTask(const std::string& name) :
     368    SetDataRefTask(name)
    370369{
    371370}
     
    375374template <typename T, class ConcreteClass>
    376375inline SetScalarDataRefTask<T, ConcreteClass>::
    377 SetScalarDataRefTask(XPLMDataRef dataRef, T value) :
    378     SetDataRefTask(dataRef),
    379     value(value)
     376SetScalarDataRefTask(XPLMDataRef dataRef) :
     377    SetDataRefTask(dataRef)
    380378{
    381379}
     
    465463//------------------------------------------------------------------------------
    466464
    467 inline SetIntDataRefTask::SetIntDataRefTask(const std::string& name, int value) :
    468     SetScalarDataRefTask<int, SetIntDataRefTask>(name, value)
    469 {
    470 }
    471 
    472 //------------------------------------------------------------------------------
    473 
    474 inline SetIntDataRefTask::SetIntDataRefTask(XPLMDataRef dataRef, int value) :
    475     SetScalarDataRefTask<int, SetIntDataRefTask>(dataRef, value)
     465inline SetIntDataRefTask::SetIntDataRefTask(const std::string& name) :
     466    SetScalarDataRefTask<int, SetIntDataRefTask>(name)
     467{
     468}
     469
     470//------------------------------------------------------------------------------
     471
     472inline SetIntDataRefTask::SetIntDataRefTask(XPLMDataRef dataRef) :
     473    SetScalarDataRefTask<int, SetIntDataRefTask>(dataRef)
    476474{
    477475}
     
    487485//------------------------------------------------------------------------------
    488486
    489 inline SetFloatDataRefTask::SetFloatDataRefTask(const std::string& name,
    490                                                 float value) :
    491     SetScalarDataRefTask<float, SetFloatDataRefTask>(name, value)
    492 {
    493 }
    494 
    495 //------------------------------------------------------------------------------
    496 
    497 inline SetFloatDataRefTask::SetFloatDataRefTask(XPLMDataRef dataRef,
    498                                                 float value) :
    499     SetScalarDataRefTask<float, SetFloatDataRefTask>(dataRef, value)
     487inline SetFloatDataRefTask::SetFloatDataRefTask(const std::string& name) :
     488    SetScalarDataRefTask<float, SetFloatDataRefTask>(name)
     489{
     490}
     491
     492//------------------------------------------------------------------------------
     493
     494inline SetFloatDataRefTask::SetFloatDataRefTask(XPLMDataRef dataRef) :
     495    SetScalarDataRefTask<float, SetFloatDataRefTask>(dataRef)
    500496{
    501497}
     
    511507//------------------------------------------------------------------------------
    512508
    513 inline SetDoubleDataRefTask::SetDoubleDataRefTask(const std::string& name,
    514                                                   double value) :
    515     SetScalarDataRefTask<double, SetDoubleDataRefTask>(name, value)
    516 {
    517 }
    518 
    519 //------------------------------------------------------------------------------
    520 
    521 inline SetDoubleDataRefTask::SetDoubleDataRefTask(XPLMDataRef dataRef,
    522                                                   double value) :
    523     SetScalarDataRefTask<double, SetDoubleDataRefTask>(dataRef, value)
     509inline SetDoubleDataRefTask::SetDoubleDataRefTask(const std::string& name) :
     510    SetScalarDataRefTask<double, SetDoubleDataRefTask>(name)
     511{
     512}
     513
     514//------------------------------------------------------------------------------
     515
     516inline SetDoubleDataRefTask::SetDoubleDataRefTask(XPLMDataRef dataRef) :
     517    SetScalarDataRefTask<double, SetDoubleDataRefTask>(dataRef)
    524518{
    525519}
  • src/xplra/plugin.cc

    r1 r9  
    3232
    3333#include <xplcommon/Util.h>
     34#include <xplcommon/config.h>
    3435
    3536#include <XPLMDefs.h>
     
    4041#include <cstring>
    4142#include <cstdarg>
     43
     44#if TARGET_API_POSIX
     45#include <signal.h>
     46#endif
    4247
    4348//------------------------------------------------------------------------------
     
    6469    strcpy(outSignature, "hu.varadiistvan.xplra");
    6570    strcpy(outDescription, "Provides remote access to datarefs");
     71
     72#if TARGET_API_POSIX
     73    signal(SIGPIPE, SIG_IGN);
     74#endif
    6675
    6776    return 1;
Note: See TracChangeset for help on using the changeset viewer.