Skip to content

Commit d130b84

Browse files
self review update
1 parent d68c204 commit d130b84

File tree

2 files changed

+64
-75
lines changed

2 files changed

+64
-75
lines changed

common/events_common.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#include "events_common.h"
2-
#include <boost/serialization/vector.hpp>
3-
#include <boost/serialization/map.hpp>
4-
#include <boost/archive/text_iarchive.hpp>
5-
#include <boost/archive/text_oarchive.hpp>
62

73
int zerrno = 0;
84

@@ -81,58 +77,3 @@ get_timestamp()
8177
return ss.str();
8278
}
8379

84-
85-
/*
86-
* Way to serialize map or vector
87-
* boost::archive::text_oarchive could be used to archive any struct/class
88-
* but that class needs some additional support, that declares
89-
* boost::serialization::access as private friend and couple more tweaks
90-
* std::map inherently supports serialization
91-
*/
92-
template <typename Map>
93-
const string
94-
serialize(const Map& data)
95-
{
96-
std::stringstream ss;
97-
boost::archive::text_oarchive oarch(ss);
98-
oarch << data;
99-
return ss.str();
100-
}
101-
102-
template <typename Map>
103-
void
104-
deserialize(const string& s, Map& data)
105-
{
106-
std::stringstream ss;
107-
ss << s;
108-
boost::archive::text_iarchive iarch(ss);
109-
iarch >> data;
110-
return;
111-
}
112-
113-
114-
template <typename Map>
115-
int
116-
map_to_zmsg(const Map& data, zmq_msg_t &msg)
117-
{
118-
string s = serialize(data);
119-
120-
int rc = zmq_msg_init_size(&msg, s.size());
121-
if (rc == 0) {
122-
strncpy((char *)zmq_msg_data(&msg), s.c_str(), s.size());
123-
}
124-
return rc;
125-
}
126-
127-
128-
template <typename Map>
129-
void
130-
zmsg_to_map(zmq_msg_t &msg, Map& data)
131-
{
132-
string s((const char *)zmq_msg_data(&msg), zmq_msg_size(&msg));
133-
deserialize(s, data);
134-
}
135-
136-
137-
138-

common/events_common.h

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "json.hpp"
1111
#include "zmq.h"
1212
#include <unordered_map>
13+
#include <boost/serialization/vector.hpp>
14+
#include <boost/serialization/map.hpp>
15+
#include <boost/archive/text_iarchive.hpp>
16+
#include <boost/archive/text_oarchive.hpp>
1317

1418
#include "logger.h"
1519
#include "events.h"
@@ -20,12 +24,18 @@ using namespace chrono;
2024
extern int errno;
2125
extern int zerrno;
2226

27+
/*
28+
* Max count of possible concurrent event publishers
29+
* A rough estimate only, more as a guideline than strict.
30+
* So this does not limit any usage
31+
*/
32+
#define MAX_PUBLISHERS_COUNT 1000
2333

2434
#define RET_ON_ERR(res, msg, ...)\
2535
if (!(res)) {\
2636
int e = errno; \
2737
zerrno = zmq_errno(); \
28-
string fmt = "errno:%d zmq_errno:%d " + msg; \
38+
string fmt = string("errno:%d zmq_errno:%d ") + msg; \
2939
SWSS_LOG_ERROR(fmt.c_str(), e, zerrno, ##__VA_ARGS__); \
3040
goto out; }
3141

@@ -64,9 +74,9 @@ T get_config_data(const string key, T def)
6474
return def;
6575
}
6676
else {
67-
stringstream ss(get_config(key));
68-
6977
T v;
78+
stringstream ss(s);
79+
7080
ss >> v;
7181

7282
return v;
@@ -78,15 +88,56 @@ string get_config(const string key);
7888

7989
const string get_timestamp();
8090

81-
const string serialize(const map_str_str_t & data);
91+
/*
92+
* Way to serialize map or vector
93+
* boost::archive::text_oarchive could be used to archive any struct/class
94+
* but that class needs some additional support, that declares
95+
* boost::serialization::access as private friend and couple more tweaks
96+
* std::map inherently supports serialization
97+
*/
98+
template <typename Map>
99+
const string
100+
serialize(const Map& data)
101+
{
102+
std::stringstream ss;
103+
boost::archive::text_oarchive oarch(ss);
104+
oarch << data;
105+
return ss.str();
106+
}
107+
108+
template <typename Map>
109+
void
110+
deserialize(const string& s, Map& data)
111+
{
112+
std::stringstream ss;
113+
ss << s;
114+
boost::archive::text_iarchive iarch(ss);
115+
iarch >> data;
116+
return;
117+
}
82118

83-
void deserialize(const string& s, map_str_str_t & data);
84119

85120
template <typename Map>
86-
int map_to_zmsg(const Map & data, zmq_msg_t &msg);
121+
int
122+
map_to_zmsg(const Map& data, zmq_msg_t &msg)
123+
{
124+
string s = serialize(data);
125+
126+
int rc = zmq_msg_init_size(&msg, s.size());
127+
if (rc == 0) {
128+
strncpy((char *)zmq_msg_data(&msg), s.c_str(), s.size());
129+
}
130+
return rc;
131+
}
132+
87133

88134
template <typename Map>
89-
void zmsg_to_map(zmq_msg_t &msg, Map &data);
135+
void
136+
zmsg_to_map(zmq_msg_t &msg, Map& data)
137+
{
138+
string s((const char *)zmq_msg_data(&msg), zmq_msg_size(&msg));
139+
deserialize(s, data);
140+
}
90141

91142

92143
/*
@@ -168,7 +219,7 @@ zmq_message_send(void *sock, p1 pt1, p2 pt2)
168219
{
169220
int rc = zmq_send_part(sock, pt2.empty() ? 0 : ZMQ_SNDMORE, pt1);
170221

171-
if (rc == 0) {
222+
if ((rc == 0) && (!pt2.empty())) {
172223
rc = zmq_send_part(sock, 0, pt2);
173224
}
174225
return rc;
@@ -183,17 +234,14 @@ zmq_message_read(void *sock, int flag, p1 pt1, p2 pt2)
183234

184235
rc = zmq_read_part(sock, flag, more, pt1);
185236
RET_ON_ERR(rc == 0, "Failed to read part1");
186-
RET_ON_ERR(more, "Expect 2 parts");
187237

188-
rc = zmq_read_part(sock, 0, more, pt2);
189-
RET_ON_ERR(rc == 0, "Failed to read part1");
190-
RET_ON_ERR(!more, "Don't expect more than 2 parts");
238+
if (more) {
239+
rc = zmq_read_part(sock, 0, more, pt2);
240+
RET_ON_ERR(rc == 0, "Failed to read part1");
241+
RET_ON_ERR(!more, "Don't expect more than 2 parts");
242+
}
191243

192244
out:
193245
return rc;
194246
}
195247

196-
197-
198-
199-

0 commit comments

Comments
 (0)