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