Skip to content

Commit c076060

Browse files
committed
Adding all changes from https://github.com/code-iai/ROSIntegration master
1 parent 9c9e2f4 commit c076060

25 files changed

+2164
-1795
lines changed

include/helper.h

Lines changed: 155 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,130 +3,161 @@
33
#include "rapidjson/document.h"
44
#include "rapidjson/writer.h"
55
#include "rapidjson/stringbuffer.h"
6-
#include "bson.h"
6+
#include <bson.h>
77

88
using json = rapidjson::Document;
9-
namespace rosbridge2cpp{
10-
class Helper {
11-
public:
12-
Helper() = default;
13-
~Helper () = default;
14-
15-
std::string static get_string_from_rapidjson(json &d){
16-
rapidjson::StringBuffer buffer;
17-
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
18-
d.Accept(writer);
19-
return buffer.GetString();
20-
}
21-
22-
std::string static get_string_from_rapidjson(const json &d){
23-
rapidjson::StringBuffer buffer;
24-
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
25-
d.Accept(writer);
26-
return buffer.GetString();
27-
}
28-
29-
// dot_notation refers to MongoDB dot notation¬
30-
// returns "" and sets success to true if suitable data can't be found via the dot notation¬
31-
std::string static get_utf8_by_key(const char *dot_notation, bson_t &b, bool &success){
32-
bson_iter_t iter;
33-
bson_iter_t val;
34-
35-
if (bson_iter_init (&iter, &b) &&
36-
bson_iter_find_descendant (&iter, dot_notation, &val) &&
37-
BSON_ITER_HOLDS_UTF8 (&val)) {
38-
success = true;
39-
return std::string(bson_iter_utf8 (&val,NULL));
40-
}
41-
42-
success = false;
43-
return "";
44-
}
45-
46-
// dot_notation refers to MongoDB dot notation¬
47-
// returns INT32_MAX and sets success to 'false' if suitable data can't be found via the dot notation¬
48-
int32_t static get_int32_by_key(const char *dot_notation, bson_t &b, bool &success){
49-
bson_iter_t iter;
50-
bson_iter_t val;
51-
52-
if (bson_iter_init (&iter, &b) &&
53-
bson_iter_find_descendant (&iter, dot_notation, &val) &&
54-
BSON_ITER_HOLDS_INT32 (&val)) {
55-
success = true;
56-
return bson_iter_int32 (&val);
57-
}
58-
success = false;
59-
return INT32_MAX;
60-
}
61-
62-
// dot_notation refers to MongoDB dot notation¬
63-
// returns -1 and sets success to 'false' if suitable data can't be found via the dot notation¬
64-
double static get_double_by_key(const char *dot_notation, bson_t &b, bool &success){
65-
bson_iter_t iter;
66-
bson_iter_t val;
67-
68-
// if (bson_iter_init (&iter, &b) &&
69-
// bson_iter_find_descendant (&iter, dot_notation, &val) &&
70-
// BSON_ITER_HOLDS_DOUBLE (&val)) {
71-
if (bson_iter_init (&iter, &b) &&
72-
bson_iter_find_descendant (&iter, dot_notation, &val)){
73-
if(!BSON_ITER_HOLDS_DOUBLE (&val)){
74-
std::cout << "Key found, but not double typed" << std::endl;
75-
}else{
76-
// std::cout << "Key " << dot_notation <<" found. success is = " << success << std::endl;
77-
std::cout << "Key " << dot_notation <<" found."<< std::endl;
78-
success = true;
79-
// std::cout << " success is now = " << success << std::endl;
80-
return bson_iter_double (&val);
81-
}
82-
}
83-
std::cout << "Couldn't find descendant" << std::endl;
84-
success = false;
85-
return -1.0;
86-
}
87-
88-
// dot_notation refers to MongoDB dot notation¬
89-
// returns false and sets success to 'false' if suitable data can't be found via the dot notation¬
90-
bool static get_bool_by_key(const char *dot_notation, bson_t &b, bool &success){
91-
bson_iter_t iter;
92-
bson_iter_t val;
93-
94-
if (bson_iter_init (&iter, &b) &&
95-
bson_iter_find_descendant (&iter, dot_notation, &val) &&
96-
BSON_ITER_HOLDS_BOOL (&val)) {
97-
success = true;
98-
return bson_iter_bool (&val);
99-
}
100-
success = false;
101-
return false;
102-
}
103-
104-
// dot_notation refers to MongoDB dot notation¬
105-
// returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation
106-
//
107-
// binary_data_length holds the size of the buffer where the returned pointer points to.
108-
static const uint8_t * get_binary_by_key(const char *dot_notation, bson_t &b, uint32_t &binary_data_length, bool &success){
109-
bson_iter_t iter;
110-
bson_iter_t val;
111-
112-
if (bson_iter_init (&iter, &b) &&
113-
bson_iter_find_descendant (&iter, dot_notation, &val) &&
114-
BSON_ITER_HOLDS_BINARY (&val)) {
115-
bson_subtype_t subtype;
116-
const uint8_t *binary;
117-
118-
bson_iter_binary(&val, &subtype, &binary_data_length, &binary);
119-
assert( binary );
120-
success = true;
121-
return binary;
122-
}
123-
success = false;
124-
return nullptr;
125-
}
126-
127-
bool static bson_has_key(bson_t &b, const char *key){
128-
return bson_has_field(&b,key);
129-
}
130-
131-
};
9+
namespace rosbridge2cpp {
10+
class Helper {
11+
public:
12+
Helper() = default;
13+
~Helper() = default;
14+
15+
std::string static get_string_from_rapidjson(json &d)
16+
{
17+
rapidjson::StringBuffer buffer;
18+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
19+
d.Accept(writer);
20+
return buffer.GetString();
21+
}
22+
23+
std::string static get_string_from_rapidjson(const json &d)
24+
{
25+
rapidjson::StringBuffer buffer;
26+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
27+
d.Accept(writer);
28+
return buffer.GetString();
29+
}
30+
31+
// dot_notation refers to MongoDB dot notation
32+
// returns "" and sets success to true if suitable data can't be found via the dot notation
33+
std::string static get_utf8_by_key(const char *dot_notation, bson_t &b, bool &success)
34+
{
35+
bson_iter_t iter;
36+
bson_iter_t val;
37+
38+
if (bson_iter_init(&iter, &b) &&
39+
bson_iter_find_descendant(&iter, dot_notation, &val) &&
40+
BSON_ITER_HOLDS_UTF8(&val)) {
41+
success = true;
42+
return std::string(bson_iter_utf8(&val, NULL));
43+
}
44+
45+
success = false;
46+
return "";
47+
}
48+
49+
// dot_notation refers to MongoDB dot notation
50+
// returns INT32_MAX and sets success to 'false' if suitable data can't be found via the dot notation
51+
int32_t static get_int32_by_key(const char *dot_notation, bson_t &b, bool &success)
52+
{
53+
bson_iter_t iter;
54+
bson_iter_t val;
55+
56+
if (bson_iter_init(&iter, &b) &&
57+
bson_iter_find_descendant(&iter, dot_notation, &val) &&
58+
BSON_ITER_HOLDS_INT32(&val)) {
59+
success = true;
60+
return bson_iter_int32(&val);
61+
}
62+
success = false;
63+
return INT32_MAX;
64+
}
65+
66+
// dot_notation refers to MongoDB dot notation
67+
// returns -1 and sets success to 'false' if suitable data can't be found via the dot notation
68+
double static get_double_by_key(const char *dot_notation, bson_t &b, bool &success)
69+
{
70+
bson_iter_t iter;
71+
bson_iter_t val;
72+
73+
// if (bson_iter_init (&iter, &b) &&
74+
// bson_iter_find_descendant (&iter, dot_notation, &val) &&
75+
// BSON_ITER_HOLDS_DOUBLE (&val)) {
76+
if (bson_iter_init(&iter, &b) &&
77+
bson_iter_find_descendant(&iter, dot_notation, &val)) {
78+
if (!BSON_ITER_HOLDS_DOUBLE(&val)) {
79+
std::cout << "Key found, but not double typed" << std::endl;
80+
}
81+
else {
82+
//std::cout << "Key " << dot_notation << " found. success is = " << success << std::endl;
83+
//std::cout << "Key " << dot_notation << " found." << std::endl;
84+
success = true;
85+
//std::cout << " success is now = " << success << std::endl;
86+
return bson_iter_double(&val);
87+
}
88+
}
89+
std::cout << "Couldn't find descendant" << std::endl;
90+
success = false;
91+
return -1.0;
92+
}
93+
94+
// dot_notation refers to MongoDB dot notation
95+
// returns false and sets success to 'false' if suitable data can't be found via the dot notation
96+
bool static get_bool_by_key(const char *dot_notation, bson_t &b, bool &success)
97+
{
98+
bson_iter_t iter;
99+
bson_iter_t val;
100+
101+
if (bson_iter_init(&iter, &b) &&
102+
bson_iter_find_descendant(&iter, dot_notation, &val) &&
103+
BSON_ITER_HOLDS_BOOL(&val)) {
104+
success = true;
105+
return bson_iter_bool(&val);
106+
}
107+
success = false;
108+
return false;
109+
}
110+
111+
// dot_notation refers to MongoDB dot notation
112+
// returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation
113+
//
114+
// binary_data_length holds the size of the buffer where the returned pointer points to.
115+
static const uint8_t * get_binary_by_key(const char *dot_notation, bson_t &b, uint32_t &binary_data_length, bool &success)
116+
{
117+
bson_iter_t iter;
118+
bson_iter_t val;
119+
120+
if (bson_iter_init(&iter, &b) &&
121+
bson_iter_find_descendant(&iter, dot_notation, &val) &&
122+
BSON_ITER_HOLDS_BINARY(&val)) {
123+
bson_subtype_t subtype;
124+
const uint8_t *binary;
125+
126+
bson_iter_binary(&val, &subtype, &binary_data_length, &binary);
127+
assert(binary);
128+
success = true;
129+
return binary;
130+
}
131+
success = false;
132+
return nullptr;
133+
}
134+
135+
// dot_notation refers to MongoDB dot notation
136+
// returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation
137+
//
138+
// array_size holds the size in byte of the buffer where the returned pointer points to.
139+
static const uint8_t * get_array_by_key(const char *dot_notation, bson_t &b, uint32_t &array_size, bool &success)
140+
{
141+
bson_iter_t iter;
142+
bson_iter_t val;
143+
144+
if (bson_iter_init(&iter, &b) &&
145+
bson_iter_find_descendant(&iter, dot_notation, &val) &&
146+
BSON_ITER_HOLDS_ARRAY(&val)) {
147+
const uint8_t *binary;
148+
149+
bson_iter_array(&val, &array_size, &binary);
150+
assert(binary);
151+
success = true;
152+
return binary;
153+
}
154+
success = false;
155+
return nullptr;
156+
}
157+
158+
bool static bson_has_key(bson_t &b, const char *key)
159+
{
160+
return bson_has_field(&b, key);
161+
}
162+
};
132163
}

include/itransport_layer.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,38 @@
1010
* Please see client/socket_tcp_connection.h for an example implementation that uses UNIX Sockets
1111
* to connect to a ROSBridge server
1212
*/
13-
namespace rosbridge2cpp{
14-
class ITransportLayer {
15-
public:
16-
enum TransportMode {JSON,BSON};
13+
namespace rosbridge2cpp {
14+
class ITransportLayer {
15+
public:
16+
enum TransportMode { JSON, BSON };
1717

18-
// Initialize the TransportLayer by connecting to the given IP and port
19-
// The implementing class should have an active connection to IP:port
20-
// when the method has been executed completly.
21-
// Returns true if the connection has been successfully.
22-
virtual bool Init(std::string ip_addr, int port) = 0;
18+
// Initialize the TransportLayer by connecting to the given IP and port
19+
// The implementing class should have an active connection to IP:port
20+
// when the method has been executed completly.
21+
// Returns true if the connection has been successfully.
22+
virtual bool Init(std::string ip_addr, int port) = 0;
2323

24-
// Send a string over the underlying transport mechanism to the rosbridge server
25-
virtual bool SendMessage(std::string data) = 0;
24+
// Send a string over the underlying transport mechanism to the rosbridge server
25+
virtual bool SendMessage(std::string data) = 0;
2626

27-
// Send a string over the underlying transport mechanism to the rosbridge server
28-
virtual bool SendMessage(const uint8_t *data, unsigned int length) = 0;
27+
// Send a string over the underlying transport mechanism to the rosbridge server
28+
virtual bool SendMessage(const uint8_t *data, unsigned int length) = 0;
2929

30-
// Register a std::function that will be called whenever a new data packet has been received by this TransportLayer.
31-
virtual void RegisterIncomingMessageCallback(std::function<void(json&)>) = 0;
30+
// Register a std::function that will be called whenever a new data packet has been received by this TransportLayer.
31+
virtual void RegisterIncomingMessageCallback(std::function<void(json&)>) = 0;
3232

33-
// Register a std::function that will be called whenever a new data packet has been received by this TransportLayer.
34-
virtual void RegisterIncomingMessageCallback(std::function<void(bson_t&)>) = 0;
33+
// Register a std::function that will be called whenever a new data packet has been received by this TransportLayer.
34+
virtual void RegisterIncomingMessageCallback(std::function<void(bson_t&)>) = 0;
3535

36-
// Register a std::function that will be called when errors occur.
37-
virtual void RegisterErrorCallback(std::function<void(TransportError)>) = 0;
36+
// Register a std::function that will be called when errors occur.
37+
virtual void RegisterErrorCallback(std::function<void(TransportError)>) = 0;
3838

39-
// Report an error to the registered ErrorCallback (see RegisterErrorCallback)
40-
virtual void ReportError(TransportError) = 0;
39+
// Report an error to the registered ErrorCallback (see RegisterErrorCallback)
40+
virtual void ReportError(TransportError) = 0;
4141

42-
// Report an error to the registered ErrorCallback (see RegisterErrorCallback)
43-
virtual void SetTransportMode(TransportMode) = 0;
44-
private:
45-
/* data */
46-
};
42+
// Report an error to the registered ErrorCallback (see RegisterErrorCallback)
43+
virtual void SetTransportMode(TransportMode) = 0;
44+
private:
45+
/* data */
46+
};
4747
}

0 commit comments

Comments
 (0)