00001 /* Glass - a distributed computing library 00002 Copyright (C) 2003-2009 Bruno Barberi Gnecco 00003 Copyright (C) 2009-2010 Corollarium Technologies 00004 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 #ifndef _GLASS_PLUGIN_HH_ 00022 #define _GLASS_PLUGIN_HH_ 00023 00024 #include "exception.h" 00025 #include "types.h" 00026 #include <vector> 00027 #include <map> 00028 #include <string> 00029 00030 namespace libglass { 00031 00032 class Glass; 00033 class Packet; 00034 class PluginBase; 00035 class PluginManager; 00036 00037 /* TODO!!! Plugins registered with PluginManagerBase are not registered in 00038 PluginManager instances already created. Although this situation should never 00039 happen currently, since all plugins register themselves before a PluginManager 00040 class can be instantiated, it precludes registration of new plugins during 00041 code execution. To overcome this, create a list of all the PluginManager's 00042 the factory creates, and when a new plugin is registered, register in all of 00043 them. */ 00044 00054 class PluginManagerBase { 00055 private: 00059 std::map<PluginId, PluginBase *> pluginmap; 00060 00064 PluginManagerBase(); 00065 00069 void operator=(PluginManagerBase &); 00070 00074 PluginManagerBase(const PluginManagerBase &) { } 00075 00076 protected: 00077 ~PluginManagerBase(); 00078 00079 friend class PluginBase; 00080 friend class Glass; 00081 00089 PluginManager *createPluginManager(Glass *g); 00090 00094 bool registerPlugin(PluginBase *base); 00095 public: 00096 00102 static PluginManagerBase& getHandle(void); 00103 }; 00104 00114 class PluginManager { 00115 private: 00119 std::map<PluginId, PluginBase *> *pluginmap; 00120 00124 Glass *glass; 00125 00129 void operator=(PluginManager &); 00130 00135 PluginManager(const PluginManager &); 00136 00137 protected: 00138 friend class PluginManagerBase; 00139 friend class PluginBase; 00140 friend class PluginInterface; 00141 friend class Glass; 00142 00146 PluginManager(Glass *g, std::map<PluginId, PluginBase *> *pluginmap); 00147 00160 bool processPacket(Packet &p, bool rootOnly = false); 00161 00169 bool sendPacket(Packet &p); 00170 00183 void unregisterNode(nodeId id); 00184 00189 Glass *getGlass(void) const; 00190 00191 public: 00195 ~PluginManager(); 00196 00202 PluginBase *getPlugin(PluginId id) const; 00203 00204 void printPlugins(void); 00205 }; 00206 00207 00235 class PluginBase { 00236 private: 00240 PluginId id; 00241 00242 protected: 00243 friend class PluginManagerBase; 00244 friend class PluginManager; 00245 00249 PluginManager *pm_parent; 00250 00261 virtual bool processPacket(Packet &p) = 0; 00262 00271 virtual PluginBase *instance(PluginManager *parent) = 0; 00272 00283 virtual void unregisterNode(nodeId id) = 0; 00284 00292 virtual bool sendPacket(Packet &p); 00293 00298 nodeRelation getNodeRelation(nodeId id) const; 00299 00304 Glass *getGlass(void) const; 00305 00306 public: 00310 PluginBase(const PluginId id, bool *registered); 00311 00315 virtual ~PluginBase() = 0; 00316 00320 PluginId getPluginId(void) const; 00321 }; 00322 00323 00324 /* And now, the plugins themselves */ 00325 00331 #define PLUGIN(cl) \ 00332 private: \ 00333 cl(); \ 00334 void operator=(cl &); \ 00335 static bool registered; /* avoid duplicate id */\ 00336 static cl singleton; /* for auto-registering */ \ 00337 protected: \ 00338 /* prototypes for virtual functions */ \ 00339 bool processPacket(Packet &p); \ 00340 void unregisterNode(nodeId id); \ 00341 /* the virtual constructor */ \ 00342 PluginBase *instance(PluginManager *parent) { \ 00343 cl *inst = new cl(); \ 00344 inst->pm_parent = parent; \ 00345 return inst; \ 00346 } \ 00347 friend class PluginManagerBase; \ 00348 public: \ 00349 static cl & getHandle(void) { \ 00350 return singleton; \ 00351 } 00352 00416 #define PLUGIN_CONSTRUCTOR(cl) \ 00417 bool cl::registered = false; \ 00418 cl cl::singleton; \ 00419 cl::cl() : PluginBase(#cl, ®istered) 00420 00421 00433 class PluginInterface { 00434 private: 00435 Glass *g; 00436 protected: 00440 PluginInterface(); 00441 00445 virtual ~PluginInterface() = 0; 00446 00452 PluginBase *getPluginBase(PluginId id); 00453 00458 nodeRelation getNodeRelation(nodeId id) const; 00459 00464 static const PluginManager *getPluginManager(void); 00465 }; 00466 00467 } //namespace 00468 00469 #endif