module.h

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/modules/webservice/module.h $
00003   version : $LastChangedRevision: 1108 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2009-12-06 18:54:18 +0100 (Sun, 06 Dec 2009) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007 by Johan De Taeye                                    *
00010  *                                                                         *
00011  * This library is free software; you can redistribute it and/or modify it *
00012  * under the terms of the GNU Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 of the License, or  *
00014  * (at your option) any later version.                                     *
00015  *                                                                         *
00016  * This library is distributed in the hope that it will be useful,         *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
00024  * USA                                                                     *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 /** @file module.h
00029   * @brief Header file for the module webservice.
00030   *
00031   * @namespace module_webservice
00032   * @brief A SOAP webservice to publish frePPLe data as a service.
00033   *
00034   * The gSOAP toolkit is used to create a SOAP service for frePPLe.
00035   *
00036   * A new Python extension is added to run a multi-threaded SOAP webservice
00037   * server.
00038   */
00039 
00040 #include "frepple.h"
00041 #include "freppleinterface.h"
00042 using namespace frepple;
00043 
00044 #include "soapH.h"
00045 
00046 
00047 // Settings specific to gsoap
00048 #define BACKLOG (100) // Max. number of backlog requests
00049 #define MAX_QUEUE (1000) // Max. size of request queue
00050 
00051 
00052 namespace module_webservice
00053 {
00054 
00055 
00056 /** Initialization routine for the library. */
00057 MODULE_EXPORT const char* initialize(const CommandLoadLibrary::ParameterList& z);
00058 
00059 
00060 /** @brief This command runs a multi-threaded SOAP webservice server.
00061   *
00062   */
00063 class CommandWebservice : public Command
00064 {
00065   private:
00066     /** Port number for the server. */
00067     static unsigned int port;
00068 
00069     /** Number of threads to handle requests. */
00070     static unsigned int threads;
00071 
00072     /** Worker function for the threads. */
00073     static void *process_queue(void*);
00074 
00075     /** Put a new connection in the queue. */
00076     int enqueue(SOAP_SOCKET);
00077 
00078     /** Pick a connection from the queue. */
00079     SOAP_SOCKET dequeue();
00080 
00081     struct thread_data
00082     {
00083       public:
00084         CommandWebservice* master;
00085         struct soap *soap_thr; // each thread needs a soap runtime environment
00086         pthread_t tid;
00087         unsigned int index;
00088     };
00089 
00090 
00091     SOAP_SOCKET queue[MAX_QUEUE]; // The global request queue of sockets
00092     int head;
00093     int tail; // Queue head and tail
00094     pthread_mutex_t queue_cs;
00095     pthread_cond_t queue_cv;
00096 
00097   public:
00098     /** Python interface for the webservice server. */
00099     static PyObject* pythonService(PyObject*, PyObject*);
00100 
00101     /** Runs the webservice server. */
00102     void execute();
00103 
00104     /** Returns a descriptive string. */
00105     string getDescription() const {return "frePPLe webservice";}
00106 
00107     /** Default constructor. */
00108     explicit CommandWebservice() : head(0), tail(0) {}
00109 
00110     /** Destructor. */
00111     virtual ~CommandWebservice() {}
00112 
00113     /** Returns the port number. */
00114     static unsigned int getPort() {return port;}
00115 
00116     /** Updates the port number. */
00117     static void setPort(int i)
00118     {
00119       if (i <= 0 || i>65535)
00120         throw DataException("Invalid port number: valid range is 1 - 65535");
00121       port = i;
00122     }
00123 
00124     /** Returns the number of threads for the server. */
00125     static unsigned int getThreads() {return threads;}
00126 
00127     /** Updates the number of threads for the server. */
00128     static void setThreads(int i)
00129     {
00130       if (i <= 0 || i>100)
00131         throw DataException("Invalid number of threads: valid range is 1 - 100");
00132       threads = i;
00133     }
00134 };
00135 
00136 
00137 }
00138 

Documentation generated for frePPLe by  doxygen