YARP
Yet Another Robot Platform
scriptbroker.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 
7 
9 #include <yarp/conf/filesystem.h>
10 
11 #include <yarp/os/Bottle.h>
12 #include <yarp/os/Network.h>
13 
14 #include <string>
15 
16 #define CONNECTION_TIMEOUT 5.0 //seconds
17 
18 
19 using namespace yarp::os;
20 using namespace yarp::manager;
21 namespace fs = yarp::conf::filesystem;
22 
25 
27 static Bottle parsePaths(const std::string& txt) {
28  Bottle result;
29  const char *at = txt.c_str();
30  int slash_tweak = 0;
31  int len = 0;
32  for (char ch : txt) {
33  if (ch==sep) {
34  result.addString(std::string(at,len-slash_tweak));
35  at += len+1;
36  len = 0;
37  slash_tweak = 0;
38  continue;
39  }
40  slash_tweak = (ch==slash && len>0)?1:0;
41  len++;
42  }
43  if (len>0) {
44  result.addString(std::string(at,len-slash_tweak));
45  }
46  return result;
47 }
48 
49 static bool fileExists(const char *fname) {
50  FILE *fp=nullptr;
51  fp = fopen(fname,"r");
52  if (fp == nullptr) {
53  return false;
54  } else {
55  fclose(fp);
56  return true;
57  }
58  }
59 
60 
61 bool ScriptLocalBroker::init(const char* szcmd, const char* szparam,
62  const char* szhost, const char* szstdio,
63  const char* szworkdir, const char* szenv )
64 {
65  OSTRINGSTREAM strDevParam;
66  std::string strParam;
67  std::string strCmd;
68  if(szcmd)
69  {
71  for (size_t i=0; i<possiblePaths.size(); ++i)
72  {
73  std::string guessString=possiblePaths.get(i).asString() +
74  std::string{slash} + szcmd;
75  const char* guess=guessString.c_str();
76  if (fileExists (guess))
77  {
78 #if defined(_WIN32)
79  strCmd = "\"" + std::string(guess) + "\"";
80 #else
81  strCmd = guess;
82 #endif
83  break;
84  }
85  }
86 
87  }
88  if (strCmd == "") {
89  return false;
90  }
91  if (szparam) {
92  strParam = szparam;
93  }
94  strDevParam<<strCmd<<" "<<strParam;
95  return LocalBroker::init(script.c_str(), strDevParam.str().c_str(),
96  szhost, szstdio, szworkdir, szenv);
97  }
98 
99 
100 bool ScriptYarprunBroker::whichFile(const char* server, const char* filename, std::string& filenameWithPath)
101 {
102  if (!strlen(server)) {
103  return false;
104  }
105 
106  yarp::os::Bottle msg, grp;
107  grp.clear();
108  grp.addString("which");
109  grp.addString(filename);
110  msg.addList() = grp;
111 
112  ContactStyle style;
113  style.quiet = true;
114  style.timeout = CONNECTION_TIMEOUT;
115  //style.carrier = carrier;
116  yarp::os::Port port;
117  port.open("...");
118 
119  bool connected = yarp::os::NetworkBase::connect(port.getName(), server, style);
120  if(!connected)
121  {
122  return false;
123  }
124 
125  yarp::os::Value filenameReader;
126  bool ret = port.write(msg, filenameReader);
127  filenameWithPath=filenameReader.asString();
128  NetworkBase::disconnect(port.getName(), server);
129  port.close();
130 
131  if(!ret)
132  {
133  return false;
134  }
135  return true;
136 }
137 
138 bool ScriptYarprunBroker::init(const char* szcmd, const char* szparam,
139  const char* szhost, const char* szstdio,
140  const char* szworkdir, const char* szenv )
141 {
142 
143  OSTRINGSTREAM strDevParam;
144  std::string strParam;
145  std::string strCmd;
146  if(szcmd)
147  {
148  std::string strHost;
149  if (szhost[0] != '/') {
150  strHost = std::string("/") + std::string(szhost);
151  } else {
152  strHost = szhost;
153  }
154  whichFile(strHost.c_str(), szcmd, strCmd);
155  }
156  if (szparam) {
157  strParam = szparam;
158  }
159  strDevParam<<strCmd<<" "<<strParam;
160  return YarpBroker::init(script.c_str(), strDevParam.str().c_str(),
161  szhost, szstdio, szworkdir, szenv);
162 }
bool ret
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:182
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:121
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:170
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:24
double timeout
Set a timeout for communication (in units of seconds, fractional seconds allowed).
Definition: ContactStyle.h:47
bool quiet
Suppress all outputs and warnings.
Definition: ContactStyle.h:36
virtual std::string getName() const
Get name of port.
Definition: Contactable.cpp:14
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition: Network.cpp:682
static bool disconnect(const std::string &src, const std::string &dest, bool quiet)
Request that an output port disconnect from an input port.
Definition: Network.cpp:700
A mini-server for network communication.
Definition: Port.h:47
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition: Port.cpp:427
void close() override
Stop port activity.
Definition: Port.cpp:354
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
A single value (typically within a Bottle).
Definition: Value.h:45
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
static constexpr char path_separator
Definition: environment.h:29
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition: environment.h:68
static constexpr value_type preferred_separator
Definition: filesystem.h:23
std::stringstream OSTRINGSTREAM
Definition: utility.h:49
An interface to the operating system, including Port based communication.
#define CONNECTION_TIMEOUT
constexpr fs::value_type slash
constexpr auto sep
static Bottle parsePaths(const std::string &txt)
static bool fileExists(const char *fname)