Skip to content

Commit 0365b3d

Browse files
Minor signature change
1 parent ca8d5b5 commit 0365b3d

File tree

7 files changed

+56
-77
lines changed

7 files changed

+56
-77
lines changed

common/events.cpp

+19-24
Original file line numberDiff line numberDiff line change
@@ -419,24 +419,17 @@ events_deinit_subscriber(event_handle_t handle)
419419
}
420420

421421

422-
423-
int
424-
event_receive(event_handle_t handle, string &key,
425-
event_params_t &params, int &missed_cnt)
426-
{
427-
if ((handle == s_subscriber) && (s_subscriber != NULL)) {
428-
return s_subscriber->event_receive(key, params, missed_cnt);
429-
}
430-
return -1;
431-
}
432-
433-
434422
event_receive_op_t
435-
event_receive_as_struct(event_handle_t handle)
423+
event_receive(event_handle_t handle)
436424
{
437425
event_receive_op_t op;
438426

439-
op.rc = event_receive(handle, op.key, op.params, op.missed_cnt);
427+
if ((handle == s_subscriber) && (s_subscriber != NULL)) {
428+
op.rc = s_subscriber->event_receive(op.key, op.params, op.missed_cnt);
429+
}
430+
else {
431+
op.rc = -1;
432+
}
440433
return op;
441434
}
442435

@@ -538,25 +531,24 @@ int
538531
event_receive_wrap(void *handle, char *event_str,
539532
int event_str_sz, char *missed_cnt_str, int missed_cnt_str_sz)
540533
{
541-
string key;
542-
event_params_t params;
543-
int missed_cnt = 0;
534+
event_receive_op_t evt;
535+
int rc = 0;
544536

545537
SWSS_LOG_DEBUG("events_receive_wrap h=%p event-sz=%d missed-sz=%d\n",
546538
handle, event_str_sz, missed_cnt_str_sz);
547539

548-
int rc = event_receive(handle, key, params, missed_cnt);
540+
evt = event_receive(handle);
549541

550-
if (rc == 0) {
542+
if (evt.rc == 0) {
551543
nlohmann::json res = nlohmann::json::object();
552544

553545
{
554546
nlohmann::json params_data = nlohmann::json::object();
555547

556-
for (event_params_t::const_iterator itc = params.begin(); itc != params.end(); ++itc) {
548+
for (event_params_t::const_iterator itc = evt.params.begin(); itc != evt.params.end(); ++itc) {
557549
params_data[itc->first] = itc->second;
558550
}
559-
res[key] = params_data;
551+
res[evt.key] = params_data;
560552
}
561553
string json_str(res.dump());
562554
rc = snprintf(event_str, event_str_sz, "%s", json_str.c_str());
@@ -566,17 +558,20 @@ event_receive_wrap(void *handle, char *event_str,
566558
event_str[event_str_sz-1] = 0;
567559
}
568560

569-
int rc_missed = snprintf(missed_cnt_str, missed_cnt_str_sz, "%d", missed_cnt);
561+
int rc_missed = snprintf(missed_cnt_str, missed_cnt_str_sz, "%d", evt.missed_cnt);
570562
if (rc_missed >= missed_cnt_str_sz) {
571563
SWSS_LOG_ERROR("missed cnt (%d) buffer.need=%d given=%d",
572-
missed_cnt, rc_missed, missed_cnt_str_sz);
564+
evt.missed_cnt, rc_missed, missed_cnt_str_sz);
573565
missed_cnt_str[missed_cnt_str_sz-1] = 0;
574566
}
575567
}
576-
else if (rc > 0) {
568+
else if (evt.rc > 0) {
577569
// timoeut
578570
rc = 0;
579571
}
572+
else {
573+
rc = evt.rc;
574+
}
580575

581576
SWSS_LOG_DEBUG("events_receive_wrap rc=%d event_str=%s missed=%s\n",
582577
rc, event_str, missed_cnt_str);

common/events.h

+8-30
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ event_handle_t events_init_subscriber(bool use_cache=false,
151151
void events_deinit_subscriber(event_handle_t handle);
152152

153153

154+
typedef struct {
155+
int rc; /* Return value of event_receive */
156+
std::string key; /* key */
157+
event_params_t params; /* Params received */
158+
int missed_cnt; /* missed count */
159+
} event_receive_op_t;
160+
154161
/*
155162
* Receive an event.
156163
* A blocking call unless the subscriber is created with a timeout.
@@ -193,36 +200,7 @@ void events_deinit_subscriber(event_handle_t handle);
193200
* < 0 - For all other failures
194201
*
195202
*/
196-
int event_receive(event_handle_t handle, std::string &key,
197-
event_params_t &params, int &missed_cnt);
198-
199-
/*
200-
* event_receive_as_struct
201-
*
202-
* Returns o/p as structured.
203-
* This is handy for invocation via python.
204-
*
205-
* input:
206-
* handle - As obtained from events_init_subscriber
207-
*
208-
* output:
209-
* None
210-
*
211-
* Return:
212-
* struct that gets return value and all o/p params of event_receive
213-
*/
214-
typedef struct {
215-
int rc; /* Return value of event_receive */
216-
/* o/p params from event receive */
217-
std::string key;
218-
event_params_t params;
219-
int missed_cnt;
220-
} event_receive_op_t;
221-
222-
event_receive_op_t event_receive_as_struct(event_handle_t handle);
203+
event_receive_op_t event_receive(event_handle_t handle);
223204

224-
/* Non ZMQ Error codes */
225-
#define ERR_MESSAGE_INVALID -2
226-
#define ERR_OTHER -1
227205

228206
#endif /* !_EVENTS_H */

common/events_common.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
#include <boost/archive/text_oarchive.hpp>
1919

2020
#include "logger.h"
21-
#include "events.h"
2221

2322
using namespace std;
2423
using namespace chrono;
2524

25+
#define ERR_MESSAGE_INVALID -2
26+
#define ERR_OTHER -1
27+
2628
/*
2729
* Max count of possible concurrent event publishers
2830
* We maintain a cache of last seen sequence number per publisher.

common/events_wrap.h

+16-12
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,27 @@ void events_deinit_subscriber_wrap(void *handle);
106106
* Receive an event.
107107
*
108108
* input:
109-
* args:
109+
* handle - Handle obtained from init subscriber
110+
* event_str:
110111
* Buffer for receiving event formatted as below.
111-
* '{ "key" : "<event's key as string>",
112-
* "params": {
113-
* <map of string:string params>
114-
* },
115-
* "missed_cnt": <missed count as int>
116-
* }'
112+
* <publish_source + tag as key>: {
113+
* <params as dict>
114+
* }
117115
* e.g: '{
118-
* "key": "sonic-events-bgp:bgp-state":
119-
* "params": {
116+
* "sonic-events-bgp:bgp-state": {
120117
* "timestamp": "2022-08-17T02:39:21.286611Z",
121118
* "ip": "100.126.188.90",
122119
* "status": "down"
123-
* },
124-
* "missed_cnt": 0
120+
* }
125121
* }'
126-
* sz:
122+
* event_str_sz:
127123
* Size of the buffer for receiving event.
128124
*
125+
* missed_cnt:
126+
* Buffer to receive missed count as int converted to string.
127+
*
128+
* missed_cnt_sz:
129+
* Size of the missed_cnt buffer.
129130
*
130131
* Return:
131132
* > 0 -- Implies received an event
@@ -135,6 +136,9 @@ void events_deinit_subscriber_wrap(void *handle);
135136
int event_receive_wrap(void *handle, char *event_str,
136137
int event_str_sz, char *missed_cnt, int missed_cnt_sz);
137138

139+
/*
140+
* Set SWSS log priority
141+
*/
138142
void swssSetLogPriority(int pri);
139143

140144
#ifdef __cplusplus

common/logger.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ Logger& Logger::getInstance()
186186
void Logger::setMinPrio(Priority prio)
187187
{
188188
getInstance().m_minPrio = prio;
189+
SWSS_LOG_ERROR("loglevel Set=%d", prio);
190+
189191
}
190192

191193
Logger::Priority Logger::getMinPrio()

pyext/swsscommon.i

-1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,5 @@ T castSelectableObj(swss::Selectable *temp)
218218
%include "dbinterface.h"
219219
%include "logger.h"
220220
%include "events.h"
221-
%ignore event_receive(event_handle_t, std::string &, event_params_t &, int &);
222221

223222
%include "status_code_util.h"

tests/events_ut.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -540,24 +540,23 @@ TEST(events, subscribe)
540540
pub_events(index_subs, cnt_subs);
541541

542542
for(i=0; true; ++i) {
543-
string key, exp_key;
544-
event_params_t params;
545-
int missed = -1;
543+
string exp_key;
544+
event_receive_op_t evt;
546545

547-
int rc = event_receive(hsub, key, params, missed);
546+
evt = event_receive(hsub);
548547

549-
if (rc != 0) {
550-
EXPECT_EQ(EAGAIN, rc);
548+
if (evt.rc != 0) {
549+
EXPECT_EQ(EAGAIN, evt.rc);
551550
break;
552551
}
553552

554-
EXPECT_EQ(ldata[i].params, params);
553+
EXPECT_EQ(ldata[i].params, evt.params);
555554

556555
exp_key = ldata[i].source + ":" + ldata[i].tag;
557556

558-
EXPECT_EQ(exp_key, key);
557+
EXPECT_EQ(exp_key, evt.key);
559558

560-
EXPECT_EQ(ldata[i].missed_cnt, missed);
559+
EXPECT_EQ(ldata[i].missed_cnt, evt.missed_cnt);
561560
}
562561

563562
EXPECT_EQ(i, (int)ARRAY_SIZE(ldata));

0 commit comments

Comments
 (0)