00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "ros/header.h"
00036 #include "ros/message.h"
00037 #include "ros/transport/transport.h"
00038 #include <ros/console.h>
00039 #include <ros/assert.h>
00040
00041 #include <sstream>
00042
00043 #include <sys/socket.h>
00044 #include <cerrno>
00045
00046 using namespace std;
00047
00048 namespace ros
00049 {
00050
00051 Header::Header()
00052 : read_map_(new M_string())
00053 {
00054
00055 }
00056
00057 Header::~Header()
00058 {
00059
00060 }
00061
00062 bool Header::parse(const boost::shared_array<uint8_t>& buffer, uint32_t size, std::string& error_msg)
00063 {
00064 return parse(buffer.get(), size, error_msg);
00065 }
00066
00067 bool Header::parse(uint8_t* buffer, uint32_t size, std::string& error_msg)
00068 {
00069 uint8_t* buf = buffer;
00070 while (buf < buffer + size)
00071 {
00072 uint32_t len;
00073 SROS_DESERIALIZE_PRIMITIVE(buf, len);
00074
00075 if (len > 1000000)
00076 {
00077 error_msg = "Received an invalid TCPROS header. Each element must be prepended by a 4-byte length.";
00078 ROS_ERROR("%s", error_msg.c_str());
00079
00080 return false;
00081 }
00082
00083 std::string line((char*)buf, len);
00084
00085 buf += len;
00086
00087
00088 size_t eqpos = line.find_first_of("=", 0);
00089 if (eqpos == string::npos)
00090 {
00091 error_msg = "Received an invalid TCPROS header. Each line must have an equals sign.";
00092 ROS_ERROR("%s", error_msg.c_str());
00093
00094 return false;
00095 }
00096 string key = line.substr(0, eqpos);
00097 string value = line.substr(eqpos+1);
00098
00099 (*read_map_)[key] = value;
00100 }
00101
00102 return true;
00103 }
00104
00105 bool Header::getValue(const std::string& key, std::string& value) const
00106 {
00107 M_string::const_iterator it = read_map_->find(key);
00108 if (it == read_map_->end())
00109 {
00110 return false;
00111 }
00112
00113 value = it->second;
00114
00115 return true;
00116 }
00117
00118 void Header::write(const M_string& key_vals, boost::shared_array<uint8_t>& buffer, uint32_t& size)
00119 {
00120
00121 size = 0;
00122 {
00123 M_string::const_iterator it = key_vals.begin();
00124 M_string::const_iterator end = key_vals.end();
00125 for (; it != end; ++it)
00126 {
00127 const std::string& key = it->first;
00128 const std::string& value = it->second;
00129
00130 size += key.length();
00131 size += value.length();
00132 size += 1;
00133 size += 4;
00134 }
00135 }
00136
00137 if (size == 0)
00138 {
00139 return;
00140 }
00141
00142 buffer.reset(new uint8_t[size]);
00143 char* ptr = (char*)buffer.get();
00144
00145
00146 {
00147 M_string::const_iterator it = key_vals.begin();
00148 M_string::const_iterator end = key_vals.end();
00149 for (; it != end; ++it)
00150 {
00151 const std::string& key = it->first;
00152 const std::string& value = it->second;
00153
00154 uint32_t len = key.length() + value.length() + 1;
00155 SROS_SERIALIZE_PRIMITIVE(ptr, len);
00156 SROS_SERIALIZE_BUFFER(ptr, key.data(), key.length());
00157 static const char equals = '=';
00158 SROS_SERIALIZE_PRIMITIVE(ptr, equals);
00159 SROS_SERIALIZE_BUFFER(ptr, value.data(), value.length());
00160 }
00161 }
00162
00163 ROS_ASSERT(ptr == (char*)buffer.get() + size);
00164 }
00165
00166 }