-
Notifications
You must be signed in to change notification settings - Fork 0
/
solClientThread.cpp
357 lines (306 loc) · 11.7 KB
/
solClientThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//******************************************************************************
//
// Copyright (c) 2019, Brandon To
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the author nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//******************************************************************************
#include "solClientThread.hpp"
#include "log.hpp"
#include "monitoringThread.hpp"
namespace topicMonitor
{
SolClientThread* SolClientThread::instance_mps = nullptr;
static solClient_rxMsgCallback_returnCode_t
sessionMessageReceiveCallback(solClient_opaqueSession_pt session_p,
solClient_opaqueMsg_pt msg_p,
void* user_p)
{
LOG(DEBUG, "SolClient message received callback invoked");
// Create a work entry and enqueue it to MonitoringThread's work queue
//
WorkEntryMessageReceived* entry_p = new WorkEntryMessageReceived();
entry_p->setMsg(msg_p);
MonitoringThread::instance()->getWorkQueue()->push(entry_p);
// Taking ownership of the message away from the context thread. We are
// responsible for freeing the message when done processing.
//
return SOLCLIENT_CALLBACK_TAKE_MSG;
}
static void
sessionEventCallback(solClient_opaqueSession_pt session_p,
solClient_session_eventCallbackInfo_pt eventInfo_p,
void* user_p)
{
LOG(DEBUG, "SolClient event callback invoked");
}
static void
contextTimerCallback(solClient_opaqueContext_pt context_p, void* user_p)
{
LOG(DEBUG, "SolClient timer callback invoked");
// Create a work entry and enqueue it to MonitoringThread's work queue
//
WorkEntryTimerTick* entry_p = new WorkEntryTimerTick();
MonitoringThread::instance()->getWorkQueue()->push(entry_p);
}
// See Messaging API Concepts from the Solace Developer Guide:
//
// https://docs.solace.com/Solace-PubSub-Messaging-APIs/Developer-Guide/Core-Messaging-API-Concepts.htm
//
SolClientThread::SolClientThread(void) :
context_mp(nullptr),
session_mp(nullptr)
{
solClient_returnCode_t rc;
// solClient_initialize() is called here because SolClientThread is only
// constructed once.
//
rc = solClient_initialize(SOLCLIENT_LOG_DEFAULT_FILTER, nullptr);
if (rc != SOLCLIENT_OK)
LOG(FATAL, "solClient initialization failed");
LOG(INFO, "solClient initialized");
// Contexts
//
// The messaging APIs use processing Contexts for organizing communication
// between an application and a Solace PubSub+ message broker. Contexts act
// as containers in which Sessions are created and Session-related events
// can be handled.
//
// A Context encapsulates threads that drive network I/O and message
// delivery notification for the Sessions and Session components associated
// with that Context. For the Java API, one thread is used for I/O and
// another for notification. For the Java RTO, C, and .NET APIs, a single
// thread is used for both I/O and for notification. The life cycle of a
// Context‑owned thread is bound to the life cycle of the Context. The
// Javascript and Node.js APIs are single threaded and have a single global
// context that is not exposed.
//
solClient_context_createFuncInfo_t contextFuncInfo =
SOLCLIENT_CONTEXT_CREATEFUNC_INITIALIZER;
rc = solClient_context_create(
SOLCLIENT_CONTEXT_PROPS_DEFAULT_WITH_CREATE_THREAD,
&context_mp,
&contextFuncInfo,
sizeof(contextFuncInfo));
if (rc != SOLCLIENT_OK)
LOG(FATAL, "solClient context creation failed");
LOG(INFO, "solClient context created");
}
SolClientThread::~SolClientThread(void)
{
solClient_returnCode_t rc;
if (session_mp != nullptr)
{
rc = solClient_session_destroy(&session_mp);
if (rc != SOLCLIENT_OK)
LOG(FATAL, "solClient session destruction failed");
}
if (context_mp != nullptr)
{
rc = solClient_context_destroy(&context_mp);
if (rc != SOLCLIENT_OK)
LOG(FATAL, "solClient context destruction failed");
}
// solClient_cleanup() is called here because SolClientThread is only
// destroyed at program termination.
//
rc = solClient_cleanup();
if (rc != SOLCLIENT_OK)
LOG(FATAL, "solClient cleanup failed");
}
returnCode_t
SolClientThread::createSession(std::string host,
std::string vpn,
std::string username,
std::string password)
{
solClient_returnCode_t rc;
// Sessions
//
// When a Context is established, one or more Sessions can be created within
// that Context. A Session creates a single, client connection to a message
// broker for sending and receiving messages.
//
// A Session provides the following primary services:
//
// * client connection
// * update and retrieve Session properties
// * retrieve Session statistics
// * add and remove subscriptions
// * create destinations and endpoints
// * publish and receive Direct messages
// * publish Guaranteed messages
// * make requests/replies (or create Requestors for the Java API)
// * create Guaranteed message Flows to receive Guaranteed messages
// * create Browsers (for the Java and .NET APIs only)
// * create cache sessions
//
// When configuring a Session, the following must be provided:
//
// * Session properties to define the operating characteristics of the
// client connection to the message broker.
// * A message callback for Direct messages that are received.
// * An event handling callback for events that occur for the Session
// (optional for the Java API).
//
solClient_session_createFuncInfo_t sessionFuncInfo =
SOLCLIENT_SESSION_CREATEFUNC_INITIALIZER;
sessionFuncInfo.rxMsgInfo.callback_p = sessionMessageReceiveCallback;
sessionFuncInfo.rxMsgInfo.user_p = nullptr;
sessionFuncInfo.eventInfo.callback_p = sessionEventCallback;
sessionFuncInfo.eventInfo.user_p = nullptr;
int propIndex = 0;
const char* sessionProps[20] = {0};
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_HOST;
sessionProps[propIndex++] = host.c_str();
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_VPN_NAME;
sessionProps[propIndex++] = vpn.c_str();
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_USERNAME;
sessionProps[propIndex++] = username.c_str();
sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_PASSWORD;
sessionProps[propIndex++] = password.c_str();
rc = solClient_session_create(
(char **)sessionProps,
context_mp,
&session_mp,
&sessionFuncInfo,
sizeof(sessionFuncInfo));
if (rc != SOLCLIENT_OK)
{
LOG(ERROR, "solClient session creation failed");
return returnCode_t::FAILURE;
}
LOG(INFO, "solClient session created");
return returnCode_t::SUCCESS;
}
returnCode_t
SolClientThread::destroySession(void)
{
if (session_mp == nullptr) { return returnCode_t::NOTHING_TO_DO; }
if (solClient_session_destroy(&session_mp) != SOLCLIENT_OK)
{
LOG(ERROR, "solClient session destruction failed");
return returnCode_t::FAILURE;
}
LOG(INFO, "solClient session destroyed");
return returnCode_t::SUCCESS;
}
returnCode_t
SolClientThread::connectSession(void)
{
if (session_mp == nullptr) { return returnCode_t::FAILURE; }
std::lock_guard<std::mutex> lock(mutex_m);
if (solClient_session_connect(session_mp) != SOLCLIENT_OK)
{
LOG(ERROR, "solClient session connection failed");
return returnCode_t::FAILURE;
}
LOG(INFO, "solClient session connected");
return returnCode_t::SUCCESS;
}
returnCode_t
SolClientThread::disconnectSession(void)
{
if (session_mp == nullptr) { return returnCode_t::FAILURE; }
std::lock_guard<std::mutex> lock(mutex_m);
if (solClient_session_disconnect(session_mp) != SOLCLIENT_OK)
{
LOG(ERROR, "solClient session disconnection failed");
return returnCode_t::FAILURE;
}
LOG(INFO, "solClient session disconnected");
return returnCode_t::SUCCESS;
}
returnCode_t
SolClientThread::topicSubscribe(std::string topic)
{
solClient_returnCode_t rc;
std::lock_guard<std::mutex> lock(mutex_m);
rc = solClient_session_topicSubscribeExt(
session_mp,
SOLCLIENT_SUBSCRIBE_FLAGS_WAITFORCONFIRM,
topic.c_str());
if (rc != SOLCLIENT_OK)
{
LOG(WARN, "solClient could not subscribe to topic '" << topic
<< "'");
return returnCode_t::FAILURE;
}
LOG(INFO, "solClient subscribed to topic '" << topic << "'");
return returnCode_t::SUCCESS;
}
returnCode_t
SolClientThread::topicUnsubscribe(std::string topic)
{
solClient_returnCode_t rc;
std::lock_guard<std::mutex> lock(mutex_m);
rc = solClient_session_topicUnsubscribeExt(
session_mp,
SOLCLIENT_SUBSCRIBE_FLAGS_WAITFORCONFIRM,
topic.c_str());
if (rc != SOLCLIENT_OK)
{
LOG(WARN, "solClient could not unsubscribe from topic '" << topic
<< "'");
return returnCode_t::FAILURE;
}
LOG(INFO, "solClient unsubscribed from topic '" << topic << "'");
return returnCode_t::SUCCESS;
}
returnCode_t
SolClientThread::startTimer(void)
{
solClient_returnCode_t rc;
std::lock_guard<std::mutex> lock(mutex_m);
rc = solClient_context_startTimer(
context_mp,
SOLCLIENT_CONTEXT_TIMER_REPEAT,
1000, // Timer ticks every second
contextTimerCallback,
nullptr,
&timerId_m);
if (rc != SOLCLIENT_OK)
{
LOG(ERROR, "solClient could not start timer");
return returnCode_t::FAILURE;
}
LOG(INFO, "solClient timer started");
return returnCode_t::SUCCESS;
}
returnCode_t
SolClientThread::stopTimer(void)
{
std::lock_guard<std::mutex> lock(mutex_m);
if (timerId_m == SOLCLIENT_CONTEXT_TIMER_ID_INVALID)
{
return returnCode_t::NOTHING_TO_DO;
}
if (solClient_context_stopTimer(context_mp, &timerId_m) != SOLCLIENT_OK)
{
LOG(ERROR, "solClient could not stop timer");
return returnCode_t::FAILURE;
}
return returnCode_t::SUCCESS;
}
} /* namespace topicMonitor */