ocra-recipes
Doxygen documentation for the ocra-recipes repository
ServerCommunications.cpp
Go to the documentation of this file.
2 
3 
4 using namespace ocra_recipes;
5 
7 {
8 }
9 
10 ServerCommunications::ServerCommunications(ocra::Controller::Ptr ctrl, ocra::Model::Ptr mdl)
11 : model(mdl),
12  controller(ctrl)
13 {
14  rpcServerPort_Name = "/ControllerServer/rpc:i";
15  outputPort_Name = "/ControllerServer:o";
16 }
17 
19 {
20  close();
21 }
22 
24 {
25  bool res = true;
26  res &= rpcServerPort.open(rpcServerPort_Name.c_str());
27  rpcServerPort.setReader(*this);
28  res &= outputPort.open(outputPort_Name.c_str());
29  return res;
30 }
32 {
33  rpcServerPort.close();
34  outputPort.close();
35 }
36 
37 bool ServerCommunications::read(yarp::os::ConnectionReader& connection)
38 {
39  yarp::os::Bottle input, reply;
40 
41  if (!input.read(connection)){
42  return false;
43  }
44  parseMessage(input, reply);
45  yarp::os::ConnectionWriter* returnToSender = connection.getWriter();
46  if (returnToSender!=NULL) {
47  if (!reply.write(*returnToSender)) {
48  OCRA_ERROR("Error writing reply to sender");
49  return false;
50  }
51  }
52  return true;
53 }
54 
55 void ServerCommunications::parseMessage(yarp::os::Bottle& input, yarp::os::Bottle& reply)
56 {
57  int btlSize = input.size();
58  for (int i=0; i<btlSize; ++i)
59  {
60  switch (input.get(i).asInt()) {
62  {
63  std::cout << "Got message: GET_CONTROLLER_STATUS." << std::endl;
64  // reply.addInt(controllerStatus);
65  }break;
66 
68  {
69  std::cout << "Got message: GET_WBI_CONFIG_FILE_PATH." << std::endl;
70  // reply.addString(ctrlOptions.wbiConfigFilePath);
71  }break;
72 
73  case GET_ROBOT_NAME:
74  {
75  std::cout << "Got message: GET_ROBOT_NAME." << std::endl;
76  // reply.addString(ctrlOptions.robotName);
77  }break;
78 
80  {
81  OCRA_INFO("Got message: GET_IS_FLOATING_BASE.");
82  reply.addInt(!model->hasFixedRoot());
83  }break;
84 
85  case START_CONTROLLER:
86  {
87  std::cout << "Got message: START_CONTROLLER." << std::endl;
88  // this->start();
89  // TODO: make a switch case for if the controller is suspended then resume but if it is stopped then start.
90  }break;
91 
92  case STOP_CONTROLLER:
93  {
94  std::cout << "Got message: STOP_CONTROLLER." << std::endl;
95  // this->stop();
96  }break;
97 
98  case PAUSE_CONTROLLER:
99  {
100  std::cout << "Got message: PAUSE_CONTROLLER." << std::endl;
101  // this->suspend();
102  // TODO: Make a custom function that puts the robot in pos mode before suspend.
103  }break;
104 
106  {
107  OCRA_INFO("Got message: CHANGE_FIXED_LINK_RIGHT.");
108  this->controller->setFixedLinkForOdometry("r_sole");
109  int isInLeftSupport = input.get(++i).asInt();
110  OCRA_INFO("Got message: " << isInLeftSupport);
111  int isInRightSupport = input.get(++i).asInt();
112  OCRA_INFO("Got message: " << isInRightSupport);
113  this->controller->setContactState(isInLeftSupport, isInRightSupport);
114  reply.addInt(SUCCESS);
115  } break;
116 
118  {
119  OCRA_INFO("Got message: CHANGE_FIXED_LINK_LEFT.");
120  this->controller->setFixedLinkForOdometry("l_sole");
121  int isInLeftSupport = input.get(++i).asInt();
122  OCRA_INFO("Got message: " << isInLeftSupport);
123  int isInRightSupport = input.get(++i).asInt();
124  OCRA_INFO("Got message: " << isInRightSupport);
125  this->controller->setContactState(isInLeftSupport, isInRightSupport);
126  reply.addInt(SUCCESS);
127  } break;
128 
129 
130  case ADD_TASKS:
131  {
132  int numberOfTasks = input.get(++i).asInt();
133  ++i;
134 
135  std::vector<ocra::TaskBuilderOptions> taskOptionsVector;
136  for (int j=0; j<numberOfTasks; ++j)
137  {
138  int sizeOfOptions = 0;
139  yarp::os::Bottle trimmedBottle = ocra::util::trimBottle(input, i);
140  ocra::TaskBuilderOptions taskOptions;
141  if (taskOptions.extractFromBottle(trimmedBottle, sizeOfOptions)) {
142  taskOptionsVector.push_back(taskOptions);
143  }
144  i += sizeOfOptions;
145  }
146  ocra::TaskConstructionManager factory(model, controller, taskOptionsVector);
147  reply.addInt(SUCCESS);
148  }break;
149 
150  case ADD_TASKS_FROM_FILE:
151  {
152  std::cout << "Got message: ADD_TASK_FROM_FILE." << std::endl;
153  }break;
154 
155  case REMOVE_TASK:
156  {
157  ++i;
158  OCRA_INFO("Got message: REMOVE_TASK.");
159  std::string taskToRemove = input.get(i).asString();
160  controller->removeTask(taskToRemove);
161  // if (taskRemoved) {
163  yarp::os::Bottle outputMessage;
165  outputMessage.addString(taskToRemove);
166  outputPort.write(outputMessage);
167  // }else{
168  // reply.addInt(SERVER_COMMUNICATIONS_MESSAGE::FAILURE);
169  // }
170  ++i;
171  }break;
172 
173  case REMOVE_TASKS:
174  {
175  std::cout << "Got message: REMOVE_TASKS." << std::endl;
176  }break;
177 
178  case GET_TASK_LIST:
179  {
180  OCRA_INFO("Got message: GET_TASK_LIST.");
181  for(auto taskName : controller->getTaskNames()) {
182  reply.addString(taskName);
183  }
184  }break;
185 
186  case GET_TASK_PORT_LIST:
187  {
188  OCRA_INFO("Got message: GET_TASK_PORT_LIST.");
189  for(auto taskPort : controller->getTaskPortNames()) {
190  reply.addString(taskPort);
191  }
192  }break;
193 
194  case GET_TASK_PORT_NAME:
195  {
196  std::string taskName = input.get(++i).asString();
197  OCRA_INFO("Got message: GET_TASK_PORT_NAME.");
198  reply.addString(controller->getTaskPortName(taskName));
199  }break;
200 
201  case HELP:
202  {
203  std::cout << "Got message: HELP." << std::endl;
204  }break;
205 
206  default:
207  {
208  std::cout << "Got message: UNKNOWN." << std::endl;
209  }break;
210 
211  }
212  }
213 }
yarp::os::Bottle trimBottle(const yarp::os::Bottle &bottle, int startIndex, int endIndex=-1)
#define OCRA_ERROR(msg)
Definition: ErrorsHelper.h:32
#define OCRA_INFO(msg)
Definition: ErrorsHelper.h:38
bool extractFromBottle(yarp::os::Bottle &bottle, int &sizeOfOptions)
virtual bool read(yarp::os::ConnectionReader &connection)
void parseMessage(yarp::os::Bottle &input, yarp::os::Bottle &reply)