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_ADAPTER 00009 #define __IDC_ADAPTER 00010 00011 #include "BufferHolder.h" 00012 00013 /** 00014 * This is the interface (abstract class) for all the adapters that will be ever 00015 * written for the Beesnest project. To write an implementation of this 00016 * interface, inherit from it and implement all the functions. Then put your 00017 * new adapter in a dynamically loaded library (DLL for windoes, sa for Linux) 00018 * with a specific format. 00019 * <p> 00020 * An adapter is a server to the Beesnest system, which in turn is a server for 00021 * an HTTP client. 00022 * <p> 00023 * An object of this class will be passed just to one thread at a time. However 00024 * if you do not prevent it (using the <b>IsMultiUse</b> function) different 00025 * threads might use different objects of this adapter at the same time. 00026 * <p> 00027 * The library that hosts the implementation of an IdcAdapter has to have at 00028 * last one function: <br> 00029 * 00030 * <b> bool GetAdapter (IdcAdapter** pp_adapter, 00031 * const char* name, 00032 * bool (*pGetParam)(const char*, char*, int)) </b> 00033 * <br> 00034 * This function will return a pointer to the implementation of the adapter. 00035 * The name is for identifying this object. the class has to save this name as 00036 * a class member, and use it to find the path for its part in the configuration 00037 * database. The last parameter is a pointer to a function of Beesnest 00038 * (ExtGetParam). this function is used to access the server configuration 00039 * database. In this function the first "C" style string is the name of the 00040 * parameter you want to retrieve, the second string is a pre-allocated buffer 00041 * for the parameter value, and the last int is the length of this buffer. This 00042 * function will return True if the parameter exist, otherwise it will return 00043 * False. You are welcome to use this function and add your adapter 00044 * configuration data to the Beesnest configuration files. 00045 * <p> 00046 * If the name of an adapter object is "MyAdapter", then this object has to (or 00047 * can) read it's configuration from "Adapters->MyAdapter". 00048 * <p> 00049 * All interfaces use BufferHolder to pass strings or buffers. 00050 * <p> 00051 * You have to have a member string variable in the implementation of this 00052 * interface, that will hold the results from the Read function. Then you can 00053 * return a BufferHolder that holds this string to the user. The user MUST not 00054 * try to change this string. If you want to let the user to clear this string, 00055 * in some point (to clear memory space), set a Write call like Write("done") 00056 * that will do that. 00057 * <p> 00058 * You can throw a BufferHolder from Read or from Write. The buffer holder has 00059 * to point to an inner class string, that must be alive at least until the next 00060 * call to one of the class functions, or until the Destructor is called.<br> 00061 * It is not defined what the script engine will do with this exception. It 00062 * might let the script writer handle it, or it might stop the processing of 00063 * the script (and the page), and put the content of the BufferHolder in the 00064 * page the user will get. use it just in extreme cases. You should NOT throw 00065 * an exception from the constructor. This kind of exception might break the 00066 * Beesnest application. If you cannot initialize the adapter, you should throw 00067 * an exception from the first call to Read or Write. 00068 */ 00069 class IdcAdapter 00070 { 00071 00072 public: 00073 00074 /** 00075 * The Destructor is very important in an adapter implementation. Do not 00076 * count on the user to release anything. In some situations the system may 00077 * just delete the adapter without any advance notice, and the Destructor 00078 * has to make sure the adapter closes itself nicely. 00079 */ 00080 virtual ~IdcAdapter () {} 00081 00082 /** 00083 * Reads some data from the adapter. 00084 * <p> 00085 * @param address The address (or name) of data to read. 00086 * @return (BufferHolder) Holds the result string. This string has to stay 00087 * alive at least until the next call to Read (or to the Destructor). 00088 * @throw (BufferHolder) A pointer to an inner string. You have to keep this 00089 * string alive at least until the next call to this adapter (or until the 00090 * call to the Destructor) 00091 */ 00092 virtual BufferHolder Read (BufferHolder address) throw (BufferHolder) = 0; 00093 00094 /** 00095 * Writes some data to the adapter. 00096 * <p> 00097 * @param address The address (or name) of data to write. 00098 * @param value The value to write to this address (Note that it might be 00099 * empty!). 00100 * @return (bool) True on success or False if the action failed. 00101 * @throw (BufferHolder) A pointer to an inner string. You have to keep this 00102 * string alive at least until the next call to this adapter (or until the 00103 * call to the Destructor) 00104 */ 00105 virtual bool Write (BufferHolder address, BufferHolder value) 00106 throw (BufferHolder) = 0; 00107 00108 /** 00109 * If you do not want more then one object from this adapter to exist at the 00110 * same time, return False from this function. This will overwrite what 00111 * ever the user sets in the configuration files. 00112 */ 00113 virtual bool IsMultiUse () 00114 { return true; } 00115 00116 }; 00117 00118 #endif // __IDC_ADAPTER 00119