YARP
Yet Another Robot Platform
RFPlugin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/RFPlugin.h>
10 
11 #include <yarp/os/RFModule.h>
12 #include <yarp/os/YarpPlugin.h>
15 
16 using namespace std;
17 using namespace yarp::os;
18 
20  public YarpPluginSelector
21 {
22  bool select(Searchable& options) override
23  {
24  return options.check("type", Value("none")).asString() == "RFModule";
25  }
26 };
27 
29 {
33 };
34 
35 
37 {
38  Private() = default;
39 
40  string alias;
41  string name;
42  string command;
43  int threadID{0};
44  SharedRFPlugin* shared{nullptr};
45 
46  RFModule* module{nullptr};
48  {
49  delete shared;
50  }
51 };
52 
53 RFPlugin::RFPlugin() :
54  mPriv(new Private)
55 {
56 }
57 
59 {
60  delete mPriv;
61 }
62 
64 {
65  return mPriv->command;
66 }
67 
69 {
70  return mPriv->module->getThreadKey();
71 }
72 
74 {
75  mPriv->module->stopModule();
76 }
77 
79 {
80  return !mPriv->module->isStopping();
81 }
82 
83 
84 std::pair<int, char**> str2ArgcArgv(char* str)
85 {
86  enum
87  {
88  kMaxArgs = 64
89  };
90  int argc = 0;
91  char* argv[kMaxArgs];
92 
93  char* p2 = strtok(str, " ");
94  while ((p2 != nullptr) && argc < kMaxArgs - 1) {
95  argv[argc++] = p2;
96  p2 = strtok(nullptr, " ");
97  }
98  argv[argc] = nullptr;
99  return make_pair(argc, argv);
100 }
101 
102 bool RFPlugin::open(const string& inCommand)
103 {
104  ResourceFinder rf;
105  string name = inCommand.substr(0, inCommand.find(' '));
106 
107  char* str = new char[inCommand.size() + 1];
108  memcpy(str, inCommand.c_str(), inCommand.size());
109  str[inCommand.size()] = '\0';
110 
111  mPriv->command = inCommand;
112  auto argcv = str2ArgcArgv(str);
113  rf.configure(argcv.first, argcv.second);
114  delete[] str;
115 
116  RFModule* staticmodule{nullptr};
117  staticmodule = RFModuleFactory::GetInstance().GetModule(name);
118  if (staticmodule != nullptr) {
119  try {
120  if (!staticmodule->configure(rf)) {
121  return false;
122  }
123  staticmodule->runModuleThreaded();
124  mPriv->module = staticmodule;
125  return true;
126  } catch (...) {
127  return false;
128  }
129  }
130 
131  YarpPluginSettings settings;
132  mPriv->shared = new SharedRFPlugin;
133  mPriv->name = name;
134  mPriv->shared->selector.scan();
135 
136  settings.setPluginName(mPriv->name);
137 
138  if (!settings.setSelector(mPriv->shared->selector)) {
139  return false;
140  }
141  if (!mPriv->shared->yarpPlugin.open(settings)) {
142  return false;
143  }
144 
145  mPriv->shared->sharedLibClass.open(*mPriv->shared->yarpPlugin.getFactory());
146 
147  if (!mPriv->shared->sharedLibClass.isValid()) {
148  return false;
149  }
150 
151  settings.setLibraryMethodName(mPriv->shared->yarpPlugin.getFactory()->getName(), settings.getMethodName());
152  settings.setClassInfo(mPriv->shared->yarpPlugin.getFactory()->getClassName(), mPriv->shared->yarpPlugin.getFactory()->getBaseClassName());
153 
154  bool ret{false};
155  try {
156  ret = mPriv->shared->sharedLibClass.getContent().configure(rf);
157  } catch (...) {
158  return false;
159  }
160 
161  if (ret) {
162  mPriv->shared->sharedLibClass->runModuleThreaded();
163  }
164  mPriv->module = &(mPriv->shared->sharedLibClass.getContent());
165  return ret;
166 }
yarp::os::RFPlugin::close
virtual void close()
closes the plugin
Definition: RFPlugin.cpp:73
yarp::os::RFModule
A base-class for standard YARP modules that supports ResourceFinder.
Definition: RFModule.h:24
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::os::RFPlugin::Private::Private
Private()=default
yarp::os::RFPlugin::Private::command
string command
Definition: RFPlugin.cpp:42
RFModuleSelector
Definition: RFPlugin.cpp:21
yarp::os::YarpPluginSelector
Pick out a set of relevant plugins.
Definition: YarpPluginSelector.h:30
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::os::YarpPluginSettings::setSelector
bool setSelector(YarpPluginSelector &selector)
Use a selector to find a plugin or plugins.
Definition: YarpPluginSettings.h:90
yarp::os::RFPlugin::Private
Definition: RFPlugin.cpp:37
yarp::os::RFModule::runModuleThreaded
virtual int runModuleThreaded()
Calls updateModule() on a separate thread until that returns false.
Definition: RFModule.cpp:406
yarp::os::ResourceFinder::configure
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Definition: ResourceFinder.cpp:803
yarp::os::RFPlugin::getCmd
virtual std::string getCmd()
get the command line used to open the plugin
Definition: RFPlugin.cpp:63
SharedRFPlugin
Definition: RFPlugin.cpp:29
yarp::os::YarpPluginSettings::setLibraryMethodName
void setLibraryMethodName(const std::string &dll_name, const std::string &fn_name)
Set the name of the library to load and the method name to use as a factory.
Definition: YarpPluginSettings.h:47
YarpPlugin.h
yarp::os::YarpPluginSettings::setPluginName
void setPluginName(const std::string &name)
Set the name of the plugin to load.
Definition: YarpPluginSettings.h:61
yarp::os::RFPlugin::getThreadKey
virtual int getThreadKey()
get the thread id
Definition: RFPlugin.cpp:68
yarp::os::RFPlugin::Private::~Private
~Private()
Definition: RFPlugin.cpp:47
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
SharedRFPlugin::sharedLibClass
SharedLibraryClass< RFModule > sharedLibClass
Definition: RFPlugin.cpp:31
module
static RFModule * module
Definition: RFModule.cpp:234
RFModule.h
yarp::os::RFPlugin::open
virtual bool open(const std::string &command)
opens the plugin
Definition: RFPlugin.cpp:102
yarp::os::YarpPluginSettings::setClassInfo
void setClassInfo(const std::string &class_name, const std::string &baseclass_name)
Set the information about the class and the base class constructed by this plugin.
Definition: YarpPluginSettings.h:73
yarp::os::YarpPluginSelector::scan
void scan()
Find plugin configuration files, and run [plugin] sections through the select method.
Definition: YarpPlugin.cpp:212
yarp::os::YarpPluginSettings
Collect hints for finding a particular plugin.
Definition: YarpPluginSettings.h:25
yarp::os::YarpPlugin< RFModule >
yarp::os::RFPlugin::Private::alias
string alias
Definition: RFPlugin.cpp:40
YarpPluginSelector.h
str2ArgcArgv
std::pair< int, char ** > str2ArgcArgv(char *str)
Definition: RFPlugin.cpp:84
yarp::os::RFPlugin::Private::name
string name
Definition: RFPlugin.cpp:41
yarp::os::RFPlugin::isRunning
virtual bool isRunning()
check if the plugin is running
Definition: RFPlugin.cpp:78
yarp::os::YarpPluginSettings::getMethodName
std::string getMethodName() const
Definition: YarpPluginSettings.h:156
RFPlugin.h
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::SharedLibraryClass< RFModule >
SharedRFPlugin::yarpPlugin
YarpPlugin< RFModule > yarpPlugin
Definition: RFPlugin.cpp:30
RFModuleFactory.h
yarp::os::RFModuleFactory::GetModule
RFModule * GetModule(const std::string &name)
Definition: RFModuleFactory.cpp:41
yarp::os::RFPlugin::~RFPlugin
virtual ~RFPlugin()
Definition: RFPlugin.cpp:58
SharedRFPlugin::selector
RFModuleSelector selector
Definition: RFPlugin.cpp:32
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::os::RFModuleFactory::GetInstance
static RFModuleFactory & GetInstance()
Definition: RFModuleFactory.cpp:30
yarp::os::ResourceFinder
Helper class for finding config files and other external resources.
Definition: ResourceFinder.h:33