Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
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
00065 PyThreadState *myThreadState = PyGILState_GetThisThreadState();
00066 if (!Py_IsInitialized() || !myThreadState)
00067 throw RuntimeException("Python isn't initialized correctly");
00068 try
00069 {
00070
00071 PyEval_RestoreThread(myThreadState);
00072
00073 PythonInterpreter::registerGlobalMethod(
00074 "webservice", CommandWebservice::pythonService, METH_NOARGS,
00075 "Starts the webservice to listen for HTTP requests");
00076 }
00077
00078 catch (...)
00079 {
00080 PyEval_ReleaseLock();
00081 throw;
00082 }
00083 PyEval_ReleaseLock();
00084 }
00085 catch (exception &e)
00086 {
00087
00088 logger << "Error: " << e.what() << endl;
00089 }
00090 catch (...)
00091 {
00092 logger << "Error: unknown exception" << endl;
00093 }
00094
00095 return name;
00096 }
00097
00098
00099 }