pluginmgr.h

Go to the documentation of this file.
00001 /*
00002  * pluginmgr.h
00003  *
00004  * Plugin Manager Class Declarations
00005  *
00006  * Portable Windows Library
00007  *
00008  * Contributor(s): Snark at GnomeMeeting
00009  *
00010  * $Revision: 20385 $
00011  * $Author: rjongbloed $
00012  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00013  */
00014 
00015 #ifndef _PLUGINMGR_H
00016 #define _PLUGINMGR_H
00017 
00018 #define DEFAULT_PLUGINDIR "/usr/lib/pwlib"
00019 
00020 #include <ptlib/plugin.h>
00021 
00022 class PPluginSuffix {
00023   private:
00024     int dummy;
00025 };
00026 
00027 template <class C>
00028 void PLoadPluginDirectory(C & obj, const PDirectory & directory, const char * suffix = NULL)
00029 {
00030   PDirectory dir = directory;
00031   if (!dir.Open()) {
00032     PTRACE(4, "Cannot open plugin directory " << dir);
00033     return;
00034   }
00035   PTRACE(4, "Enumerating plugin directory " << dir);
00036   do {
00037     PString entry = dir + dir.GetEntryName();
00038     PDirectory subdir = entry;
00039     if (subdir.Open())
00040       PLoadPluginDirectory<C>(obj, entry, suffix);
00041     else {
00042       PFilePath fn(entry);
00043       if (
00044            (fn.GetType() *= PDynaLink::GetExtension()) &&
00045            (
00046              (suffix == NULL) || (fn.GetTitle().Right(strlen(suffix)) *= suffix)
00047            )
00048          )
00049         obj.LoadPlugin(entry);
00050     }
00051   } while (dir.Next());
00052 }
00053 
00055 //
00056 //  Manager for plugins
00057 //
00058 
00059 class PPluginManager : public PObject
00060 {
00061   PCLASSINFO(PPluginManager, PObject);
00062 
00063   public:
00064     // functions to load/unload a dynamic plugin 
00065     PBoolean LoadPlugin (const PString & fileName);
00066     void LoadPluginDirectory (const PDirectory & dir);
00067   
00068     // functions to access the plugins' services 
00069     PStringArray GetPluginTypes() const;
00070     PStringArray GetPluginsProviding(const PString & serviceType) const;
00071     PPluginServiceDescriptor * GetServiceDescriptor(const PString & serviceName, const PString & serviceType) const;
00072     PObject * CreatePluginsDevice(const PString & serviceName, const PString & serviceType, int userData = 0) const;
00073     PObject * CreatePluginsDeviceByName(const PString & deviceName, const PString & serviceType, int userData = 0, const PString & serviceName = PString::Empty()) const;
00074     PStringArray GetPluginsDeviceNames(const PString & serviceName, const PString & serviceType, int userData = 0) const;
00075     PBoolean GetPluginsDeviceCapabilities(const PString & serviceType,const PString & serviceName,const PString & deviceName,void * capabilities) const;
00076 
00077     // function to register a service (used by the plugins themselves)
00078     PBoolean RegisterService (const PString & serviceName, const PString & serviceType, PPluginServiceDescriptor * descriptor);
00079 
00080     // Get the list of plugin directories
00081     static PStringArray GetPluginDirs();
00082 
00083     // static functions for accessing global instances of plugin managers
00084     static PPluginManager & GetPluginManager();
00085 
00103     void AddNotifier(
00104       const PNotifier & filterFunction,
00105       PBoolean existing = PFalse
00106     );
00107 
00108     void RemoveNotifier(
00109       const PNotifier & filterFunction
00110     );
00111 
00112   protected:
00113     void LoadPluginDirectory (const PDirectory & directory, const PStringList & suffixes);
00114     void CallNotifier(PDynaLink & dll, INT code);
00115 
00116     PMutex pluginsMutex;
00117     PArray<PDynaLink> plugins;
00118     
00119     PMutex servicesMutex;
00120     PArray<PPluginService> services;
00121 
00122     PMutex notifiersMutex;
00123     PList<PNotifier> notifiers;
00124 };
00125 
00127 //
00128 //  Manager for plugin modules
00129 //
00130 
00131 class PPluginModuleManager : public PObject
00132 {
00133   public:
00134     typedef PDictionary<PString, PDynaLink> PluginListType;
00135 
00136     PPluginModuleManager(const char * _signatureFunctionName, PPluginManager * pluginMgr = NULL);
00137 
00138     PBoolean LoadPlugin(const PString & fileName)
00139     { if (pluginMgr == NULL) return PFalse; else return pluginMgr->LoadPlugin(fileName); }
00140 
00141     void LoadPluginDirectory(const PDirectory &directory)
00142     { if (pluginMgr != NULL) pluginMgr->LoadPluginDirectory(directory); }
00143 
00144     virtual void OnLoadPlugin(PDynaLink & /*dll*/, INT /*code*/)
00145     { }
00146 
00147     virtual PluginListType GetPluginList() const
00148     { return pluginDLLs; }
00149 
00150     virtual void OnShutdown()
00151     { }
00152 
00153   protected:
00154     PluginListType pluginDLLs;
00155     PDECLARE_NOTIFIER(PDynaLink, PPluginModuleManager, OnLoadModule);
00156 
00157   protected:
00158     const char * signatureFunctionName;
00159     PPluginManager * pluginMgr;
00160 };
00161 
00162 #endif // ifndef _PLUGINMGR_H

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1