YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
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
19using namespace yarp::os;
20using namespace yarp::manager;
21namespace fs = yarp::conf::filesystem;
22
25
27static 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
49static 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
61bool ScriptLocalBroker::init(const char* szcmd, const char* szparam,
62 const char* szhost, const char* szstdio,
63 const char* szworkdir, const char* szenv )
64{
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(),
97 }
98
99
100bool ScriptYarprunBroker::whichFile(const std::string& server, const std::string& filename, std::string& filenameWithPath)
101{
102 if (server.empty()) {
103 return false;
104 }
105
107 grp.clear();
108 grp.addString("which");
109 grp.addString(filename);
110 msg.addList() = grp;
111
112 ContactStyle style;
113 style.quiet = true;
115 //style.carrier = carrier;
116 yarp::os::Port port;
117 port.open("...");
118
120 if(!connected)
121 {
122 return false;
123 }
124
126 bool ret = port.write(msg, filenameReader);
129 port.close();
130
131 if(!ret)
132 {
133 return false;
134 }
135 return true;
136}
137
138bool ScriptYarprunBroker::init(const char* szcmd, const char* szparam,
139 const char* szhost, const char* szstdio,
140 const char* szworkdir, const char* szenv )
141{
142
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(),
162}
bool ret
static yarp::os::Bottle parsePaths(const std::string &txt)
Definition Run.cpp:101
constexpr fs::value_type slash
Definition Run.cpp:98
static bool fileExists(const char *fname)
Definition Run.cpp:124
bool connected(const std::string &from, const std::string &to, const std::string &carrier) override
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition Bottle.cpp:182
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
A mini-server for performing network communication in the background.
Preferences for how to communicate with a contact.
double timeout
Set a timeout for communication (in units of seconds, fractional seconds allowed).
bool quiet
Suppress all outputs and warnings.
virtual std::string getName() const
Get name of port.
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:46
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:436
void close() override
Stop port activity.
Definition Port.cpp:363
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:43
#define CONNECTION_TIMEOUT
static constexpr char path_separator
Definition environment.h:27
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
Definition environment.h:66
static constexpr value_type preferred_separator
Definition filesystem.h:21
std::stringstream OSTRINGSTREAM
Definition utility.h:50
An interface to the operating system, including Port based communication.
constexpr fs::value_type slash
constexpr auto sep
static Bottle parsePaths(const std::string &txt)
static bool fileExists(const char *fname)