Changeset 3:459106f9bf9a in xplra


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

Very basic client and server functionality works

Files:
8 added
4 edited

Legend:

Unmodified
Added
Removed
  • src/xplra/ListenThread.cc

    r2 r3  
    5555    while(!quitEvent.check() && !quitEvent.failed() && !acceptor.failed()) {
    5656        if (acceptor.accept()) {
    57             ServerThread* serverThread = new ServerThread(acceptor);
     57            ServerThread* serverThread = new ServerThread(requestQueue,
     58                                                          acceptor);
    5859            serverThread->start();
    5960        }
  • src/xplra/Makefile.am

    r2 r3  
    2424
    2525libxplra_la_SOURCES= \
    26         plugin.cc       \
    27         ListenThread.cc \
    28         RequestQueue.cc \
    29         ServerThread.cc
     26        plugin.cc               \
     27        ListenThread.cc         \
     28        RequestQueue.cc         \
     29        ServerThread.cc         \
     30        TaskRequest.cc          \
     31        DataRefTask.cc          \
     32        GetDataRefTask.cc
    3033
    3134libxplra_la_LIBADD=$(LIBXPLCOMMON_LIBS)
     
    3538        Request.h               \
    3639        RequestQueue.h          \
    37         ServerThread.h
     40        ServerThread.h          \
     41        Task.h                  \
     42        TaskRequest.h           \
     43        DataRefTask.h           \
     44        GetDataRefTask.h
  • src/xplra/ServerThread.cc

    r2 r3  
    3131#include "ServerThread.h"
    3232
     33#include "RequestQueue.h"
     34#include "GetDataRefTask.h"
     35#include "TaskRequest.h"
     36
     37#include <xplcommon/Util.h>
     38
     39#include <cstdio>
     40
    3341//------------------------------------------------------------------------------
    3442
     
    3745using xplcommon::LocalAcceptor;
    3846using xplcommon::Mutex;
     47using xplcommon::Util;
     48
     49using std::string;
    3950
    4051//------------------------------------------------------------------------------
     
    5768//------------------------------------------------------------------------------
    5869
    59 ServerThread::ServerThread(LocalAcceptor& acceptor) :
     70ServerThread::ServerThread(RequestQueue& requestQueue, LocalAcceptor& acceptor) :
    6071    Thread(true),
     72    requestQueue(requestQueue),
    6173    bufferedStream(acceptor.getSocket(&waiter)),
    6274    stream(*bufferedStream)
     
    89101void ServerThread::run()
    90102{
    91     stream.write("Hello, world!", 13);
    92     stream.flush();
     103    Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run\n", this);
     104    while(stream) {
     105        uint8_t command = stream.readU8();
     106        if (!stream) continue;
     107
     108        // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: command=0x%02x\n",
     109        //             this, command);
     110
     111        if (command==CMD_GET_SINGLE) {
     112            string name = stream.readString();
     113            uint8_t type = stream.readU8();
     114            if (!stream) continue;
     115            // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: name='%s', type=0x%02x\n",
     116            //             this, name.c_str(), type);
     117            if (type==TYPE_BYTE_ARRAY) {
     118                int length = stream.readS32();
     119                if (!stream) continue;
     120
     121                // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: length=%lu\n",
     122                //             this, static_cast<unsigned long>(length));
     123
     124                GetByteArrayDataRefTask* task =
     125                    new GetByteArrayDataRefTask(name, length);
     126                TaskRequest request(task);
     127
     128                if (requestQueue.execute(&request)) {
     129                    // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: request executed\n");
     130                    stream.writeU8(RESULT_OK);
     131                    int length = task->getLength();
     132                    stream.writeS32(length);
     133                    if (length>0) {
     134                        stream.write(task->getData(), length);
     135                    }
     136                } else {
     137                    break;
     138                }
     139            } else {
     140                stream.writeU8(RESULT_OTHER_ERROR);
     141            }
     142        } else {
     143            stream.writeU8(RESULT_INVALID_COMMAND);
     144        }
     145        stream.flush();
     146    }
     147
     148    Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: quitting\n", this);
    93149}
    94150
  • src/xplra/ServerThread.h

    r2 r3  
    3737#include <xplcommon/Waiter.h>
    3838#include <xplcommon/BufferedStream.h>
    39 #include <xplcommon/BlockingStream.h>
     39#include <xplcommon/DataStream.h>
    4040#include <xplcommon/LocalAcceptor.h>
    4141
     
    4848//------------------------------------------------------------------------------
    4949
     50class RequestQueue;
     51
     52//------------------------------------------------------------------------------
     53
    5054/**
    5155 * A thread serving a client.
     
    5357class ServerThread : public xplcommon::Thread
    5458{
     59public:
     60    /**
     61     * Command: get the value of a single dataref.
     62     */
     63    static const uint8_t CMD_GET_SINGLE = 0x01;
     64
     65    /**
     66     * Data type: int
     67     */
     68    static const uint8_t TYPE_INT = 0x01;
     69
     70    /**
     71     * Data type: float
     72     */
     73    static const uint8_t TYPE_FLOAT = 0x02;
     74
     75    /**
     76     * Data type: double
     77     */
     78    static const uint8_t TYPE_DOUBLE = 0x03;
     79
     80    /**
     81     * Data type: float array
     82     */
     83    static const uint8_t TYPE_FLOAT_ARRAY = 0x11;
     84
     85    /**
     86     * Data type: int array
     87     */
     88    static const uint8_t TYPE_INT_ARRAY = 0x12;
     89
     90    /**
     91     * Data type: byte array
     92     */
     93    static const uint8_t TYPE_BYTE_ARRAY = 0x13;
     94
     95    /**
     96     * Result code: no error, everything is OK.
     97     */
     98    static const uint8_t RESULT_OK = 0x00;
     99
     100    /**
     101     * Result code: invalid command
     102     */
     103    static const uint8_t RESULT_INVALID_COMMAND = 0x01;
     104
     105    /**
     106     * Result code: unknown dataref
     107     */
     108    static const uint8_t RESULT_UNKNOWN_DATAREF = 0x02;
     109
     110    /**
     111     * Result code: invalid type
     112     */
     113    static const uint8_t RESULT_INVALID_TYPE = 0x03;
     114
     115    /**
     116     * Result code: other error
     117     */
     118    static const uint8_t RESULT_OTHER_ERROR = 0xff;
     119
    55120private:
    56121    /**
     
    77142private:
    78143    /**
     144     * The request queue to use.
     145     */
     146    RequestQueue& requestQueue;
     147
     148    /**
    79149     * Our waiter.
    80150     */
     
    87157
    88158    /**
    89      * The blocking stream being used for communication.
     159     * The data stream being used for communication.
    90160     */
    91     xplcommon::BlockingStream stream;
     161    xplcommon::DataStream stream;
    92162
    93163public:
     
    96166     * given acceptor.
    97167     */
    98     ServerThread(xplcommon::LocalAcceptor& acceptor);
     168    ServerThread(RequestQueue& requestQueue,
     169                 xplcommon::LocalAcceptor& acceptor);
    99170
    100171    /**
Note: See TracChangeset for help on using the changeset viewer.