How does ROS implement XMLRPC?

Publisher:sigma28Latest update time:2023-09-14 Source: 古月居Author: Lemontree Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The XMLRPC code is in the _comm-noet-develulitiesxmlrpcpp path at the end.

Fortunately, the whole project is not too big. XMLRPC is divided into two parts: client and server.

Let's look at the client first. The main code is in the XmlRpcClient.cpp file.

The core function in the XmlRpcClient.cpp file is execu, which is used to execute remote calls. The code is as follows.

// Execute the named procedure on the remote server.
// Pas should be an array of the arguments f the method.
// Returns true if the request was sent and a result received (although the result might be a fault).
bool XmlRpcClient::execute(const char* method, XmlRpcValue const& params, XmlRpcValue& result)
{
  XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s (_connectionState %s).", method, connectionStateStr(_connectionState));


  // This is not a thre-safe operation, if you want to do multithreng, use separate
  // clients for eh thread. If you want to protect youelf from multiple threads
  // accessing the same client, replace this code with a real mutex.
  if (_executing)
    return false;


  _executing = true;
  ClearFlagOnExit cf(_executing);


  _sendAttempts = 0;
  _isFault = false;


  if ( ! setupConnection())
    return false;


  if ( ! generateRequest(method, params))
    return false;


  result.clear();
  double msTime = -1.0;   // Process until exit is cal
  _disp.work(msTime);


  if (_connectionState != IDLE || ! parseResponse(result)) {
    _header = "";
    return false;
  }


  // close() if server does not supports HTTP1.1
  // otherwise, reusing the socket to write leads to a SIGPE because
  // the remote server could shut down the corresponding socket.
  if (_header.find("HTTP/1.1 200 OK", 0, 15) != 0) {
    close();
  }


  XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s completed.", method);
  _header = "";
  _response = "";
  return true;
}

It first calls the setupConnection() function to establish a connection with the server.

After the connection is successful, call the generateRequest() function to generate a request message.

The header of the XMLRPC request message is handed over to the generateHeader() function. The code is as follows.

// Prepend http headers
std::string XmlRpcClient::generateHeader(size_t length) const
{
  std::string header = 
    "POST " + _uri + " HTTP/1.1rn"
    "User-Agent: ";
  header += XMLRPC_VERSION;
  header += "rnHost: ";
  header += _host;


  char buff[40];
  std::snprintf(buff,40,":%drn", _port);


  header += buff;
  header += "Content-Type: text/xmlrnContent-length: ";


  std::snprintf(buff,40,"%zurnrn", length);
  return header + buff;
}

The body part first converts the remote call method and parameters into XML format, and the generateRequest() function combines the header and body into a complete message, as follows:

std::string header = generateHeader(body.length());
_request = header + body;

After sending the message to the server, it starts waiting quietly.

Once the message returned by the server is received, the parseResponse function is called to parse the message data, that is, to convert the XML format into a pure data format.

We found that XMLRPC uses the socket function to implement the client and server.

We searched for the word socket and found that its original meaning is socket. This is very vivid, establishing a connection to achieve communication is like inserting a plug into a socket.

Although XMLRPC is also part of ROS, it is just a basic function after all. We will just use it and will not explore its implementation details for the time being.

Reference address:How does ROS implement XMLRPC?

Previous article:SenseTime and Spain's BOMAPA Group Sign AI Industrial Solutions Cooperation Agreement
Next article:What is XMLRPC in ROS

Latest robot Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号