This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
subscribe.c
210 lines (181 loc) · 5.27 KB
/
subscribe.c
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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "zeromqrecv.h"
#include <kdbhelper.h>
#include <kdblogger.h>
static int setupNotificationCallback (Plugin * handle)
{
ELEKTRA_NOT_NULL (handle);
ElektraZeroMqRecvPluginData * pluginData = elektraPluginGetData (handle);
ELEKTRA_NOT_NULL (pluginData);
KeySet * global = elektraPluginGetGlobalKeySet (handle);
ElektraNotificationCallback callback;
Key * callbackKey = ksLookupByName (global, "system:/elektra/notification/callback", 0);
const void * callbackPtr = keyValue (callbackKey);
if (callbackPtr == NULL)
{
return -1;
}
callback = *(ElektraNotificationCallback *) keyValue (callbackKey);
ElektraNotificationCallbackContext * context;
Key * contextKey = ksLookupByName (global, "system:/elektra/notification/context", 0);
const void * contextPtr = keyValue (contextKey);
context = contextPtr == NULL ? NULL : *(ElektraNotificationCallbackContext **) contextPtr;
pluginData->notificationCallback = callback;
pluginData->notificationContext = context;
return 0;
}
/**
* @internal
* Called whenever the socket becomes readable.
* ZeroMq since sends multipart messages atomically (all or nothing)
* both message parts are instantly available.
*
* @param socket ZeroMq socket
* @param context context passed to elektraIoAdapterZeroMqAttach()
*/
static void zeroMqRecvSocketReadable (void * socket, void * context)
{
Plugin * handle = (Plugin *) context;
ELEKTRA_NOT_NULL (handle);
ElektraZeroMqRecvPluginData * data = elektraPluginGetData (handle);
ELEKTRA_NOT_NULL (data);
if (data->notificationCallback == NULL)
{
if (setupNotificationCallback (handle) != 0)
{
ELEKTRA_LOG_WARNING ("notificationCallback not set up; aborting");
return;
}
}
char * changeType;
char * changedKeyName;
zmq_msg_t message;
zmq_msg_init (&message);
int result = zmq_msg_recv (&message, socket, ZMQ_DONTWAIT);
if (result == -1)
{
ELEKTRA_LOG_WARNING ("receiving change type failed: %s; aborting", zmq_strerror (zmq_errno ()));
zmq_msg_close (&message);
return;
}
if (!zmq_msg_more (&message))
{
ELEKTRA_LOG_WARNING ("message has only one part; aborting");
zmq_msg_close (&message);
return;
}
int length = zmq_msg_size (&message);
changeType = elektraMemDup (zmq_msg_data (&message), length + 1);
changeType[length] = '\0';
ELEKTRA_LOG_DEBUG ("received change type %s", changeType);
result = zmq_msg_recv (&message, socket, ZMQ_DONTWAIT);
if (result == -1)
{
ELEKTRA_LOG_WARNING ("receiving key name failed: %s; aborting", zmq_strerror (zmq_errno ()));
elektraFree (changeType);
zmq_msg_close (&message);
return;
}
length = zmq_msg_size (&message);
changedKeyName = elektraMemDup (zmq_msg_data (&message), length + 1);
changedKeyName[length] = '\0';
ELEKTRA_LOG_DEBUG ("received key name %s", changedKeyName);
// notify about changes
Key * changedKey = keyNew (changedKeyName, KEY_END);
data->notificationCallback (changedKey, data->notificationContext);
zmq_msg_close (&message);
elektraFree (changeType);
elektraFree (changedKeyName);
}
/**
* @internal
* Setup ZeroMq for receiving notifications.
*
* @param handle plugin handle
*/
void elektraZeroMqRecvSetup (Plugin * handle)
{
ElektraZeroMqRecvPluginData * data = elektraPluginGetData (handle);
ELEKTRA_NOT_NULL (data);
// create zmq context
if (!data->zmqContext)
{
data->zmqContext = zmq_ctx_new ();
if (data->zmqContext == NULL)
{
ELEKTRA_LOG_WARNING ("zmq_ctx_new failed %s", zmq_strerror (zmq_errno ()));
return;
}
}
// create publish socket
if (!data->zmqSubscriber)
{
data->zmqSubscriber = zmq_socket (data->zmqContext, ZMQ_SUB);
if (data->zmqSubscriber == NULL)
{
ELEKTRA_LOG_WARNING ("zmq_socket failed %s", zmq_strerror (zmq_errno ()));
zmq_close (data->zmqSubscriber);
return;
}
// subscribe to notifications
char * keyCommitType = "Commit";
if (zmq_setsockopt (data->zmqSubscriber, ZMQ_SUBSCRIBE, keyCommitType, elektraStrLen (keyCommitType)) != 0)
{
ELEKTRA_LOG_WARNING ("failed to subscribe to %s messages", keyCommitType);
}
// connect to endpoint
int result = zmq_connect (data->zmqSubscriber, data->endpoint);
if (result != 0)
{
ELEKTRA_LOG_WARNING ("zmq_connect error: %s\n", zmq_strerror (zmq_errno ()));
zmq_close (data->zmqSubscriber);
data->zmqSubscriber = NULL;
return;
}
}
if (!data->zmqAdapter)
{
// attach ZeroMq adater and wait for socket to be writable
data->zmqAdapter = elektraIoAdapterZeroMqAttach (data->zmqSubscriber, data->ioBinding, ELEKTRA_IO_ADAPTER_ZEROMQCB_READ,
zeroMqRecvSocketReadable, handle);
if (!data->zmqAdapter)
{
ELEKTRA_LOG_WARNING ("could not attach zmq adapter");
zmq_close (data->zmqSubscriber);
data->zmqSubscriber = NULL;
return;
}
}
}
/**
* @internal
* Cleanup ZeroMq.
*
* @param handle plugin handle
*/
void elektraZeroMqRecvTeardown (Plugin * handle)
{
ElektraZeroMqRecvPluginData * data = elektraPluginGetData (handle);
ELEKTRA_NOT_NULL (data);
if (data->zmqAdapter)
{
elektraIoAdapterZeroMqDetach (data->zmqAdapter);
data->zmqAdapter = NULL;
}
if (data->zmqSubscriber)
{
zmq_close (data->zmqSubscriber);
data->zmqSubscriber = NULL;
}
if (data->zmqContext)
{
zmq_ctx_destroy (data->zmqContext);
data->zmqContext = NULL;
}
}