webservice/module.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.8.0/modules/webservice/module.cpp $
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 #include "module.h"
00029 
00030 
00031 namespace module_webservice
00032 {
00033 
00034 unsigned int CommandWebservice::port = 6262;
00035 unsigned int CommandWebservice::threads = 10;
00036 
00037 
00038 MODULE_EXPORT const char* initialize(const CommandLoadLibrary::ParameterList& z)
00039 {
00040   // Initialize only once
00041   static bool init = false;
00042   static const char* name = "webservice";
00043   if (init)
00044   {
00045     logger << "Warning: Initializing module webservice more than once." << endl;
00046     return name;
00047   }
00048   init = true;
00049 
00050   try
00051   {
00052     // Process the module parameters
00053     for (CommandLoadLibrary::ParameterList::const_iterator x = z.begin();
00054       x != z.end(); ++x)
00055     {
00056       if (x->first == "port")
00057         CommandWebservice::setPort(x->second.getInt());
00058       else if (x->first == "threads")
00059         CommandWebservice::setThreads(x->second.getInt());
00060       else
00061         logger << "Warning: Unrecognized parameter '" << x->first << "'" << endl;
00062     }
00063 
00064     // Initialize the Python extension.
00065     PyThreadState *myThreadState = PyGILState_GetThisThreadState();
00066     if (!Py_IsInitialized() || !myThreadState)
00067       throw RuntimeException("Python isn't initialized correctly");
00068     try
00069     {
00070       // Get the global lock.
00071       PyEval_RestoreThread(myThreadState);
00072       // Register new Python data types
00073       PythonInterpreter::registerGlobalMethod(
00074         "webservice", CommandWebservice::pythonService, METH_NOARGS,
00075         "Starts the webservice to listen for HTTP requests");
00076     }
00077     // Release the global lock when leaving the function
00078     catch (...)
00079     {
00080       PyEval_ReleaseLock();
00081       throw;  // Rethrow the exception
00082     }
00083     PyEval_ReleaseLock();
00084   }
00085   catch (exception &e)
00086   {
00087     // Avoid throwing errors during the initialization!
00088     logger << "Error: " << e.what() << endl;
00089   }
00090   catch (...)
00091   {
00092     logger << "Error: unknown exception" << endl;
00093   }
00094   // Return the name of the module
00095   return name;
00096 }
00097 
00098 
00099 }       // end namespace
Generated by  doxygen 1.6.2-20100208