Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Plugin.C

00001 /* $Id: Plugin.C,v 1.2 2000/11/03 22:03:28 rpav Exp $
00002  *
00003  * SFW - Studio FrameWork
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program 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
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*
00023  * Desc
00024  *
00025  */ 
00026 
00027 #include <iostream>
00028 #include <string>
00029 #include <dlfcn.h>
00030 #include <SFW/Plugin.H>
00031 #include <SFW/Log.H>
00032 
00033 using namespace SFW;
00034 
00035 TYPE_INIT(SFW::Plugin);
00036 
00037 Plugin::Plugin(string _path) {
00038     path     = _path;
00039     type     = "<unknown>";
00040     dlHandle = (void*)0;
00041 
00042     /* Fix weird paths */
00043     string::size_type i;
00044 
00045     while((i = path.find("//")) != string::npos)
00046     path.erase(i,1);
00047     
00048     /* Temporary open, grab some info, close */
00049     load();
00050     unload();
00051 }
00052 
00053 Plugin::~Plugin() {
00054     mutex.lock();
00055     unload();
00056     mutex.unlock();
00057 }
00058 
00059 bool Plugin::isLoaded() {
00060     mutex.lock();
00061     bool ret = static_cast<bool>(dlHandle);
00062     mutex.unlock();
00063 
00064     return ret;
00065 }
00066 
00067 string Plugin::getPath() {
00068     return path;
00069 }
00070 
00071 string Plugin::getIDString() {
00072     mutex.lock();
00073     string ret = idstring;
00074     mutex.unlock();
00075     
00076     return idstring;
00077 }
00078 
00079 string Plugin::getType() {
00080     mutex.lock();
00081     string ret = type;
00082     mutex.unlock();
00083     
00084     return ret;
00085 }
00086 
00087 /* Load functions as reload if it's already loaded */
00088 bool Plugin::load() {
00089     mutex.lock();
00090     
00091     if(isLoaded())
00092     unload();
00093     
00094     /* Grab info again in case it's changed */
00095     dlHandle = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
00096     if(!dlHandle) {
00097     Log(L_WARN,
00098         "Plugin: Error querying plugin, path %s\n" 
00099         "        Reason: %s", path.c_str(), dlerror());
00100 
00101     goto error;
00102     }
00103 
00104     const char * (*getIDString)();
00105     const char * (*getType)();
00106     char          *error;
00107 
00108     getIDString = (const char * (*)())dlsym(dlHandle, "getIDString");
00109     if((error = dlerror())) {
00110     Log(L_WARN,
00111         "Plugin: Warning: unable to query ID string\n"
00112         "        Reason:  %s", error);
00113     idstring = "Unknown (" + path + ")";
00114     } else {
00115     idstring = string((getIDString)());
00116     }
00117     
00118     getType = (const char * (*)())dlsym(dlHandle, "getType");
00119     if((error = dlerror())) {
00120     Log(L_WARN,
00121         "Plugin: Warning: unable to query type (perhaps this isn't a module?)\n"
00122         "        Reason:  %s", error);
00123     goto error;
00124     } else {
00125     type = string((getType)());
00126     }
00127 
00128     factory = (FactoryFn)dlsym(dlHandle, "factory");
00129     if((error = dlerror())) {
00130     Log(L_WARN,
00131         "Plugin: Warning: unable to get get the factory for %s\n"
00132         "        Reason:  %s", idstring.c_str(), error);
00133     goto error;
00134     }
00135 
00136     mutex.unlock();
00137     return true;
00138 
00139  error:
00140     idstring = "[Broken] " + path;
00141     
00142     unload();
00143     mutex.unlock();
00144     return false;
00145 }
00146 
00147 bool Plugin::unload() {
00148     mutex.lock();
00149     
00150     if(!isLoaded()) {
00151     mutex.unlock();
00152     return false;
00153     }
00154     
00155     int c = dlclose(dlHandle);
00156     
00157     if(c)
00158     Log(L_DBG0, "Plugin: Warning: dlclose on %s returned non-zero value %d",
00159         path.c_str(), c);
00160 
00161     dlHandle = (void*)0;
00162 
00163     mutex.unlock();
00164     return true;
00165 }
00166 
00167 Object* Plugin::makeObject() {
00168     mutex.lock();
00169 
00170     if(!isLoaded()) {
00171     Log(L_DBG0,
00172         "Plugin: Warning: makeObject() called on unloaded plugin, loading\n"
00173         "        Info:    plugin type: %s ID: '%s'",
00174         type.c_str(), idstring.c_str());
00175     load();
00176     }
00177 
00178     Object *ret = (*factory)();
00179     mutex.unlock();
00180 
00181     return ret;
00182 }

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000