一、Client发送
bool execute(const std::string& method, const XmlRpc::XmlRpcValue& request, XmlRpc::XmlRpcValue& response, XmlRpc::XmlRpcValue& payload, bool wait_for_master)
wait_for_master:是否一定要等到roscore有应答。true:一直等,false:不等。
node-app会创建一个NodeHandle对象,构造该对象时,会向roscore发registerPublisher。
@method:registerPublisher @params:要发送到roscore的请求 @result:从roscore收到的应答 bool XmlRpcClient::execute(const char* method, XmlRpcValue const& params, XmlRpcValue& result) { // setupConnection任务: // 1)创建socket(fd) // 2)设置fd为非阻塞模式 // 3)connect(fd, _host, _port)。_host: localhost,_port: 11311 // 4)添加XmlRpcClient作为一个事件源 // 执行setupConnection()后,_connectionState = WRITE_REQUEST; if (!setupConnection()) { return false; } // 根据method、params参数,生成请求字符串,放在_request。 if ( ! generateRequest(method, params)) { return false; } result.clear(); double msTime = -1.0; // Process until exit is called // _disp.work会执行1)发送请求。2)以select机制接收应答。收到的应答放在_response。 // 如果收到应答(XmlRpcClient::readResponse()),执行_disp.work后,_connectionState = IDLE _disp.work(msTime); // parseResponse作用是将xml格式的_response转换为XmlRpcValue格式的result。 if (_connectionState != IDLE || ! parseResponse(result)) { return false; } _response = ""; return true; }
以下是要发送到roscore的请求(XmlRpc::_request)
POST / HTTP/1.1 User-Agent: XMLRPC++ 0.7 Host: localhost:11311 Content-Type: text/xml Content-length: 297 <?xml version="1.0"?> <methodCall> <methodName>registerPublisher</methodName> <params> <param> <value>/rplidar_node</value> </param> <param> <value>/rosout</value> </param> <param> <value>rosgraph_msgs/Log</value> </param> <param> <value>http://DESKTOP-DF7GFDR:50191/</value> </param> </params> </methodCall>
ros::init时,如果没有指定环境变量,就取默认的getDefaultMasterURI()。
namespace ros { const std::string& getDefaultMasterURI() { static const std::string uri = "http://localhost:11311"; return uri; } }