Revision: 8662
          http://playerstage.svn.sourceforge.net/playerstage/?rev=8662&view=rev
Author:   hsujohnhsu
Date:     2010-05-06 17:56:14 +0000 (Thu, 06 May 2010)

Log Message:
-----------
add helper functions for GazeboConfig.  Add pluginPaths for dynamically 
loadable plugins.

Modified Paths:
--------------
    code/gazebo/trunk/server/GazeboConfig.cc
    code/gazebo/trunk/server/GazeboConfig.hh
    code/gazebo/trunk/server/controllers/ControllerFactory.cc

Modified: code/gazebo/trunk/server/GazeboConfig.cc
===================================================================
--- code/gazebo/trunk/server/GazeboConfig.cc    2010-05-06 17:49:44 UTC (rev 
8661)
+++ code/gazebo/trunk/server/GazeboConfig.cc    2010-05-06 17:56:14 UTC (rev 
8662)
@@ -60,34 +60,16 @@
 
   char *ogre_resource_path = getenv("OGRE_RESOURCE_PATH");
   if(ogre_resource_path) 
-  {
-    std::string str(ogre_resource_path);
-    int pos1 = 0;
-    int pos2 = str.find(delim);
-    while (pos2 != (int)std::string::npos)
-    {
-      this->ogrePaths.push_back(str.substr(pos1,pos2-pos1));
-      pos1 = pos2+1;
-      pos2 = str.find(delim,pos2+1);
-    }
-    this->ogrePaths.push_back(str.substr(pos1,str.size()-pos1));
-  }
+    this->AddOgrePaths(std::string(ogre_resource_path));
 
   char *gazebo_resource_path = getenv("GAZEBO_RESOURCE_PATH");
   if(gazebo_resource_path) 
-  {
-    std::string str(gazebo_resource_path);
-    int pos1 = 0;
-    int pos2 = str.find(delim);
-    while (pos2 != (int)std::string::npos)
-    {
-      this->gazeboPaths.push_back(str.substr(pos1,pos2-pos1));
-      pos1 = pos2+1;
-      pos2 = str.find(delim,pos2+1);
-    }
-    this->gazeboPaths.push_back(str.substr(pos1,str.size()-pos1));
-  }
+    this->AddGazeboPaths(std::string(gazebo_resource_path));
 
+  char *gazebo_plugin_path = getenv("GAZEBO_PLUGIN_PATH");
+  if(gazebo_plugin_path) 
+    this->AddPluginPaths(std::string(gazebo_plugin_path));
+
   if (cfgFile.is_open())
   {
     XMLConfig rc;
@@ -146,3 +128,59 @@
 {
   return this->ogrePaths;
 }
+
+std::list<std::string> &GazeboConfig::GetPluginPaths()
+{
+  return this->pluginPaths;
+}
+
+void GazeboConfig::AddGazeboPaths(std::string gazebo_resource_path)
+{
+  std::string delim(":");
+  if(!gazebo_resource_path.empty()) 
+  {
+    int pos1 = 0;
+    int pos2 = gazebo_resource_path.find(delim);
+    while (pos2 != (int)std::string::npos)
+    {
+      this->gazeboPaths.push_back(gazebo_resource_path.substr(pos1,pos2-pos1));
+      pos1 = pos2+1;
+      pos2 = gazebo_resource_path.find(delim,pos2+1);
+    }
+    
this->gazeboPaths.push_back(gazebo_resource_path.substr(pos1,gazebo_resource_path.size()-pos1));
+  }
+}
+
+void GazeboConfig::AddOgrePaths(std::string ogre_resource_path)
+{
+  std::string delim(":");
+  if(!ogre_resource_path.empty()) 
+  {
+    int pos1 = 0;
+    int pos2 = ogre_resource_path.find(delim);
+    while (pos2 != (int)std::string::npos)
+    {
+      this->ogrePaths.push_back(ogre_resource_path.substr(pos1,pos2-pos1));
+      pos1 = pos2+1;
+      pos2 = ogre_resource_path.find(delim,pos2+1);
+    }
+    
this->ogrePaths.push_back(ogre_resource_path.substr(pos1,ogre_resource_path.size()-pos1));
+  }
+}
+
+void GazeboConfig::AddPluginPaths(std::string gazebo_plugin_path)
+{
+  std::string delim(":");
+  if(!gazebo_plugin_path.empty()) 
+  {
+    int pos1 = 0;
+    int pos2 = gazebo_plugin_path.find(delim);
+    while (pos2 != (int)std::string::npos)
+    {
+      this->pluginPaths.push_back(gazebo_plugin_path.substr(pos1,pos2-pos1));
+      pos1 = pos2+1;
+      pos2 = gazebo_plugin_path.find(delim,pos2+1);
+    }
+    
this->pluginPaths.push_back(gazebo_plugin_path.substr(pos1,gazebo_plugin_path.size()-pos1));
+  }
+}

Modified: code/gazebo/trunk/server/GazeboConfig.hh
===================================================================
--- code/gazebo/trunk/server/GazeboConfig.hh    2010-05-06 17:49:44 UTC (rev 
8661)
+++ code/gazebo/trunk/server/GazeboConfig.hh    2010-05-06 17:56:14 UTC (rev 
8662)
@@ -53,12 +53,27 @@
     /// \brief Get paths to ogre install
     public: std::list<std::string>& GetOgrePaths();
  
+    /// \brief Get plugin paths
+    public: std::list<std::string>& GetPluginPaths();
+ 
+    /// \brief Add colon delimited paths to Gazebo install 
+    public: void AddGazeboPaths(std::string path);
+
+    /// \brief Add colon delimited paths to ogre install
+    public: void AddOgrePaths(std::string path);
+ 
+    /// \brief Add colon delimited paths to plugins
+    public: void AddPluginPaths(std::string path);
+ 
     /// Paths gazebo install
     private: std::list<std::string> gazeboPaths;
     
     /// Paths to the ogre install
     private: std::list<std::string> ogrePaths;
 
+    /// Paths to the plugins
+    private: std::list<std::string> pluginPaths;
+
   };
 
 

Modified: code/gazebo/trunk/server/controllers/ControllerFactory.cc
===================================================================
--- code/gazebo/trunk/server/controllers/ControllerFactory.cc   2010-05-06 
17:49:44 UTC (rev 8661)
+++ code/gazebo/trunk/server/controllers/ControllerFactory.cc   2010-05-06 
17:56:14 UTC (rev 8662)
@@ -33,6 +33,10 @@
 #include "Controller.hh"
 #include "ControllerFactory.hh"
 
+#include <sys/stat.h>
+#include "Simulator.hh"
+#include "GazeboConfig.hh"
+
 #ifdef HAVE_DL
 #include <dlfcn.h>
 #elif HAVE_LTDL
@@ -73,13 +77,27 @@
 // Load a controller plugin. Used by Model and Sensor when creating 
controllers.
 void ControllerFactory::LoadPlugin(const std::string &plugin, const 
std::string &classname)
 {
+  // search and expand plugin with full path by searching 
GazeboConfig::pluginPaths,
+  // otherwise, leave as is, and let LD_LIBRARY_PATH take care of business
+  struct stat st;
+  bool found = false;
+  std::string fullname;
+  std::list<std::string>::iterator iter;
+  std::list<std::string> 
pluginPaths=Simulator::Instance()->GetGazeboConfig()->GetPluginPaths();
+  for (iter=pluginPaths.begin(); iter!=pluginPaths.end(); ++iter)
+  {
+    fullname = (*iter)+std::string("/")+plugin;
+    if (stat(fullname.c_str(), &st) == 0) {found=true; break;}
+  }
+  if (!found) fullname = plugin;
+
 #ifdef HAVE_DL
 
-  void* handle = dlopen(plugin.c_str(), RTLD_LAZY|RTLD_GLOBAL);
+  void* handle = dlopen(fullname.c_str(), RTLD_LAZY|RTLD_GLOBAL);
   if (!handle)
   {
     std::ostringstream stream;
-    stream << "Failed to load " << plugin << ": " << dlerror();
+    stream << "Failed to load " << fullname << ": " << dlerror();
     gzthrow(stream.str());
   }
 
@@ -113,12 +131,12 @@
                        init_done = true;
        }
        
-       lt_dlhandle handle = lt_dlopenext(plugin.c_str());
+       lt_dlhandle handle = lt_dlopenext(fullname.c_str());
        
        if (!handle)
        {
                std::ostringstream stream;
-               stream << "Failed to load " << plugin << ": " << lt_dlerror();
+               stream << "Failed to load " << fullname << ": " << lt_dlerror();
                gzthrow(stream.str());
        }
        


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
Playerstage-commit mailing list
Playerstage-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to