6 int ClientCommunications::CONTROLLER_CLIENT_COUNT = 0;
10 : inputCallback(*this)
12 clientNumber = ++ClientCommunications::CONTROLLER_CLIENT_COUNT;
14 while(yarp.exists((
"/ControllerClient/"+ std::to_string(clientNumber) +
"/rpc:o")))
19 rpcClientPort_Name =
"/ControllerClient/"+ std::to_string(clientNumber) +
"/rpc:o";
20 inputPort_Name =
"/ControllerClient/"+ std::to_string(clientNumber) +
":i";
26 --ClientCommunications::CONTROLLER_CLIENT_COUNT;
31 rpcClientPort.open(rpcClientPort_Name.c_str());
32 rpcClientPort.setReader(*
this);
33 inputPort.open(inputPort_Name.c_str());
34 inputPort.setReader(inputCallback);
36 bool isConOpen = openServerConnections(timeout);
37 if(isConOpen && connectToTasks)
39 isConOpen &= openTaskConnections();
43 for(
auto rpc_i : taskRpcClients)
45 yarp::os::Bottle message, reply;
47 rpc_i.second->write(message, reply);
50 yLog.error() <<
"Couldn't connect to the individual task ports.";
58 std::vector<std::string> retVec;
59 for(
auto rpc_i : taskRpcClients)
61 yarp::os::Bottle message, reply;
63 rpc_i.second->write(message, reply);
64 retVec.push_back(reply.get(0).asString());
71 rpcClientPort.close();
74 for(
auto rpc_i : taskRpcClients)
76 rpc_i.second->close();
78 taskRpcClients.clear();
84 if(taskRpcClients.find(taskName) != taskRpcClients.end())
86 taskRpcClients[taskName]->close();
87 taskRpcClients.erase(taskName);
93 yarp::os::Bottle input;
95 if (!input.read(connection)){
104 bool ClientCommunications::openServerConnections(
double timeout)
106 if (!yarp.checkNetwork()) {
107 yLog.error() <<
"Yarp network isn't running.";
111 bool connected =
false;
112 double timeDelayed = 0.0;
113 double delayTime = 0.01;
114 while(!connected && (timeDelayed < timeout))
116 connected = yarp.connect(rpcClientPort_Name.c_str(),
"/ControllerServer/rpc:i");
117 yarp::os::Time::delay(delayTime);
118 timeDelayed += delayTime;
119 if (timeDelayed>= timeout) {
120 yLog.error() <<
"Could not connect to the ocra controller server. Are you sure it is running?";
140 std::vector<std::string> portNameVec;
141 yarp::os::Bottle message, reply;
143 rpcClientPort.write(message, reply);
144 for(
auto i=0; i<reply.size(); ++i)
146 portNameVec.push_back(reply.get(i).asString());
153 yarp::os::Bottle message, reply;
155 message.addString(taskName);
156 rpcClientPort.write(message, reply);
158 std::string portName =
"";
159 if (reply.size()>0) {
160 portName = reply.get(0).asString();
167 std::vector<std::string> nameVec;
168 yarp::os::Bottle message, reply;
170 rpcClientPort.write(message, reply);
171 for(
auto i=0; i<reply.size(); ++i)
173 nameVec.push_back(reply.get(i).asString());
180 if(taskRpcClients.find(taskName) != taskRpcClients.end())
182 return taskRpcClients[taskName];
191 yarp::os::Bottle reply;
192 rpcClientPort.write(requestBottle, reply);
198 yarp::os::Bottle requestBottle, reply;
199 requestBottle.addInt(request);
200 rpcClientPort.write(requestBottle, reply);
206 yarp::os::Bottle requestBottle, reply;
207 for(
auto request : requestVector){
208 requestBottle.addInt(request);
210 rpcClientPort.write(requestBottle, reply);
214 bool ClientCommunications::openTaskConnections()
219 bool taskConnected = taskNames.size() == taskPortNames.size();
223 for(
auto i=0; i<taskPortNames.size(); ++i)
225 std::string tmpTaskPortName =
"/ControllerClient/" + std::to_string(clientNumber) +
"/" + taskNames[i] +
":o";
226 taskRpcClients[taskNames[i]] = std::make_shared<yarp::os::RpcClient>();
227 taskRpcClients[taskNames[i]]->open(tmpTaskPortName.c_str());
228 taskConnected &= yarp.connect(tmpTaskPortName.c_str(), taskPortNames[i].c_str());
231 yLog.error() <<
"The number of task ports and names does not match! Can't connect to task RPC ports.";
234 return taskConnected;
239 int btlSize = input.size();
240 for (
int i=0; i<btlSize;)
242 switch (input.get(i).asInt()) {
246 std::cout <<
"Got message: REMOVE_TASK_PORT - " << input.get(i).asString() << std::endl;
247 close(input.get(i).asString());
254 std::cout <<
"Got message: HELP." << std::endl;
260 std::cout <<
"Got message: UNKNOWN." << std::endl;
276 yarp::os::Bottle input;
277 if (input.read(connection)){
287 int btlSize = input.size();
288 for (
auto i=0; i<btlSize; ++i) {
289 switch (input.get(i).asInt()) {
294 yLog.info() <<
"Removing task " << input.get(i).asString();
295 close(input.get(i).asString());
void parseMessage(yarp::os::Bottle &input)
virtual ~ClientCommunications()
std::vector< std::string > getTaskTypes()
yarp::os::Bottle queryController(yarp::os::Bottle &requestBottle)
std::vector< std::string > getTaskNames()
virtual bool read(yarp::os::ConnectionReader &connection)
bool parseInput(yarp::os::Bottle &input)
bool open(double timeout=20.0, bool connectToTasks=true)
std::vector< std::string > getTaskPortNames()
std::shared_ptr< yarp::os::RpcClient > getTaskClient(const std::string &taskName)
std::string getTaskPortName(const std::string &taskName)
SERVER_COMMUNICATIONS_MESSAGE