plugin.cpp

00001 
00002 /***************************************************************************
00003  *  plugin.cpp - XML-RPC methods related to plugin management
00004  *
00005  *  Created: Mon Aug 31 00:56:55 2009
00006  *  Copyright  2006-2009  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "plugin.h"
00024 #include <plugin/manager.h>
00025 #include <utils/logging/logger.h>
00026 
00027 #include <algorithm>
00028 #include <map>
00029 #include <string>
00030 #include <vector>
00031 
00032 #include <xmlrpc-c/girerr.hpp>
00033 
00034 /** @class XmlRpcPluginMethods "plugin.h"
00035  * Wrapper class for plugin related XML-RPC methods.
00036  * @author Tim Niemueller
00037  */
00038 
00039 /** Constructor.
00040  * @param registry XML registry, methods will be automatically registered
00041  * @param plugin_manager plugin manager used for listing, loading and unloading plugins
00042  * @param logger logger to output informational and error messages
00043  */
00044 XmlRpcPluginMethods::XmlRpcPluginMethods(xmlrpc_c::registry *registry,
00045                                          fawkes::PluginManager *plugin_manager,
00046                                          fawkes::Logger *logger)
00047 {
00048   __xmlrpc_registry = registry;
00049   __plugin_manager  = plugin_manager;
00050   __logger          = logger;
00051   __plugin_list     = new plugin_list(plugin_manager);
00052   __plugin_load     = new plugin_load(plugin_manager, logger);
00053   __plugin_unload   = new plugin_unload(plugin_manager, logger);
00054   __xmlrpc_registry->addMethod("plugin.list",   __plugin_list);
00055   __xmlrpc_registry->addMethod("plugin.load",   __plugin_load);
00056   __xmlrpc_registry->addMethod("plugin.unload", __plugin_unload);
00057 }
00058 
00059 /** Destructor. */
00060 XmlRpcPluginMethods::~XmlRpcPluginMethods()
00061 {
00062   delete __plugin_list;
00063   delete __plugin_load;
00064   delete __plugin_unload;
00065 }
00066 
00067 
00068 /** @class XmlRpcPluginMethods::plugin_list "plugin.h"
00069  * Plugin list XML-RPC method.
00070  * @author Tim Niemueller
00071  */
00072 
00073 /** Constructor.
00074  * @param plugin_manager plugin manager to query for plugin listings.
00075  */
00076 XmlRpcPluginMethods::plugin_list::plugin_list(fawkes::PluginManager *plugin_manager)
00077 {
00078   _signature = "A:";
00079   _help = "Returns array of plugins. Each entry is a struct consisting of the "
00080     "entries name, desc, and loaded.";
00081 
00082   __plugin_manager = plugin_manager;
00083 }
00084 
00085 /** Virtual empty destructor. */
00086 XmlRpcPluginMethods::plugin_list::~plugin_list()
00087 {
00088 }
00089 
00090 /** Execute method.
00091  * @param params parameters
00092  * @param result result value
00093  */
00094 void
00095 XmlRpcPluginMethods::plugin_list::execute(xmlrpc_c::paramList const& params,
00096                                           xmlrpc_c::value *   const  result)
00097 {
00098   std::list<std::pair<std::string, std::string> > available_plugins;
00099   std::list<std::string> loadedp;
00100   available_plugins = __plugin_manager->get_available_plugins();
00101   loadedp           = __plugin_manager->get_loaded_plugins();
00102 
00103   loadedp.sort();
00104   
00105   std::vector<xmlrpc_c::value> array;
00106 
00107   std::list<std::pair<std::string, std::string> >::iterator i;
00108   for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
00109     std::map<std::string, xmlrpc_c::value> elem;
00110     elem.insert(std::make_pair("name", xmlrpc_c::value_string(i->first)));
00111     elem.insert(std::make_pair("desc", xmlrpc_c::value_string(i->second)));
00112     bool loaded = std::binary_search(loadedp.begin(), loadedp.end(), i->first);
00113     elem.insert(std::make_pair("loaded", xmlrpc_c::value_boolean(loaded)));
00114     array.push_back(xmlrpc_c::value_struct(elem));
00115   }
00116 
00117   *result = xmlrpc_c::value_array(array);
00118 }
00119 
00120 
00121 /** @class XmlRpcPluginMethods::plugin_load "plugin.h"
00122  * XML-RPC method to load a plugin.
00123  * @author Tim Niemueller
00124  */
00125 
00126 
00127 /** Constructor.
00128  * @param plugin_manager plugin manager to query for plugin listings.
00129  * @param logger logger to report problems
00130  */
00131 XmlRpcPluginMethods::plugin_load::plugin_load(fawkes::PluginManager *plugin_manager,
00132                                               fawkes::Logger *logger)
00133 {
00134   _signature = "b:s";
00135   _help = "Load plugin specified as argument, returns true on success, false otherwise.";
00136 
00137   __plugin_manager = plugin_manager;
00138   __logger         = logger;
00139 }
00140 
00141 /** Virtual empty destructor. */
00142 XmlRpcPluginMethods::plugin_load::~plugin_load()
00143 {
00144 }
00145 
00146 /** Execute method.
00147  * @param params parameters
00148  * @param result result value
00149  */
00150 void
00151 XmlRpcPluginMethods::plugin_load::execute(xmlrpc_c::paramList const& params,
00152                                           xmlrpc_c::value *   const  result)
00153 {
00154   try {
00155     std::string plugin_name = params.getString(0);
00156     __plugin_manager->load(plugin_name.c_str());
00157   } catch (girerr::error &e) {
00158     throw xmlrpc_c::fault(e.what(), xmlrpc_c::fault::CODE_UNSPECIFIED);
00159   } catch (fawkes::Exception &e) {
00160     __logger->log_warn("XML-RPC plugin.load", e);
00161     *result = xmlrpc_c::value_boolean(false);
00162   }
00163 
00164   *result = xmlrpc_c::value_boolean(true);
00165 }
00166 
00167 
00168 
00169 /** @class XmlRpcPluginMethods::plugin_unload "plugin.h"
00170  * XML-RPC method to unload a plugin.
00171  * @author Tim Niemueller
00172  */
00173 
00174 /** Constructor.
00175  * @param plugin_manager plugin manager to query for plugin listings.
00176  * @param logger logger to report problems
00177  */
00178 XmlRpcPluginMethods::plugin_unload::plugin_unload(fawkes::PluginManager *plugin_manager,
00179                                                   fawkes::Logger *logger)
00180 {
00181   _signature = "b:s";
00182   _help = "Unload plugin specified as argument, returns true on success, false otherwise.";
00183 
00184   __plugin_manager = plugin_manager;
00185   __logger         = logger;
00186 }
00187 
00188 /** Virtual empty destructor. */
00189 XmlRpcPluginMethods::plugin_unload::~plugin_unload()
00190 {
00191 }
00192 
00193 /** Execute method.
00194  * @param params parameters
00195  * @param result result value
00196  */
00197 void
00198 XmlRpcPluginMethods::plugin_unload::execute(xmlrpc_c::paramList const& params,
00199                                           xmlrpc_c::value *   const  result)
00200 {
00201   try {
00202     std::string plugin_name = params.getString(0);
00203     __plugin_manager->unload(plugin_name.c_str());
00204   } catch (girerr::error &e) {
00205     throw xmlrpc_c::fault(e.what(), xmlrpc_c::fault::CODE_UNSPECIFIED);
00206   } catch (fawkes::Exception &e) {
00207     __logger->log_warn("XML-RPC plugin.unload", e);
00208     *result = xmlrpc_c::value_boolean(false);
00209   }
00210 
00211   *result = xmlrpc_c::value_boolean(true);
00212 }

Generated on Tue Feb 22 13:32:26 2011 for Fawkes API by  doxygen 1.4.7