// Copyright (c) 2013 by István Váradi // This file is part of XPLRA, a remote-access plugin for X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. #ifndef XPLRA_SERVERTHREAD_H #define XPLRA_SERVERTHREAD_H //------------------------------------------------------------------------------ #include #include #include #include #include #include #include #include //------------------------------------------------------------------------------ namespace xplra { //------------------------------------------------------------------------------ class ListenThread; class RequestQueue; class GetMultiDataRefRequest; class SetMultiDataRefRequest; //------------------------------------------------------------------------------ /** * A thread serving a client. */ class ServerThread : public hu::varadiistvan::scpl::Thread { private: /** * Type for the set of server thread instances. */ typedef std::set instances_t; /** * Type for the registered multiple-data query requests. */ typedef std::map getMultiRequests_t; /** * Type for the registered multiple-data update requests. */ typedef std::map setMultiRequests_t; /** * A mutex to protect the collection of server threads. */ static hu::varadiistvan::scpl::Mutex instancesMutex; /** * The instances of this class. */ static instances_t instances; public: /** * Call quit() an all threads. */ static void quitAll(); private: /** * The listen thread this server was started by. */ ListenThread& listenThread; /** * The request queue to use. */ RequestQueue& requestQueue; /** * Our waiter. */ hu::varadiistvan::scpl::io::Waiter waiter; /** * The buffered stream to use for communication at the low-level. */ hu::varadiistvan::scpl::io::BufferedStream* bufferedStream; /** * The data stream being used for communication. */ hu::varadiistvan::scpl::io::DataStream stream; /** * The ID of the next multiple-data query request. */ size_t nextGetMultiRequestID; /** * The registered multiple-data query requests. */ getMultiRequests_t getMultiRequests; /** * The ID of the next multiple-data update request. */ size_t nextSetMultiRequestID; /** * The registered multiple-data query requests. */ setMultiRequests_t setMultiRequests; public: /** * Construct the thread by using a LocalSocket returned by the * given acceptor. */ ServerThread(ListenThread& listenThread, RequestQueue& requestQueue, hu::varadiistvan::scpl::io::LocalAcceptor& acceptor); /** * Destroy the thread. */ virtual ~ServerThread(); /** * Quit the thread. It interrupts the blocking stream. */ void quit(); /** * Perform the thread's operation. */ virtual void run(); private: /** * Handle the COMMAND_GET_SINGLE command * * @return true, if we can continue, false if the thread should quit */ bool handleGetSingle(); /** * Handle the COMMAND_SET_SINGLE command * * @return true, if we can continue, false if the thread should quit */ bool handleSetSingle(); /** * Handle the COMMAND_GET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleGetMulti(); /** * Handle the COMMAND_SET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleSetMulti(); /** * Handle the COMMAND_REGISTER_GET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleRegisterGetMulti(); /** * Handle the COMMAND_UNREGISTER_GET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleUnregisterGetMulti(); /** * Handle the COMMAND_EXECUTE_GET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleExecuteGetMulti(); /** * Handle the COMMAND_REGISTER_SET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleRegisterSetMulti(); /** * Handle the COMMAND_UNREGISTER_SET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleUnregisterSetMulti(); /** * Handle the COMMAND_EXECUTE_SET_MULTI command * * @return true, if we can continue, false if the thread should quit */ bool handleExecuteSetMulti(); /** * Handle the COMMAND_GET_VERSIONS command * * @return true, if we can continue, false if the thread should quit */ bool handleGetVersions(); /** * Handle the COMMAND_SHOW_MESSAGE command * * @return true, if we can continue, false if the thread should quit */ bool handleShowMessage(); }; //------------------------------------------------------------------------------ } /* namespace xplra */ //------------------------------------------------------------------------------ #endif // XPLRA_SERVERTHREAD_H // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: