• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Engines/BnPython/BnPython.h

Go to the documentation of this file.
00001 
00002 /* DcDynamicHTML.h: interface for the CdcDynamicHTML class. */
00003 
00004 #ifndef __CDC_DAYNAMIC_HTML
00005 #define __CDC_DAYNAMIC_HTML
00006 
00007 #include <Python.h>
00008 
00009 #include "../../Interfaces/DcEngine.h"
00010 
00011 #include <iostream>
00012 #include <map>
00013 #include <set>
00014 using namespace std;
00015 
00016 #include <time.h>
00017 
00018 /**
00019  * This is a wrapper to the Python interpreter, for the Beesnest server.<br>
00020  * The only issue here is that the Python interpreter works with "C" style
00021  * functions, but Beesnest works with classes and objects. The solution: in
00022  * every call to a Beesnest function from python, "self" will be an integer that
00023  * will map to one of this class instances. The user doesn't have to do anything
00024  * for it.
00025  *
00026  * 2008-01-18 - I overload the "exit" function. It will terminate the script
00027  * but return True (so it will not consider as an error). However, I use
00028  * Python exception to do it.
00029  * Calling sys.exit terminate the entire Beesnest application. This is a bug!
00030  */
00031 class BnPython : public IdcEngine
00032 {
00033 private:
00034     /** Pointer to the response object. */
00035     IdcResponse* pRes;
00036 
00037     /** Pointer to the request object. */
00038     IdcRequest* pReq;
00039 
00040     /** Optional pointer to the adapters set object. */
00041     IdcAdaptersSet* pSet;
00042 
00043     /** Set to store all (unreleased) adapters. */
00044     set<unsigned int> oAdaptersList;
00045 
00046     /** Pointer to a function of Beesnest to get parameters from
00047         the configuration files. */
00048     bool (*pfGetParam)(const char*, char*, int);
00049 
00050     /** Store the name of this engine object. */
00051     string sName;
00052 
00053     /** Pointer to the Python interpreter thread. */
00054     PyThreadState* oTS;
00055 
00056     /** The buffer to return to Beesnest. */
00057     string sBuffer;
00058 
00059     /** This is the key to the pointer of the class instance. */
00060     int nPointerCell;
00061 
00062     /** This is the returned buffer size limit. */
00063     int nSizeLimit;
00064 
00065     /** Non zero if need to throw exception to Beesnest. */
00066     int nException;
00067 
00068     /** Will be false if script terminate using "exit" - no error message. */
00069     bool bExit;
00070     // Note that sys.exit will close Beesnest - This is a bug.
00071 
00072     /** Counter to get next key. */
00073     static int counter;
00074 
00075     /** This static map store pointers to the class instances. */
00076     static map<int, BnPython*> mPointers;
00077 
00078     /** This static function return a pointer to BnPython instance by an integer
00079         key, or NULL if key is invalid. */
00080     static BnPython* GetPointer (int num);
00081 
00082     /** This function releases all the adapters when script is done. */
00083     void ReleaseAdapters ();
00084 
00085     /** This function checks that the returned buffer is still in the size limit. */
00086     bool CheckSizeLimit ();
00087 
00088 public:
00089     /**
00090      * Constractor - Starts the interpreter and initialize variables. It also
00091      * inserts the pointer to this object (this) to the Pointers map, under a
00092      * new integer key.
00093      */
00094     BnPython (const char* name, bool (*pGetParam)(const char*, char*, int));
00095 
00096     /**
00097      * Destructor - Close the interpreter.
00098      */
00099     virtual ~BnPython ();
00100 
00101     /**
00102      * Do the actual processing of a script.
00103      */
00104     virtual bool Process (BufferHolder code, BufferHolder& result, int size_limit);
00105 
00106     /**
00107      * Prepare the class for a new request. Set the new Request, Response and
00108      * maybe Adapters Set objects.
00109      */
00110     virtual void Prepare (IdcRequest* preq, IdcResponse* pres, IdcAdaptersSet* pset);
00111 
00112     /**
00113      * Just return True in most cases. Return False after an exception.
00114      */
00115     virtual bool IsStorable () { return nException == 0; }
00116 
00117     /**
00118      * Make sure all the adapters are released, and clear the buffer that goes
00119      * back to Beesnest.
00120      */
00121      virtual void Stored ();
00122 
00123     /* "C" style functions to transfer to the Python interpreter.
00124         They all look the same. */
00125 
00126     friend PyObject* Print (PyObject *self, PyObject *args);
00127     friend PyObject* GetRequestHeader (PyObject *self, PyObject *args);
00128     friend PyObject* GetRequestVariable (PyObject *self, PyObject *args);
00129     friend PyObject* SetRequestVariable (PyObject *self, PyObject *args);
00130     friend PyObject* GetRequestVarsNamesList (PyObject *self, PyObject *args);
00131     friend PyObject* SetResponseHeader (PyObject *self, PyObject *args);
00132     friend PyObject* SetResponseCookie (PyObject *self, PyObject *args);
00133     friend PyObject* Clear (PyObject *self, PyObject *args);
00134     friend PyObject* Flush (PyObject *self, PyObject *args);
00135     friend PyObject* GetAdapter (PyObject *self, PyObject *args);
00136     friend PyObject* ReleaseAdapter (PyObject *self, PyObject *args);
00137     friend PyObject* Read (PyObject *self, PyObject *args);
00138     friend PyObject* Write (PyObject *self, PyObject *args);
00139     friend PyObject* Exit (PyObject *self, PyObject *args);
00140 };
00141 
00142 #endif /* __CDC_DAYNAMIC_HTML */
00143 

Generated on Mon Oct 11 2010 16:23:25 for Beesnest by  doxygen 1.7.2