YARP
Yet Another Robot Platform
RosHeader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "RosHeader.h"
10 #include "TcpRosLogComponent.h"
11 
12 #include <cstdio>
13 #include <cstring>
14 #include <yarp/os/Bytes.h>
15 #include <yarp/os/NetType.h>
16 
17 using namespace yarp::os;
18 using namespace std;
19 
20 void RosHeader::appendInt32(char *&buf,int x) {
21  Bytes bytes(buf,4);
22  NetType::netInt(x,bytes);
23  buf += 4;
24 }
25 
26 void RosHeader::appendString(char *&buf,const string& str) {
27  memcpy(buf,str.c_str(),str.length());
28  buf += str.length();
29 }
30 
31 
32 string RosHeader::showMessage(string s) {
33  string result;
34  for (char ch : s) {
35  char buf[256];
36  sprintf(buf, "%c (%#x) ", (ch>=' ')?ch:'.', *reinterpret_cast<unsigned char*>(&ch));
37  result += buf;
38  }
39  return result;
40 }
41 
43  size_t len = 0;
44  for (auto& it : data) {
45  string key = it.first;
46  string val = it.second;
47  len += 4 + key.length() + 1 + val.length();
48  }
49  string result(len,'\0');
50  char *buf = (char *)result.c_str();
51  for (auto& it : data) {
52  string key = it.first;
53  string val = it.second;
54  appendInt32(buf,key.length()+1+val.length());
55  appendString(buf,key);
56  appendString(buf,string("="));
57  appendString(buf,val);
58  }
59  return result;
60 }
61 
62 
63 bool RosHeader::readHeader(const string& bin) {
64  data.clear();
65 
66  unsigned int len = bin.length();
67  char *at = (char*) bin.c_str();
68 
69  while (len>0) {
70  Bytes bytes(at,4);
71  int slen = NetType::netInt(bytes);
72  at += 4;
73  len -= 4;
74  string keyval(at,slen);
75  size_t delim = keyval.find_first_of('=',0);
76  if (delim == string::npos) {
77  yCWarning(TCPROSCARRIER, "Corrupt ROS header");
78  }
79  string key = keyval.substr(0,delim);
80  string val = keyval.substr(delim+1);
81  yCTrace(TCPROSCARRIER, "key %s => val %s\n", key.c_str(), val.c_str());
82  data[key] = val;
83  at += slen;
84  len -= slen;
85  }
86  return true;
87 }
88 
89 
90 std::string RosHeader::toString() const {
91  string result;
92  for (const auto& it : data) {
93  string key = it.first;
94  string val = it.second;
95  result += key;
96  result += "->";
97  result += val;
98  result += " ";
99  }
100  return result;
101 }
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
RosHeader::appendInt32
static void appendInt32(char *&buf, int x)
Definition: RosHeader.cpp:20
RosHeader::readHeader
bool readHeader(const std::string &bin)
Definition: RosHeader.cpp:63
RosHeader::showMessage
static std::string showMessage(std::string s)
Definition: RosHeader.cpp:32
NetType.h
RosHeader::appendString
static void appendString(char *&buf, const std::string &str)
Definition: RosHeader.cpp:26
RosHeader::writeHeader
std::string writeHeader()
Definition: RosHeader.cpp:42
RosHeader.h
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
RosHeader::toString
std::string toString() const
Definition: RosHeader.cpp:90
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
TcpRosLogComponent.h
Bytes.h
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
yarp::os::NetType::netInt
static int netInt(const yarp::os::Bytes &code)
Definition: NetType.cpp:96
TCPROSCARRIER
const yarp::os::LogComponent & TCPROSCARRIER()
Definition: TcpRosLogComponent.cpp:16