Changeset 6:8dd4ca9966d0 in xplra


Ignore:
Timestamp:
01/03/13 19:09:46 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Made all the single query operation work for all types

Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • misc/client.py

    • Property exe set to *
    r3 r6  
    11#!/usr/bin/env python
    22
     3import sys
    34import cmd
    45import struct
     
    4243        self.use_rawinput = True
    4344        self.intro = "\nX-Plane Remote Access plugin command prompt\n"
    44         self.prompt = "XPLRA>"
     45        self.prompt = "XPLRA> "
    4546
    4647        self.daemon = True
     
    5051        if line=="EOF":
    5152            print
    52             return self.do_quit("")
     53            return True
    5354        else:
    5455            return super(CLI, self).default(line)
     
    5859        words = args.split()
    5960        if len(words)<2:
    60             print >> sys.stderr, "missing parameters"
     61            print >> sys.stderr, "Missing parameters"
    6162            return False
    6263
     
    6566        type = words[1]
    6667        if type not in self._types:
    67             print >> sys.stderr, "invalid type"
     68            print >> sys.stderr, "Invalid type"
    6869            return False
    6970
    7071        length = None
    71         if len(words)>2:
     72        offset = None
     73        if len(words)>3:
    7274            length = int(words[2])
     75            offset = int(words[3])
    7376
    7477        self._writeU8(0x01)
     
    7780        if length is not None:
    7881            self._writeS32(length)
     82            self._writeS32(offset)
    7983        self._flush()
    8084
    8185        result = self._readU8()
    82         print "result:", result
    8386        if result==0:
     87            value = None
    8488            if type=="i":
    8589                value = self._readS32()
     
    9296                if length>0:
    9397                    value = [self._readFloat() for i in range(0, length)]
    94                 else:
    95                     value = None
    9698            elif type=="ia":
    9799                length = self._readS32()
    98100                if length>0:
    99101                    value = [self._readS32() for i in range(0, length)]
    100                 else:
    101                     value = None
    102102            elif type=="ba":
    103103                length = self._readS32()
     
    108108                        if b==0: break
    109109                        value += chr(b)
    110                 else:
    111                     value = None
     110                    #value = bytes
    112111
    113             print "value:", value, len(value)
     112            print value
     113        else:
     114            print "Result code:", result
    114115
    115116    def _writeU8(self, x):
  • src/xplra/DataRefTask.h

    r3 r6  
    7272    DataRefTask(XPLMDataRef dataRef);
    7373
     74public:
    7475    /**
    7576     * Determine if the task is valid, which is so, if the dataref is
     
    8889    XPLMDataRef getDataRef() const;
    8990
     91protected:
    9092    /**
    9193     * Perform the task. If the dataref is not valid yet, it will be
  • src/xplra/GetDataRefTask.cc

    r4 r6  
    3030
    3131#include "GetDataRefTask.h"
     32#include "Protocol.h"
    3233
    3334#include "xplcommon/Util.h"
     
    3536//------------------------------------------------------------------------------
    3637
     38using xplra::GetDataRefTask;
     39using xplra::GetIntDataRefTask;
     40using xplra::GetFloatDataRefTask;
     41using xplra::GetDoubleDataRefTask;
     42
     43using xplcommon::DataStream;
     44
     45using std::string;
     46
     47//------------------------------------------------------------------------------
     48
     49GetDataRefTask* GetDataRefTask::create(uint8_t& result, DataStream& stream)
     50{
     51    result = Protocol::RESULT_OK;
     52
     53    string name = stream.readString();
     54    uint8_t type = stream.readU8();
     55    if (!stream) return 0;
     56
     57    if (type==Protocol::TYPE_INT) {
     58        return new GetIntDataRefTask(name);
     59    } else if (type==Protocol::TYPE_FLOAT) {
     60        return new GetFloatDataRefTask(name);
     61    } else if (type==Protocol::TYPE_DOUBLE) {
     62        return new GetDoubleDataRefTask(name);
     63    } else if (type==Protocol::TYPE_FLOAT_ARRAY ||
     64               type==Protocol::TYPE_INT_ARRAY ||
     65               type==Protocol::TYPE_BYTE_ARRAY)
     66    {
     67        int length = stream.readS32();
     68        int offset = stream.readS32();
     69        if (!stream) return 0;
     70
     71        if (type==Protocol::TYPE_FLOAT_ARRAY) {
     72            return new GetFloatArrayDataRefTask(name, length, offset);
     73        } else if (type==Protocol::TYPE_INT_ARRAY) {
     74            return new GetIntArrayDataRefTask(name, length, offset);
     75        } else {
     76            return new GetByteArrayDataRefTask(name, length, offset);
     77        }
     78    } else {
     79        result = Protocol::RESULT_INVALID_TYPE;
     80        return 0;
     81    }
     82}
     83
     84//------------------------------------------------------------------------------
     85//------------------------------------------------------------------------------
     86
     87void GetIntDataRefTask::writeValue(DataStream& stream)
     88{
     89    stream.writeS32(getValue());
     90}
     91
     92//------------------------------------------------------------------------------
     93
     94void GetFloatDataRefTask::writeValue(DataStream& stream)
     95{
     96    stream.writeFloat(getValue());
     97}
     98
     99//------------------------------------------------------------------------------
     100
     101void GetDoubleDataRefTask::writeValue(DataStream& stream)
     102{
     103    stream.writeDouble(getValue());
     104}
    37105
    38106//------------------------------------------------------------------------------
  • src/xplra/GetDataRefTask.h

    r4 r6  
    3333#include "DataRefTask.h"
    3434
     35#include <xplcommon/DataStream.h>
     36
    3537//------------------------------------------------------------------------------
    3638
     
    4648public:
    4749    /**
     50     * Read a dataref query specification and create the appropriate
     51     * GetDataRefTask instance.
     52     *
     53     * @return the new instance, or 0 on error. In that case the given
     54     * result reference variable will be filled with the error code,
     55     * unless the stream failed, in which case it remains RESULT_OK.
     56     */
     57    static GetDataRefTask* create(uint8_t& result, xplcommon::DataStream& stream);
     58
     59    /**
    4860     * Construct the task for the dataref with the given name.
    4961     */
     
    5466     */
    5567    GetDataRefTask(XPLMDataRef dataRef);
     68
     69    /**
     70     * Write the value into the given stream.
     71     */
     72    virtual void writeValue(xplcommon::DataStream& stream) = 0;
    5673};
    5774
     
    120137     */
    121138    GetIntDataRefTask(XPLMDataRef dataRef);
     139
     140    /**
     141     * Write the value into the given stream.
     142     */
     143    virtual void writeValue(xplcommon::DataStream& stream);
    122144};
    123145
     
    147169     */
    148170    GetFloatDataRefTask(XPLMDataRef dataRef);
     171
     172    /**
     173     * Write the value into the given stream.
     174     */
     175    virtual void writeValue(xplcommon::DataStream& stream);
    149176};
    150177
     
    174201     */
    175202    GetDoubleDataRefTask(XPLMDataRef dataRef);
     203
     204    /**
     205     * Write the value into the given stream.
     206     */
     207    virtual void writeValue(xplcommon::DataStream& stream);
    176208};
    177209
     
    253285     */
    254286    virtual void process();
     287
     288    /**
     289     * Write the value into the given stream.
     290     */
     291    virtual void writeValue(xplcommon::DataStream& stream);
    255292};
    256293
     
    493530
    494531//------------------------------------------------------------------------------
     532
     533template <typename T, class ConcreteClass>
     534void GetArrayDataRefTask<T, ConcreteClass>::
     535writeValue(xplcommon::DataStream& stream)
     536{
     537    stream.writeS32(length);
     538    if (length>0) {
     539        stream.write(data, length * sizeof(T));
     540    }
     541}
     542
     543//------------------------------------------------------------------------------
    495544// Inline definitions
    496545//------------------------------------------------------------------------------
  • src/xplra/Makefile.am

    r5 r6  
    2929        ServerThread.cc         \
    3030        TaskRequest.cc          \
    31         DataRefTask.cc
     31        DataRefTask.cc          \
     32        GetDataRefTask.cc
    3233
    3334libxplra_la_LIBADD=$(LIBXPLCOMMON_LIBS)
  • src/xplra/Protocol.h

    r5 r6  
    4444     * Command: get the value of a single dataref.
    4545     */
    46     static const uint8_t CMD_GET_SINGLE = 0x01;
     46    static const uint8_t COMMAND_GET_SINGLE = 0x01;
    4747
    4848    /**
  • src/xplra/ServerThread.cc

    r5 r6  
    110110        //             this, command);
    111111
    112         if (command==Protocol::CMD_GET_SINGLE) {
    113             string name = stream.readString();
    114             uint8_t type = stream.readU8();
    115             if (!stream) continue;
    116             // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: name='%s', type=0x%02x\n",
    117             //             this, name.c_str(), type);
    118             if (type==Protocol::TYPE_BYTE_ARRAY) {
    119                 int length = stream.readS32();
    120                 if (!stream) continue;
    121 
    122                 // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: length=%lu\n",
    123                 //             this, static_cast<unsigned long>(length));
    124 
    125                 GetByteArrayDataRefTask* task =
    126                     new GetByteArrayDataRefTask(name, length);
    127                 TaskRequest request(task);
    128 
    129                 if (requestQueue.execute(&request)) {
    130                     // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: request executed\n");
    131                     stream.writeU8(Protocol::RESULT_OK);
    132                     int length = task->getLength();
    133                     stream.writeS32(length);
    134                     if (length>0) {
    135                         stream.write(task->getData(), length);
    136                     }
    137                 } else {
    138                     break;
    139                 }
    140             } else {
    141                 stream.writeU8(Protocol::RESULT_OTHER_ERROR);
    142             }
     112        if (command==Protocol::COMMAND_GET_SINGLE) {
     113            if (!handleGetSingle(stream)) break;
    143114        } else {
    144115            stream.writeU8(Protocol::RESULT_INVALID_COMMAND);
     
    152123//------------------------------------------------------------------------------
    153124
     125bool ServerThread::handleGetSingle(xplcommon::DataStream& stream)
     126{
     127    uint8_t result = Protocol::RESULT_OK;
     128
     129    GetDataRefTask* task = GetDataRefTask::create(result, stream);
     130
     131    if (!stream) return false;
     132    else if (task==0) {
     133        stream.writeU8(Protocol::RESULT_INVALID_TYPE);
     134        return true;
     135    }
     136
     137    TaskRequest request(task);
     138    if (!requestQueue.execute(&request)) return false;
     139
     140    if (task->isValid()) {
     141        stream.writeU8(Protocol::RESULT_OK);
     142        task->writeValue(stream);
     143    } else {
     144        stream.writeU8(Protocol::RESULT_UNKNOWN_DATAREF);
     145    }
     146
     147    return true;
     148}
     149
     150//------------------------------------------------------------------------------
     151
    154152// Local Variables:
    155153// mode: C++
  • src/xplra/ServerThread.h

    r5 r6  
    122122     */
    123123    virtual void run();
     124
     125private:
     126    /**
     127     * Handle the COMMAND_GET_SINGLE command
     128     *
     129     * @return true, if we can continue, false if the thread should quit
     130     */
     131    bool handleGetSingle(xplcommon::DataStream& stream);
    124132};
    125133
Note: See TracChangeset for help on using the changeset viewer.