blob: 19d8934c7733130c6c05ba719a17c5a2588d778c [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
Michal Vaskoa53ef882015-11-24 11:02:01 +010036#include "notification_server.h"
37#include "netopeerguid.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");
Michal Vasko43d94a62015-11-05 10:59:32 +0100563 for (i = ls->notif_count; i; --i) {
564 notif = ls->notifications + i - 1;
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 Vasko43d94a62015-11-05 10:59:32 +0100589 ls->notif_count = 0;
590 free(ls->notifications);
591 ls->notifications = NULL;
592
Michal Vasko1ac11282015-11-05 10:28:07 +0100593 DEBUG("notification: POP notifications done");
Michal Vaskoc3146782015-11-04 14:46:41 +0100594 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200595
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100596 //DEBUG("unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200597 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100598 DEBUG("notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200599 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100600 //DEBUG("unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200601
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100602 if (m < n) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100603 DEBUG("ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200604
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100605 return -1;
606 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100607 break;
608
609 case LWS_CALLBACK_RECEIVE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100610 DEBUG("Callback receive.");
611 DEBUG("received: (%s)", (char *)in);
Michal Vaskoc3146782015-11-04 14:46:41 +0100612 if (pss->session_id == NULL) {
Michal Vasko2bff5fb2015-11-05 10:23:02 +0100613 char *sid_end;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200614 int start = -1;
615 time_t stop = time(NULL) + 30;
616
Michal Vasko2bff5fb2015-11-05 10:23:02 +0100617 sid_end = strchr(in, ' ');
618 pss->session_id = strndup(in, sid_end - (char *)in);
619
620 ++sid_end;
621 sscanf(sid_end, "%d %d", (int *) &start, (int *) &stop);
622 DEBUG("notification: SID (%s) from (%s) (%i,%i)", pss->session_id, (char *) in, (int) start, (int) stop);
Tomas Cejka15c56302013-05-30 01:11:30 +0200623
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100624 DEBUG("lock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200625 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100626 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200627 return -1;
628 }
Michal Vasko3d8681d2015-11-05 10:23:41 +0100629 DEBUG("get session with ID (%s)", pss->session_id);
630 struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200631 if (ls == NULL) {
Michal Vasko3d8681d2015-11-05 10:23:41 +0100632 DEBUG("notification: session_id not found (%s)", pss->session_id);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100633 DEBUG("unlock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200634 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100635 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200636 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100637 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200638 return -1;
639 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100640 DEBUG("lock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200641 pthread_mutex_lock(&ls->lock);
642
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100643 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200644 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100645 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200646 }
647
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100648 DEBUG("Found session to subscribe notif.");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200649 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100650 DEBUG("session already closed - handle no notification");
651 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200652 pthread_mutex_unlock(&ls->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100653 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200654 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200655 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200656 if (ls->ntfc_subscribed != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100657 DEBUG("notification: already subscribed");
658 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200659 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200660 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200661 return 0;
662 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100663 DEBUG("notification: prepare to subscribe stream");
664 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200665 pthread_mutex_unlock(&ls->lock);
666
667 /* notif_subscribe locks on its own */
Michal Vaskoc3146782015-11-04 14:46:41 +0100668 return notif_subscribe(ls, pss->session_id, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200669 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100670 if (len < 6)
671 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100672 break;
673 /*
674 * this just demonstrates how to use the protocol filter. If you won't
675 * study and reject connections based on header content, you don't need
676 * to handle this callback
677 */
678
679 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
680 //dump_handshake_info(wsi);
681 /* you could return non-zero here and kill the connection */
682 break;
Tomas Cejka866f2282014-09-18 15:20:26 +0200683 //gives segfault :-(
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100684 //case LWS_CALLBACK_CLOSED:
685 // if (pss->session_key != NULL) {
686 // free(pss->session_key);
687 // }
688 // if (pss != NULL) {
689 // free(pss);
690 // }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100691
692 default:
693 break;
694 }
695
696 return 0;
697}
698
699/* list of supported protocols and callbacks */
700
701static struct libwebsocket_protocols protocols[] = {
702 /* first protocol must always be HTTP handler */
703
704 {
705 "http-only", /* name */
706 callback_http, /* callback */
707 sizeof (struct per_session_data__http), /* per_session_data_size */
708 0, /* max frame size / rx buffer */
Michal Vaskoc3146782015-11-04 14:46:41 +0100709 0,
710 NULL,
711 NULL,
712 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100713 },
714 {
715 "notification-protocol",
716 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200717 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200718 4000,
Michal Vaskoc3146782015-11-04 14:46:41 +0100719 0,
720 NULL,
721 NULL,
722 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100723 },
Michal Vaskoc3146782015-11-04 14:46:41 +0100724 { NULL, NULL, 0, 0, 0, NULL, NULL, 0 } /* terminator */
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100725};
726
727
Tomas Cejkaba21b382013-04-13 02:37:32 +0200728/**
729 * initialization of notification module
730 */
Michal Vaskoc3146782015-11-04 14:46:41 +0100731int notification_init(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100732{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200733 //char cert_path[1024];
734 //char key_path[1024];
735 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100736 struct lws_context_creation_info info;
737 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200738 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100739 const char *iface = NULL;
740 int debug_level = 7;
741
742 memset(&info, 0, sizeof info);
743 info.port = NOTIFICATION_SERVER_PORT;
744
745 /* tell the library what debug level to emit and to send it to syslog */
746 lws_set_log_level(debug_level, lwsl_emit_syslog);
747
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100748 DEBUG("Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200749 //lwsl_notice("libwebsockets test server - "
750 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
751 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100752 max_poll_elements = getdtablesize();
753 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
754 fd_lookup = malloc(max_poll_elements * sizeof (int));
755 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200756 ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100757 return -1;
758 }
759
760 info.iface = iface;
761 info.protocols = protocols;
762
763 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
764 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
765
766 //info.ssl_cert_filepath = cert_path;
767 //info.ssl_private_key_filepath = key_path;
768
769 info.gid = -1;
770 info.uid = -1;
771 info.options = opts;
772
773 /* create server */
774 context = libwebsocket_create_context(&info);
775 if (context == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100776 DEBUG("libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100777 return -1;
778 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200779
Tomas Cejka15c56302013-05-30 01:11:30 +0200780 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200781 ERROR("notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200782 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200783 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100784}
785
Michal Vaskoc3146782015-11-04 14:46:41 +0100786void notification_close(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100787{
Tomas Cejka98ed6cc2015-04-27 23:35:18 +0200788 if (context) {
789 libwebsocket_context_destroy(context);
790 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100791 free(pollfds);
792 free(fd_lookup);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100793
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100794 DEBUG("libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100795}
796
Tomas Cejkaba21b382013-04-13 02:37:32 +0200797
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100798/**
799 * \brief send notification if any
800 * \return < 0 on error
801 */
802int notification_handle()
803{
804 static struct timeval tv;
805 static unsigned int olds = 0;
806 int n = 0;
807
808 gettimeofday(&tv, NULL);
809
810 /*
811 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
812 * live websocket connection using the DUMB_INCREMENT protocol,
813 * as soon as it can take more packets (usually immediately)
814 */
815
816 if (((unsigned int)tv.tv_sec - olds) > 0) {
817 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
818 olds = tv.tv_sec;
819 }
820
821
822 /*
823 * this represents an existing server's single poll action
824 * which also includes libwebsocket sockets
825 */
826
827 n = poll(pollfds, count_pollfds, 50);
828 if (n < 0)
829 return n;
830
831
832 if (n) {
833 for (n = 0; n < count_pollfds; n++) {
834 if (pollfds[n].revents) {
835 /*
836 * returns immediately if the fd does not
837 * match anything under libwebsockets
838 * control
839 */
840 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
841 return 1;
842 }
843 }
844 }
845 }
846 return 0;
847}
848
849#endif
850
851
852#ifndef WITH_NOTIFICATIONS
853#ifdef TEST_NOTIFICATION_SERVER
854int main(int argc, char **argv)
855{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100856 if (notification_init(NULL, NULL) == -1) {
857 fprintf(stderr, "Error during initialization\n");
858 return 1;
859 }
860 while (!force_exit) {
861 notification_handle();
862 }
863 notification_close();
864}
865#endif
866#endif