00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 Beesnest is released under the GNU General Public License. 00005 But this file is in the public domain, you can do whatever you want with it. 00006 */ 00007 00008 #ifndef __IDC_ENGINE 00009 #define __IDC_ENGINE 00010 00011 #include "BufferHolder.h" 00012 #include "DcRequest.h" 00013 #include "DcResponse.h" 00014 #include "DcAdaptersSet.h" 00015 00016 /** 00017 * This is the interface for a scripting engine Beesnest will get. It uses 00018 * <b>IdcResponse</b>, <b>IdcRequest</b> and <b>IdcAdaptersSet</b> ( you don't 00019 * implement these three interfaces, Beesnest does). <br> 00020 * 00021 * When the system first gets an engine it calls the <b>Prepare</b> function. 00022 * This will pass the engine a Request, Response and an Adapters Set objects. 00023 * The data needs to be passed just once for every request. Note that if the 00024 * user is not logged in, the Adapters Set pointer will be NULL. 00025 * <p> 00026 * For every (part of a) buffer that needs to be processed by this engine, the 00027 * system will call the <b>Process</b> function. The engine will process the 00028 * buffer and will put the result buffer in a reference to a BufferHolder (as a 00029 * pointer to it's own inner string). The engine will get the buffer size limit 00030 * as well. 00031 * <p> 00032 * IMPORTANT: The returned BufferHolder has to point to a string that will 00033 * continue to exist after Process returns. Either a class member string or 00034 * a dynamically allocated string. You can clear/delete this string when 00035 * <b>Stored</b> is called (or in the Destructor). Beesnest will not try to 00036 * change or delete the returned buffer! It will crush the program if it 00037 * does! 00038 * <p> 00039 * An engine has to return with a False value if the buffer size goes over the 00040 * limit. Whenever you have an error it is your responsibility to put in the 00041 * returned buffer an error message to the client, and return False. 00042 * <p> 00043 * The library that hosts the implementation of an IdcEngine has to have at last 00044 * one function: <br> 00045 * <b> bool GetEngine (IdcEngine** pp_engine, 00046 * const char* name, 00047 * bool (*pGetParam)(const char*, char*, int)) </b> 00048 * <br> 00049 * This function will return a pointer to the implementation of the engine. 00050 * The name is for identifying this object. the class has to save this name as 00051 * a class member, and use it to find the path for its part in the configuration 00052 * database. The last parameter is a pointer to a function of Beesnest 00053 * (ExtGetParam). This function is for accessing the server configuration 00054 * database. In this function the first "C" style string is the name of the 00055 * parameter you want to get, the second string is a pre-allocated buffer to the 00056 * parameter value, and the last int is the length of this buffer. 00057 * This function will return True if the parameter exists, otherwise it will 00058 * return False. You welcome to use this function and add your engine 00059 * configuration data to the configuration files. 00060 * <p> 00061 * Other optional functions of the engine library can be:<br> 00062 * <b> void Init (); </b> and <b> void Term (); </b> These functions, if exist, 00063 * will be called when the first instance of an engine is loaded, and just 00064 * before Beesnest exits. Do any global initialization/termination you need with 00065 * these functions. 00066 * <p> 00067 * If the name of an engine object is "MyEngine", then this object has to (or 00068 * can) read its configuration from "Engines->MyEngine". 00069 * 00070 * <p> 00071 * For a logged in user, the Beesnest system can store an engine. It means That 00072 * a programmer can create a script or a program that will execute over several 00073 * pages. All the data and code that exist when the script started, will still 00074 * exist when the script continue to execute on another page! If you want your 00075 * engine to be "Storable" you need to set the function <b>IsStorable</b> to 00076 * return True. This function is already implemented, and it just returns False. 00077 * When Beesnest stores an engine it calls the <b>Stored</b> function. 00078 * (Beesnest call IsStorable before every time it stores an engine, so you can 00079 * use this function to let it know when not to store the engine). 00080 * Beesnest does not store the engine if the returned value from Process was 00081 * False. 00082 * 00083 * <p> 00084 * All interfaces uses BufferHolder to pass strings or buffers. 00085 */ 00086 class IdcEngine 00087 { 00088 public: 00089 /** 00090 * Destructor. IMPORTANT: In order to implement a script engine you 00091 * HAVE to be able to close the interpreter in a clean way in the Destructor. 00092 * You will not have any knowledge about what did the interpreter do, and 00093 * when did Beesnest is going to call this Destructor. all you know is that 00094 * the script is not running any more! 00095 */ 00096 virtual ~IdcEngine () {} 00097 00098 /** 00099 * Initialize the engine for this page. If you have errors here (cannot 00100 * initialize the engine) just return False from the first call to the 00101 * Process function, and put some message in the returned buffer. 00102 * <p> 00103 * @param preq Pointer to the page Request object. 00104 * @param pres Pointer to the page Response object. 00105 * @param pset Pointer to the user's Adapters Set object. This will be NULL 00106 * if the client is not logged in! 00107 */ 00108 virtual void Prepare (IdcRequest* preq, IdcResponse* pres, 00109 IdcAdaptersSet* pset) = 0; 00110 00111 /** 00112 * This function processes (a part of) a script. 00113 * <p> 00114 * @param code The Buffer Holder that holds the buffer to process. Don't 00115 * try to change this buffer! Note that this buffer WILL NOT end with NULL! 00116 * @param result Reference to a Buffer Holder that the result HTML will 00117 * be in. If this holder will not point to NULL and the buffer will not be 00118 * empty, after calling this function, Beesnest will add it to the response, 00119 * (replacing the "code" string). Beesnest doesn't try to delete this buffer! 00120 * @param size_limit The maximum returned buffer size. If you reach this 00121 * limit, you should return from this function with False value (but put 00122 * some message in 'result'), 0 is unlimited. 00123 * @return (bool) False when you have errors. Put some error message in 00124 * 'result'. If the returned value is False, Beesnest will still sent this 00125 * buffer to the client (with everything that came before it). But it will 00126 * stop to process the page (for the next pieces of scripts). 00127 * @throw int This function can throw an integer value. Beesnest just print 00128 * this value to the console/log end exit the current thread. If you get 00129 * an exception from IdcResponse.Flush, you have to throw an exception. 00130 */ 00131 virtual bool Process (BufferHolder code, BufferHolder& result, 00132 int size_limit) = 0; 00133 00134 /** 00135 * If you want Beesnest to be able to keep an instance of this script engine 00136 * over multiple pages, just return True from this function. The user can 00137 * still turn this feature off from the engine configuration file 00138 * (Engines->Engine-Name->Sortable). Beesnest call this function before 00139 * every time it stores the script engine, so if from some reason, you want 00140 * to reset the engine (like after an error), return False from this 00141 * function 00142 */ 00143 virtual bool IsStorable () { return false; } 00144 00145 /** 00146 * If this engine is "Storable", this function will be called when Beesnest 00147 * stores it. You can use it to remove the string you return a pointer to 00148 * in Process, from memory. Do not remove this string before the call to 00149 * Stored or the call to the Destructor. 00150 * <p> 00151 * After storing an engine the pointers to the Request, Response and 00152 * Adapters Set objects should be considered as invalid. The system will 00153 * set these pointers again, when it take the adapter out of the storage. 00154 * You can "play it safe" and set them to NULL in this function (although 00155 * no one can access them when the engine is in storage). 00156 */ 00157 virtual void Stored () {} 00158 00159 /* 00160 The next two function are for the next version of Beesnest. I'm not calling 00161 them in this version (1.0). 00162 The first function will let the engine know that in the next call to Process, 00163 the "code" variable will be an already compiled script. 00164 The next function will ask for a (pointer to) a compiled version of the last 00165 script that was processed. If the engine doesn't have a compiled version, it 00166 will return False. 00167 The first function will never be called with an engine that return False 00168 for the second function call. 00169 */ 00170 virtual void NextIsCompiled () {} 00171 virtual bool GetCompiledScript (BufferHolder& comp) { return false; } 00172 }; 00173 00174 #endif /* __IDC_ENGINE */ 00175