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