PCMPlugin.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00025 //
00026 // Inner includes
00027 //
00028 
00029 #include "../include/PCMPlugin.h"
00030 
00031 namespace ParCompMark
00032 {
00033 
00034   //
00035   // Constructors & destructor
00036   //
00037 
00038   Plugin::Plugin(const PluginType & type, const std::string & name, const std::string & filename):
00039         // Parent initializer 
00040   DynLoad(filename              /* Plugin file name. */
00041   ),
00042         // Parent initializer 
00043   Name(name                     /* Name of the plugin. */
00044         )
00045         // You have to initialize the following attributes:
00046         // - mPluginType
00047         // - mInitialized
00048         // - mNeededLibs
00049         // - mLastError
00050   {
00051         mPluginType = type;
00052         mInitialized = false;
00053         mLastError = 0;
00054 
00055         _getNeededLibs();
00056         _setPluginHandle();
00057         _setLoggerFunction();
00058 
00059         Logger::getInstance()->log(Logger::NOTICE,
00060                                  "Plugin `" + mName + "' has been created (`" + mLibraryName + "\').");
00061   }
00062 
00063  /*----------------------------------------------------------------------*/
00064 
00065   Plugin::~Plugin()
00066   {
00067         if(mInitialized)
00068          finalize();
00069 
00070         Logger::getInstance()->log(Logger::NOTICE,
00071                                  "Plugin `" + mName + "' has been destroyed (`" + mLibraryName + "\').");
00072   }
00073 
00074  /*----------------------------------------------------------------------*/
00075 
00076   //
00077   // Class methods
00078   //
00079 
00080   void Plugin::_loggerFunction(const void *handle, const char *message)
00081   {
00082         const Plugin *plugin = static_cast < const Plugin * >(handle);
00083 
00084         Logger::getInstance()->log(Logger::NOTICE, "Plugin `" + plugin->getName() + "\' message: " + message);
00085   }
00086 
00087  /*----------------------------------------------------------------------*/
00088 
00089   //
00090   // Methods
00091   //
00092 
00093   void Plugin::initialize()
00094   {
00095         Assert(!mInitialized, INVALID_OPERATION_ERROR, "Plugin::initialize()");
00096 
00097         _onLoad();
00098 
00099         // Plugin specific initialization method
00100         _initializeSpecific();
00101 
00102         mInitialized = true;
00103         Logger::getInstance()->log(Logger::DEBUG, "Plugin `" + mName + "' has been initialized.");
00104   }
00105 
00106  /*----------------------------------------------------------------------*/
00107 
00108   void Plugin::finalize()
00109   {
00110         Assert(mInitialized, INVALID_OPERATION_ERROR, "Plugin::finalize()");
00111 
00112         // Plugin specific initialization method
00113         // TODO: causes pure virtual call in PluginManager::unloadPlugins()
00114         //_finalizeSpecific();
00115 
00116         _onUnload();
00117 
00118         mInitialized = false;
00119 
00120         Logger::getInstance()->log(Logger::DEBUG, "Plugin `" + mName + "' has been finalized.");
00121   }
00122 
00123  /*----------------------------------------------------------------------*/
00124 
00125   void Plugin::_getNeededLibs()
00126   {
00127         // Jump out if no such function
00128         if(!hasFunction("pcmGetNeededLibs"))
00129          return;
00130 
00131         pcmGetNeededLibsType pcmGetNeededLibs = (pcmGetNeededLibsType) getFunction("pcmGetNeededLibs");
00132         const char **neededLibs = pcmGetNeededLibs();
00133 
00134         if(neededLibs)
00135          for(const char **lib = neededLibs; *lib; lib++)
00136                 mNeededLibs.push_back(*lib);
00137 
00138         Logger::getInstance()->log(Logger::DEBUG, "Needed libraries are retrieved from plugin `" + mName + "\'.");
00139   }
00140 
00141  /*----------------------------------------------------------------------*/
00142 
00143   void Plugin::_setPluginHandle()
00144   {
00145         // Jump out if no such function
00146         if(!hasFunction("pcmSetPluginHandle"))
00147          return;
00148 
00149         pcmSetPluginHandleType pcmSetPluginHandle = (pcmSetPluginHandleType) getFunction("pcmSetPluginHandle");
00150 
00151         mLastError = pcmSetPluginHandle((void *) this);
00152         _checkError();
00153 
00154         Logger::getInstance()->log(Logger::DEBUG, "Plugin handle is set for plugin `" + mName + "\'.");
00155   }
00156 
00157  /*----------------------------------------------------------------------*/
00158 
00159   void Plugin::_setLoggerFunction()
00160   {
00161         // Jump out if no such function
00162         if(!hasFunction("pcmSetLoggerFunction"))
00163          return;
00164 
00165         pcmSetLoggerFunctionType pcmSetLoggerFunction = (pcmSetLoggerFunctionType) getFunction("pcmSetLoggerFunction");
00166 
00167         mLastError = pcmSetLoggerFunction(Plugin::_loggerFunction);
00168         _checkError();
00169 
00170         Logger::getInstance()->log(Logger::DEBUG, "Logger function is set for plugin `" + mName + "\'.");
00171   }
00172 
00173  /*----------------------------------------------------------------------*/
00174 
00175   void Plugin::_onLoad()
00176   {
00177         // Jump out if no such function
00178         if(!hasFunction("pcmOnLoad"))
00179          return;
00180 
00181         pcmOnLoadType pcmOnLoad = (pcmOnLoadType) getFunction("pcmOnLoad");
00182 
00183         mLastError = pcmOnLoad();
00184         _checkError();
00185 
00186         Logger::getInstance()->log(Logger::DEBUG, "pcmLoad event handler is called for plugin `" + mName + "\'.");
00187   }
00188 
00189  /*----------------------------------------------------------------------*/
00190 
00191   void Plugin::_onUnload()
00192   {
00193         // Jump out if no such function
00194         if(!hasFunction("pcmOnLoad"))
00195          return;
00196 
00197         pcmOnUnloadType pcmOnUnload = (pcmOnUnloadType) getFunction("pcmOnLoad");
00198 
00199         mLastError = pcmOnUnload();
00200         _checkError();
00201 
00202         Logger::getInstance()->log(Logger::DEBUG, "pcmUnload event handler is called for plugin `" + mName + "\'.");
00203   }
00204 
00205  /*----------------------------------------------------------------------*/
00206 
00207   const char *Plugin::_getErrorMsg()
00208   {
00209         // Jump out if no such function
00210         if(!hasFunction("pcmGetErrorMsg"))
00211          return 0;
00212 
00213         pcmGetErrorMsgType pcmGetErrorMsg = (pcmGetErrorMsgType) getFunction("pcmGetErrorMsg");
00214 
00215         const char *msg = pcmGetErrorMsg(mLastError);
00216 
00217         Logger::getInstance()->log(Logger::DEBUG, "pcmGetErrorMsg function is called for plugin `" + mName + "\'.");
00218 
00219         return msg;
00220   }
00221 
00222  /*----------------------------------------------------------------------*/
00223 
00224 }