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 #include "module.h"
00028
00029
00030 namespace module_webservice
00031 {
00032
00033 unsigned int CommandWebservice::port = 6262;
00034 unsigned int CommandWebservice::threads = 10;
00035
00036
00037 MODULE_EXPORT const char* initialize(const CommandLoadLibrary::ParameterList& z)
00038 {
00039
00040 static bool init = false;
00041 static const char* name = "webservice";
00042 if (init)
00043 {
00044 logger << "Warning: Initializing module webservice more than once." << endl;
00045 return name;
00046 }
00047 init = true;
00048
00049 try
00050 {
00051
00052 for (CommandLoadLibrary::ParameterList::const_iterator x = z.begin();
00053 x != z.end(); ++x)
00054 {
00055 if (x->first == "port")
00056 CommandWebservice::setPort(x->second.getInt());
00057 else if (x->first == "threads")
00058 CommandWebservice::setThreads(x->second.getInt());
00059 else
00060 logger << "Warning: Unrecognized parameter '" << x->first << "'" << endl;
00061 }
00062
00063
00064 PyThreadState *myThreadState = PyGILState_GetThisThreadState();
00065 if (!Py_IsInitialized() || !myThreadState)
00066 throw RuntimeException("Python isn't initialized correctly");
00067 try
00068 {
00069
00070 PyEval_RestoreThread(myThreadState);
00071
00072 PythonInterpreter::registerGlobalMethod(
00073 "webservice", CommandWebservice::pythonService, METH_NOARGS,
00074 "Starts the webservice to listen for HTTP requests");
00075 }
00076
00077 catch (...)
00078 {
00079 PyEval_ReleaseLock();
00080 throw;
00081 }
00082 PyEval_ReleaseLock();
00083 }
00084 catch (exception &e)
00085 {
00086
00087 logger << "Error: " << e.what() << endl;
00088 }
00089 catch (...)
00090 {
00091 logger << "Error: unknown exception" << endl;
00092 }
00093
00094 return name;
00095 }
00096
00097
00098 }