blob: 588594b210e06d947d6ae1440122ca7dc06e1637 [file] [log] [blame]
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001/*
2 * libwebsockets-test-server - libwebsockets test implementation
3 *
4 * Copyright (C) 2010-2011 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <getopt.h>
25#include <string.h>
26#include <sys/time.h>
27#include <sys/stat.h>
Tomas Cejkaba21b382013-04-13 02:37:32 +020028#include <sys/queue.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010029#include <fcntl.h>
30#include <assert.h>
Tomas Cejkaba21b382013-04-13 02:37:32 +020031#include <pthread.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010032#include <errno.h>
Tomas Cejkaba21b382013-04-13 02:37:32 +020033#include <libnetconf.h>
34#include <libwebsockets.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010035
Tomas Cejkad340dbf2013-03-24 20:36:57 +010036#include "notification_module.h"
Tomas Cejkaba21b382013-04-13 02:37:32 +020037#include "mod_netconf.h"
Michal Vaskoc3146782015-11-04 14:46:41 +010038#include "../config.h"
Tomas Cejkad340dbf2013-03-24 20:36:57 +010039
Michal Vaskoc3146782015-11-04 14:46:41 +010040#ifdef TEST_NOTIFICATION_SERVER
Tomas Cejkaba21b382013-04-13 02:37:32 +020041static int force_exit = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010042#endif
43
44#if defined(TEST_NOTIFICATION_SERVER) || defined(WITH_NOTIFICATIONS)
Tomas Cejkad340dbf2013-03-24 20:36:57 +010045static int max_poll_elements;
46
47static struct pollfd *pollfds;
48static int *fd_lookup;
49static int count_pollfds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010050static struct libwebsocket_context *context = NULL;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010051
Tomas Cejkaba21b382013-04-13 02:37:32 +020052struct ntf_thread_config {
53 struct nc_session *session;
Michal Vaskoc3146782015-11-04 14:46:41 +010054 char *session_id;
Tomas Cejkaba21b382013-04-13 02:37:32 +020055};
56
Michal Vaskoc3146782015-11-04 14:46:41 +010057extern struct session_with_mutex *netconf_sessions_list;
Tomas Cejkaba21b382013-04-13 02:37:32 +020058static pthread_key_t thread_key;
59
Tomas Cejkad340dbf2013-03-24 20:36:57 +010060/*
61 * This demo server shows how to use libwebsockets for one or more
62 * websocket protocols in the same server
63 *
64 * It defines the following websocket protocols:
65 *
66 * dumb-increment-protocol: once the socket is opened, an incrementing
67 * ascii string is sent down it every 50ms.
68 * If you send "reset\n" on the websocket, then
69 * the incrementing number is reset to 0.
70 *
71 * lws-mirror-protocol: copies any received packet to every connection also
72 * using this protocol, including the sender
73 */
74
75enum demo_protocols {
76 /* always first */
77 PROTOCOL_HTTP = 0,
78
79 PROTOCOL_NOTIFICATION,
80
81 /* always last */
82 DEMO_PROTOCOL_COUNT
83};
84
85
86#define LOCAL_RESOURCE_PATH "."
87char *resource_path = LOCAL_RESOURCE_PATH;
88
89/*
90 * We take a strict whitelist approach to stop ../ attacks
91 */
92
93struct serveable {
94 const char *urlpath;
95 const char *mimetype;
Tomas Cejka15c56302013-05-30 01:11:30 +020096};
Tomas Cejkad340dbf2013-03-24 20:36:57 +010097
98static const struct serveable whitelist[] = {
99 { "/favicon.ico", "image/x-icon" },
100 { "/libwebsockets.org-logo.png", "image/png" },
101
102 /* last one is the default served if no match */
103 { "/test.html", "text/html" },
104};
105
106struct per_session_data__http {
107 int fd;
108};
109
110/* this protocol server (always the first one) just knows how to do HTTP */
111
112static int callback_http(struct libwebsocket_context *context,
113 struct libwebsocket *wsi,
114 enum libwebsocket_callback_reasons reason, void *user,
Michal Vaskoc3146782015-11-04 14:46:41 +0100115 void *in, size_t UNUSED(len))
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100116{
117 char client_name[128];
118 char client_ip[128];
119 char buf[256];
120 int n, m;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100121 static unsigned char buffer[4096];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100122 struct per_session_data__http *pss = (struct per_session_data__http *)user;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200123 struct libwebsocket_pollargs *pa = (struct libwebsocket_pollargs *) in;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100124
125 switch (reason) {
126 case LWS_CALLBACK_HTTP:
Michal Vaskoc3146782015-11-04 14:46:41 +0100127 for (n = 0; n < (signed) (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100128 if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
129 break;
130
131 sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath);
132
Tomas Cejkadc8f08d2015-04-29 11:08:24 +0200133 if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype, NULL, 0))
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100134 return -1; /* through completion or error, close the socket */
135
136 /*
137 * notice that the sending of the file completes asynchronously,
138 * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
139 * it's done
140 */
141
142 break;
143
144 case LWS_CALLBACK_HTTP_FILE_COMPLETION:
145// lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n");
146 /* kill the connection after we sent one file */
147 return -1;
148
149 case LWS_CALLBACK_HTTP_WRITEABLE:
150 /*
151 * we can send more of whatever it is we were sending
152 */
153
154 do {
155 n = read(pss->fd, buffer, sizeof buffer);
156 /* problem reading, close conn */
157 if (n < 0)
158 goto bail;
159 /* sent it all, close conn */
160 if (n == 0)
161 goto bail;
162 /*
163 * because it's HTTP and not websocket, don't need to take
164 * care about pre and postamble
165 */
166 m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP);
167 if (m < 0)
168 /* write failed, close conn */
169 goto bail;
170 if (m != n)
171 /* partial write, adjust */
172 lseek(pss->fd, m - n, SEEK_CUR);
173
174 } while (!lws_send_pipe_choked(wsi));
175 libwebsocket_callback_on_writable(context, wsi);
176 break;
177
178bail:
179 close(pss->fd);
180 return -1;
181
Tomas Cejka11d5d062014-07-15 13:13:52 +0200182 case LWS_CALLBACK_LOCK_POLL:
183 /*
184 * lock mutex to protect pollfd state
185 * called before any other POLL related callback
186 */
187 break;
188
189 case LWS_CALLBACK_UNLOCK_POLL:
190 /*
191 * unlock mutex to protect pollfd state when
192 * called after any other POLL related callback
193 */
194 break;
195
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100196 /*
197 * callback for confirming to continue with client IP appear in
198 * protocol 0 callback since no websocket protocol has been agreed
199 * yet. You can just ignore this if you won't filter on client IP
200 * since the default uhandled callback return is 0 meaning let the
201 * connection continue.
202 */
203
204 case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
205 libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name,
206 sizeof(client_name), client_ip, sizeof(client_ip));
207
Tomas Cejkaba21b382013-04-13 02:37:32 +0200208 //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100209 /* if we returned non-zero from here, we kill the connection */
210 break;
211
212 /*
213 * callbacks for managing the external poll() array appear in
214 * protocol 0 callback
215 */
216
217 case LWS_CALLBACK_ADD_POLL_FD:
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100218 if (count_pollfds >= max_poll_elements) {
219 lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
220 return 1;
221 }
222
Tomas Cejka11d5d062014-07-15 13:13:52 +0200223 fd_lookup[pa->fd] = count_pollfds;
224 pollfds[count_pollfds].fd = pa->fd;
225 pollfds[count_pollfds].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100226 pollfds[count_pollfds++].revents = 0;
227 break;
228
229 case LWS_CALLBACK_DEL_POLL_FD:
230 if (!--count_pollfds)
231 break;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200232 m = fd_lookup[pa->fd];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100233 /* have the last guy take up the vacant slot */
234 pollfds[m] = pollfds[count_pollfds];
235 fd_lookup[pollfds[count_pollfds].fd] = m;
236 break;
237
Tomas Cejka11d5d062014-07-15 13:13:52 +0200238 case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
239 pollfds[fd_lookup[pa->fd]].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100240 break;
241
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100242
243 default:
244 break;
245 }
246
247 return 0;
248}
249
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100250
251/* dumb_increment protocol */
252
253/*
254 * one of these is auto-created for each connection and a pointer to the
255 * appropriate instance is passed to the callback in the user parameter
256 *
257 * for this example protocol we use it to individualize the count for each
258 * connection.
259 */
260
Tomas Cejkaba21b382013-04-13 02:37:32 +0200261struct per_session_data__notif_client {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100262 int number;
Michal Vaskoc3146782015-11-04 14:46:41 +0100263 char *session_id;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200264 struct nc_session *session;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100265};
266
Michal Vasko3d8681d2015-11-05 10:23:41 +0100267struct session_with_mutex *get_ncsession_from_sid(const char *session_id)
Tomas Cejkaba21b382013-04-13 02:37:32 +0200268{
269 struct session_with_mutex *locked_session = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100270 if (session_id == NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200271 return (NULL);
272 }
Michal Vaskoc3146782015-11-04 14:46:41 +0100273 for (locked_session = netconf_sessions_list;
Michal Vasko59ccb362015-11-05 10:21:44 +0100274 locked_session && strcmp(nc_session_get_id(locked_session->session), session_id);
Michal Vaskoc3146782015-11-04 14:46:41 +0100275 locked_session = locked_session->next);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200276 return locked_session;
277}
278
279/* rpc parameter is freed after the function call */
Michal Vaskoc3146782015-11-04 14:46:41 +0100280static int send_recv_process(struct nc_session *session, const char* UNUSED(operation), nc_rpc* rpc)
Tomas Cejkaba21b382013-04-13 02:37:32 +0200281{
282 nc_reply *reply = NULL;
283 char *data = NULL;
284 int ret = EXIT_SUCCESS;
285
286 /* send the request and get the reply */
287 switch (nc_session_send_recv(session, rpc, &reply)) {
288 case NC_MSG_UNKNOWN:
289 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200290 ERROR("notifications: receiving rpc-reply failed.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200291 //cmd_disconnect(NULL);
292 ret = EXIT_FAILURE;
293 break;
294 }
Tomas Cejkacf44e522015-04-24 17:29:21 +0200295 ERROR("notifications: Unknown error occurred.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200296 ret = EXIT_FAILURE;
297 break;
298 case NC_MSG_NONE:
299 /* error occurred, but processed by callback */
300 break;
301 case NC_MSG_REPLY:
302 switch (nc_reply_get_type(reply)) {
303 case NC_REPLY_OK:
304 break;
305 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100306 DEBUG("notifications: recv: %s.", data = nc_reply_get_data (reply));
307 free(data);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200308 break;
309 case NC_REPLY_ERROR:
310 /* wtf, you shouldn't be here !?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100311 DEBUG("notifications: operation failed, but rpc-error was not processed.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200312 ret = EXIT_FAILURE;
313 break;
314 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100315 DEBUG("notifications: unexpected operation result.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200316 ret = EXIT_FAILURE;
317 break;
318 }
319 break;
320 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100321 DEBUG("notifications: Unknown error occurred.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200322 ret = EXIT_FAILURE;
323 break;
324 }
325 nc_rpc_free(rpc);
326 nc_reply_free(reply);
327
328 return (ret);
329}
330
331/**
332 * \brief Callback to store incoming notification
333 * \param [in] eventtime - time when notification occured
334 * \param [in] content - content of notification
335 */
336static void notification_fileprint (time_t eventtime, const char* content)
337{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200338 struct session_with_mutex *target_session = NULL;
339 notification_t *ntf = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100340 const char *session_id = NULL;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200341
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100342 DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content);
Tomas Cejka15c56302013-05-30 01:11:30 +0200343
Michal Vaskoc3146782015-11-04 14:46:41 +0100344 session_id = pthread_getspecific(thread_key);
345 DEBUG("notification: fileprint getspecific (%s)", session_id);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200346 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200347 ERROR("notifications: Error while locking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200348 return;
349 }
Michal Vaskoc3146782015-11-04 14:46:41 +0100350 DEBUG("Get session with mutex from key %s.", session_id);
Michal Vasko3d8681d2015-11-05 10:23:41 +0100351 target_session = get_ncsession_from_sid(session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200352 if (target_session == NULL) {
Michal Vaskoc3146782015-11-04 14:46:41 +0100353 ERROR("notifications: no session found last_session_key (%s)", session_id);
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200354 goto unlock_glob;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200355 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200356 if (pthread_mutex_lock(&target_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200357 ERROR("notifications: Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200358 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200359
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100360 DEBUG("notification: ready to push to notifications queue");
Michal Vaskoc3146782015-11-04 14:46:41 +0100361 if (target_session->notif_count < NOTIFICATION_QUEUE_SIZE) {
362 ++target_session->notif_count;
363 target_session->notifications = realloc(target_session->notifications,
364 target_session->notif_count * sizeof *target_session->notifications);
365 if (target_session->notifications) {
366 ntf = target_session->notifications + target_session->notif_count - 1;
367 }
368 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200369 if (ntf == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200370 ERROR("notifications: Failed to allocate element ");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200371 goto unlock_all;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200372 }
Tomas Cejka73286932013-05-27 22:54:35 +0200373 ntf->eventtime = eventtime;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200374 ntf->content = strdup(content);
375
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100376 DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200377
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200378unlock_all:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200379 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200380 ERROR("notifications: Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200381 }
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200382unlock_glob:
383 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200384 ERROR("notifications: Error while locking rwlock");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200385 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200386}
387
388/**
389 * \brief Thread for libnetconf notifications dispatch
390 * \param [in] arg - struct ntf_thread_config * with nc_session
391 */
392void* notification_thread(void* arg)
393{
394 struct ntf_thread_config *config = (struct ntf_thread_config*)arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100395 DEBUG("notifications: in thread for libnetconf notifications");
Tomas Cejka15c56302013-05-30 01:11:30 +0200396
397 /* store hash identification of netconf session for notifications printing callback */
Michal Vaskoc3146782015-11-04 14:46:41 +0100398 if (pthread_setspecific(thread_key, config->session_id) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200399 ERROR("notifications: cannot set thread-specific hash value.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200400 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200401
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100402 DEBUG("notifications: dispatching");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200403 ncntf_dispatch_receive(config->session, notification_fileprint);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100404 DEBUG("notifications: ended thread for libnetconf notifications");
Michal Vaskoc3146782015-11-04 14:46:41 +0100405 if (config->session_id != NULL) {
406 free(config->session_id);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100407 }
408 if (config != NULL) {
409 free(config);
410 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200411 return (NULL);
412}
413
414
Michal Vaskoc3146782015-11-04 14:46:41 +0100415int notif_subscribe(struct session_with_mutex *locked_session, const char *session_id, time_t start_time, time_t stop_time)
Tomas Cejkaba21b382013-04-13 02:37:32 +0200416{
417 time_t start = -1;
418 time_t stop = -1;
419 struct nc_filter *filter = NULL;
420 char *stream = NULL;
421 nc_rpc *rpc = NULL;
422 pthread_t thread;
423 struct ntf_thread_config *tconfig;
424 struct nc_session *session;
425
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100426 DEBUG("notif_subscribe");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200427 if (locked_session == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100428 DEBUG("notifications: no locked_session was given.");
Tomas Cejkacf44e522015-04-24 17:29:21 +0200429 /* Close notification client */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200430 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200431 }
432
Tomas Cejka47387fd2013-06-10 20:37:46 +0200433 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200434 session = locked_session->session;
435
436 start = time(NULL) + start_time;
437 stop = time(NULL) + stop_time;
Tomas Cejka57714142014-06-22 17:59:58 +0200438 start = 0;
439 stop = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100440 DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200441
442 if (session == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200443 ERROR("notifications: NETCONF session not established.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200444 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200445 }
446
447 /* check if notifications are allowed on this session */
448 if (nc_session_notif_allowed(session) == 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200449 ERROR("notifications: Notification subscription is not allowed on this session.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200450 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200451 }
452 /* check times */
453 if (start != -1 && stop != -1 && start > stop) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200454 ERROR("notifications: Subscription start time must be lower than the end time.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200455 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200456 }
457
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100458 DEBUG("Prepare to execute subscription.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200459 /* create requests */
Tomas Cejka9ca00802014-07-08 16:03:26 +0200460 rpc = nc_rpc_subscribe(stream, filter, (start_time == -1)?NULL:&start, (stop_time == 0)?NULL:&stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200461 nc_filter_free(filter);
462 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200463 ERROR("notifications: creating an rpc request failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200464 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200465 }
466
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100467 DEBUG("Send NC subscribe.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200468 create_err_reply_p();
Tomas Cejkaba21b382013-04-13 02:37:32 +0200469 if (send_recv_process(session, "subscribe", rpc) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200470 ERROR("Subscription RPC failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200471 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200472 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200473
474 GETSPEC_ERR_REPLY
475 if (err_reply != NULL) {
476 free_err_reply();
Tomas Cejkacf44e522015-04-24 17:29:21 +0200477 ERROR("RPC-Error received and cleaned, because we can't send it anywhere.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200478 goto operation_failed;
479 }
480
Tomas Cejkaba21b382013-04-13 02:37:32 +0200481 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200482 locked_session->ntfc_subscribed = 1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200483
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100484 DEBUG("Create config for notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200485 tconfig = malloc(sizeof(struct ntf_thread_config));
Tomas Cejkacf44e522015-04-24 17:29:21 +0200486 if (tconfig == NULL) {
487 ERROR("notifications: Allocation failed.");
488 goto operation_failed;
489 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200490 tconfig->session = session;
Michal Vaskoc3146782015-11-04 14:46:41 +0100491 tconfig->session_id = strdup(session_id);
492 DEBUG("notifications: creating libnetconf notification thread (%s).", tconfig->session_id);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200493
Tomas Cejka47387fd2013-06-10 20:37:46 +0200494 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100495 DEBUG("Create notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200496 if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200497 ERROR("notifications: creating a thread for receiving notifications failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200498 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200499 }
500 pthread_detach(thread);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100501 DEBUG("Subscription finished.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200502 return 0;
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200503
504operation_failed:
505 pthread_mutex_unlock(&locked_session->lock);
506 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200507}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100508
509static int callback_notification(struct libwebsocket_context *context,
510 struct libwebsocket *wsi,
511 enum libwebsocket_callback_reasons reason,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200512 void *user, void *in, size_t len)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100513{
Michal Vaskoc3146782015-11-04 14:46:41 +0100514 int n = 0, m = 0, i;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200515 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100516 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200517 struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user;
518
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100519 switch (reason) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100520 case LWS_CALLBACK_ESTABLISHED:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100521 DEBUG("notification client connected.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100522 break;
523
524 case LWS_CALLBACK_SERVER_WRITEABLE:
Michal Vaskoc3146782015-11-04 14:46:41 +0100525 if (pss->session_id == NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200526 return 0;
527 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100528 //DEBUG("Callback server writeable.");
529 //DEBUG("lock session lock.");
Tomas Cejka866f2282014-09-18 15:20:26 +0200530 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100531 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200532 return -1;
533 }
Michal Vasko3d8681d2015-11-05 10:23:41 +0100534 //DEBUG("get session_with_mutex for %s.", pss->session_id);
535 struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200536 if (ls == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100537 DEBUG("notification: session not found");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200538 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100539 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200540 return -1;
541 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200542 return -1;
543 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200544 pthread_mutex_lock(&ls->lock);
Tomas Cejka866f2282014-09-18 15:20:26 +0200545 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100546 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkaba21b382013-04-13 02:37:32 +0200547 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200548
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100549 //DEBUG("check for closed session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200550 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100551 DEBUG("unlock session key.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200552 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100553 DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200554 return -1;
Tomas Cejka654f84e2013-04-19 11:55:01 +0200555 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200556 return -1;
557 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100558 //DEBUG("lock private lock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200559 notification_t *notif = NULL;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200560
Michal Vasko1ac11282015-11-05 10:28:07 +0100561 if (ls->notif_count) {
562 DEBUG("notification: POP notifications for session");
563 for (i = 0; i < ls->notif_count; ++i) {
564 notif = ls->notifications + i;
Tomas Cejka73286932013-05-27 22:54:35 +0200565
Michal Vasko1ac11282015-11-05 10:28:07 +0100566 n = 0;
567 pthread_mutex_lock(&json_lock);
568 json_object *notif_json = json_object_new_object();
569 json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime));
570 json_object_object_add(notif_json, "content", json_object_new_string(notif->content));
571 pthread_mutex_unlock(&json_lock);
Tomas Cejka15c56302013-05-30 01:11:30 +0200572
Michal Vasko1ac11282015-11-05 10:28:07 +0100573 const char *msgtext = json_object_to_json_string(notif_json);
Tomas Cejka15c56302013-05-30 01:11:30 +0200574
Michal Vasko1ac11282015-11-05 10:28:07 +0100575 //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t);
576 n = sprintf((char *)p, "%s", msgtext);
577 DEBUG("ws send %dB in %lu", n, sizeof(buf));
578 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
579 if (lws_send_pipe_choked(wsi)) {
580 libwebsocket_callback_on_writable(context, wsi);
581 break;
582 }
Michal Vaskoc3146782015-11-04 14:46:41 +0100583
Michal Vasko1ac11282015-11-05 10:28:07 +0100584 pthread_mutex_lock(&json_lock);
585 json_object_put(notif_json);
586 pthread_mutex_unlock(&json_lock);
587 free(notif->content);
Michal Vaskoc3146782015-11-04 14:46:41 +0100588 }
Michal Vasko1ac11282015-11-05 10:28:07 +0100589 DEBUG("notification: POP notifications done");
Michal Vaskoc3146782015-11-04 14:46:41 +0100590 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200591
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100592 //DEBUG("unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200593 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100594 DEBUG("notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200595 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100596 //DEBUG("unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200597
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100598 if (m < n) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100599 DEBUG("ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200600
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100601 return -1;
602 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100603 break;
604
605 case LWS_CALLBACK_RECEIVE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100606 DEBUG("Callback receive.");
607 DEBUG("received: (%s)", (char *)in);
Michal Vaskoc3146782015-11-04 14:46:41 +0100608 if (pss->session_id == NULL) {
Michal Vasko2bff5fb2015-11-05 10:23:02 +0100609 char *sid_end;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200610 int start = -1;
611 time_t stop = time(NULL) + 30;
612
Michal Vasko2bff5fb2015-11-05 10:23:02 +0100613 sid_end = strchr(in, ' ');
614 pss->session_id = strndup(in, sid_end - (char *)in);
615
616 ++sid_end;
617 sscanf(sid_end, "%d %d", (int *) &start, (int *) &stop);
618 DEBUG("notification: SID (%s) from (%s) (%i,%i)", pss->session_id, (char *) in, (int) start, (int) stop);
Tomas Cejka15c56302013-05-30 01:11:30 +0200619
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100620 DEBUG("lock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200621 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100622 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200623 return -1;
624 }
Michal Vasko3d8681d2015-11-05 10:23:41 +0100625 DEBUG("get session with ID (%s)", pss->session_id);
626 struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200627 if (ls == NULL) {
Michal Vasko3d8681d2015-11-05 10:23:41 +0100628 DEBUG("notification: session_id not found (%s)", pss->session_id);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100629 DEBUG("unlock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200630 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100631 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200632 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100633 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200634 return -1;
635 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100636 DEBUG("lock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200637 pthread_mutex_lock(&ls->lock);
638
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100639 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200640 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100641 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200642 }
643
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100644 DEBUG("Found session to subscribe notif.");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200645 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100646 DEBUG("session already closed - handle no notification");
647 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200648 pthread_mutex_unlock(&ls->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100649 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200650 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200651 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200652 if (ls->ntfc_subscribed != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100653 DEBUG("notification: already subscribed");
654 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200655 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200656 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200657 return 0;
658 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100659 DEBUG("notification: prepare to subscribe stream");
660 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200661 pthread_mutex_unlock(&ls->lock);
662
663 /* notif_subscribe locks on its own */
Michal Vaskoc3146782015-11-04 14:46:41 +0100664 return notif_subscribe(ls, pss->session_id, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200665 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100666 if (len < 6)
667 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100668 break;
669 /*
670 * this just demonstrates how to use the protocol filter. If you won't
671 * study and reject connections based on header content, you don't need
672 * to handle this callback
673 */
674
675 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
676 //dump_handshake_info(wsi);
677 /* you could return non-zero here and kill the connection */
678 break;
Tomas Cejka866f2282014-09-18 15:20:26 +0200679 //gives segfault :-(
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100680 //case LWS_CALLBACK_CLOSED:
681 // if (pss->session_key != NULL) {
682 // free(pss->session_key);
683 // }
684 // if (pss != NULL) {
685 // free(pss);
686 // }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100687
688 default:
689 break;
690 }
691
692 return 0;
693}
694
695/* list of supported protocols and callbacks */
696
697static struct libwebsocket_protocols protocols[] = {
698 /* first protocol must always be HTTP handler */
699
700 {
701 "http-only", /* name */
702 callback_http, /* callback */
703 sizeof (struct per_session_data__http), /* per_session_data_size */
704 0, /* max frame size / rx buffer */
Michal Vaskoc3146782015-11-04 14:46:41 +0100705 0,
706 NULL,
707 NULL,
708 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100709 },
710 {
711 "notification-protocol",
712 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200713 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200714 4000,
Michal Vaskoc3146782015-11-04 14:46:41 +0100715 0,
716 NULL,
717 NULL,
718 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100719 },
Michal Vaskoc3146782015-11-04 14:46:41 +0100720 { NULL, NULL, 0, 0, 0, NULL, NULL, 0 } /* terminator */
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100721};
722
723
Tomas Cejkaba21b382013-04-13 02:37:32 +0200724/**
725 * initialization of notification module
726 */
Michal Vaskoc3146782015-11-04 14:46:41 +0100727int notification_init(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100728{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200729 //char cert_path[1024];
730 //char key_path[1024];
731 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100732 struct lws_context_creation_info info;
733 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200734 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100735 const char *iface = NULL;
736 int debug_level = 7;
737
738 memset(&info, 0, sizeof info);
739 info.port = NOTIFICATION_SERVER_PORT;
740
741 /* tell the library what debug level to emit and to send it to syslog */
742 lws_set_log_level(debug_level, lwsl_emit_syslog);
743
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100744 DEBUG("Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200745 //lwsl_notice("libwebsockets test server - "
746 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
747 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100748 max_poll_elements = getdtablesize();
749 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
750 fd_lookup = malloc(max_poll_elements * sizeof (int));
751 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200752 ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100753 return -1;
754 }
755
756 info.iface = iface;
757 info.protocols = protocols;
758
759 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
760 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
761
762 //info.ssl_cert_filepath = cert_path;
763 //info.ssl_private_key_filepath = key_path;
764
765 info.gid = -1;
766 info.uid = -1;
767 info.options = opts;
768
769 /* create server */
770 context = libwebsocket_create_context(&info);
771 if (context == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100772 DEBUG("libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100773 return -1;
774 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200775
Tomas Cejka15c56302013-05-30 01:11:30 +0200776 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200777 ERROR("notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200778 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200779 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100780}
781
Michal Vaskoc3146782015-11-04 14:46:41 +0100782void notification_close(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100783{
Tomas Cejka98ed6cc2015-04-27 23:35:18 +0200784 if (context) {
785 libwebsocket_context_destroy(context);
786 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100787 free(pollfds);
788 free(fd_lookup);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100789
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100790 DEBUG("libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100791}
792
Tomas Cejkaba21b382013-04-13 02:37:32 +0200793
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100794/**
795 * \brief send notification if any
796 * \return < 0 on error
797 */
798int notification_handle()
799{
800 static struct timeval tv;
801 static unsigned int olds = 0;
802 int n = 0;
803
804 gettimeofday(&tv, NULL);
805
806 /*
807 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
808 * live websocket connection using the DUMB_INCREMENT protocol,
809 * as soon as it can take more packets (usually immediately)
810 */
811
812 if (((unsigned int)tv.tv_sec - olds) > 0) {
813 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
814 olds = tv.tv_sec;
815 }
816
817
818 /*
819 * this represents an existing server's single poll action
820 * which also includes libwebsocket sockets
821 */
822
823 n = poll(pollfds, count_pollfds, 50);
824 if (n < 0)
825 return n;
826
827
828 if (n) {
829 for (n = 0; n < count_pollfds; n++) {
830 if (pollfds[n].revents) {
831 /*
832 * returns immediately if the fd does not
833 * match anything under libwebsockets
834 * control
835 */
836 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
837 return 1;
838 }
839 }
840 }
841 }
842 return 0;
843}
844
845#endif
846
847
848#ifndef WITH_NOTIFICATIONS
849#ifdef TEST_NOTIFICATION_SERVER
850int main(int argc, char **argv)
851{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100852 if (notification_init(NULL, NULL) == -1) {
853 fprintf(stderr, "Error during initialization\n");
854 return 1;
855 }
856 while (!force_exit) {
857 notification_handle();
858 }
859 notification_close();
860}
861#endif
862#endif