blob: e3e6461dfb577a344c8f25452b73c998c9ec238b [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 Vaskoc3146782015-11-04 14:46:41 +0100267struct session_with_mutex *get_ncsession_from_key(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);
351 target_session = get_ncsession_from_key(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 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100534 //DEBUG("get session_with_mutex for %s.", pss->session_key);
Michal Vaskoc3146782015-11-04 14:46:41 +0100535 struct session_with_mutex *ls = get_ncsession_from_key(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 Vaskoc3146782015-11-04 14:46:41 +0100561 DEBUG("notification: POP notifications for session");
Tomas Cejka73286932013-05-27 22:54:35 +0200562
Michal Vaskoc3146782015-11-04 14:46:41 +0100563 for (i = 0; i < ls->notif_count; ++i) {
564 notif = ls->notifications + i;
Tomas Cejka15c56302013-05-30 01:11:30 +0200565
Michal Vaskoc3146782015-11-04 14:46:41 +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 Vaskoc3146782015-11-04 14:46:41 +0100573 const char *msgtext = json_object_to_json_string(notif_json);
574
575 //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 }
583
584 pthread_mutex_lock(&json_lock);
585 json_object_put(notif_json);
586 pthread_mutex_unlock(&json_lock);
587 free(notif->content);
588 }
589 DEBUG("notification: POP notifications done");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200590
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100591 //DEBUG("unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200592 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100593 DEBUG("notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200594 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100595 //DEBUG("unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200596
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100597 if (m < n) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100598 DEBUG("ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200599
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100600 return -1;
601 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100602 break;
603
604 case LWS_CALLBACK_RECEIVE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100605 DEBUG("Callback receive.");
606 DEBUG("received: (%s)", (char *)in);
Michal Vaskoc3146782015-11-04 14:46:41 +0100607 if (pss->session_id == NULL) {
608 char session_id_buf[41];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200609 int start = -1;
610 time_t stop = time(NULL) + 30;
611
Michal Vaskoc3146782015-11-04 14:46:41 +0100612 strncpy((char *) session_id_buf, (const char *) in, 40);
613 session_id_buf[40] = '\0';
614 pss->session_id = strdup(session_id_buf);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200615 sscanf(in+40, "%d %d", (int *) &start, (int *) &stop);
Michal Vaskoc3146782015-11-04 14:46:41 +0100616 DEBUG("notification: get key (%s) from (%s) (%i,%i)", pss->session_id, (char *) in, (int) start, (int) stop);
Tomas Cejka15c56302013-05-30 01:11:30 +0200617
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100618 DEBUG("lock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200619 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100620 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200621 return -1;
622 }
Michal Vaskoc3146782015-11-04 14:46:41 +0100623 DEBUG("get session from key (%s)", pss->session_id);
624 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200625 if (ls == NULL) {
Michal Vaskoc3146782015-11-04 14:46:41 +0100626 DEBUG("notification: session_key not found (%s)", pss->session_id);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100627 DEBUG("unlock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200628 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100629 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200630 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100631 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200632 return -1;
633 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100634 DEBUG("lock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200635 pthread_mutex_lock(&ls->lock);
636
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100637 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200638 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100639 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200640 }
641
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100642 DEBUG("Found session to subscribe notif.");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200643 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100644 DEBUG("session already closed - handle no notification");
645 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200646 pthread_mutex_unlock(&ls->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100647 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200648 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200649 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200650 if (ls->ntfc_subscribed != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100651 DEBUG("notification: already subscribed");
652 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200653 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200654 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200655 return 0;
656 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100657 DEBUG("notification: prepare to subscribe stream");
658 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200659 pthread_mutex_unlock(&ls->lock);
660
661 /* notif_subscribe locks on its own */
Michal Vaskoc3146782015-11-04 14:46:41 +0100662 return notif_subscribe(ls, pss->session_id, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200663 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100664 if (len < 6)
665 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100666 break;
667 /*
668 * this just demonstrates how to use the protocol filter. If you won't
669 * study and reject connections based on header content, you don't need
670 * to handle this callback
671 */
672
673 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
674 //dump_handshake_info(wsi);
675 /* you could return non-zero here and kill the connection */
676 break;
Tomas Cejka866f2282014-09-18 15:20:26 +0200677 //gives segfault :-(
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100678 //case LWS_CALLBACK_CLOSED:
679 // if (pss->session_key != NULL) {
680 // free(pss->session_key);
681 // }
682 // if (pss != NULL) {
683 // free(pss);
684 // }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100685
686 default:
687 break;
688 }
689
690 return 0;
691}
692
693/* list of supported protocols and callbacks */
694
695static struct libwebsocket_protocols protocols[] = {
696 /* first protocol must always be HTTP handler */
697
698 {
699 "http-only", /* name */
700 callback_http, /* callback */
701 sizeof (struct per_session_data__http), /* per_session_data_size */
702 0, /* max frame size / rx buffer */
Michal Vaskoc3146782015-11-04 14:46:41 +0100703 0,
704 NULL,
705 NULL,
706 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100707 },
708 {
709 "notification-protocol",
710 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200711 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200712 4000,
Michal Vaskoc3146782015-11-04 14:46:41 +0100713 0,
714 NULL,
715 NULL,
716 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100717 },
Michal Vaskoc3146782015-11-04 14:46:41 +0100718 { NULL, NULL, 0, 0, 0, NULL, NULL, 0 } /* terminator */
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100719};
720
721
Tomas Cejkaba21b382013-04-13 02:37:32 +0200722/**
723 * initialization of notification module
724 */
Michal Vaskoc3146782015-11-04 14:46:41 +0100725int notification_init(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100726{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200727 //char cert_path[1024];
728 //char key_path[1024];
729 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100730 struct lws_context_creation_info info;
731 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200732 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100733 const char *iface = NULL;
734 int debug_level = 7;
735
736 memset(&info, 0, sizeof info);
737 info.port = NOTIFICATION_SERVER_PORT;
738
739 /* tell the library what debug level to emit and to send it to syslog */
740 lws_set_log_level(debug_level, lwsl_emit_syslog);
741
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100742 DEBUG("Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200743 //lwsl_notice("libwebsockets test server - "
744 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
745 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100746 max_poll_elements = getdtablesize();
747 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
748 fd_lookup = malloc(max_poll_elements * sizeof (int));
749 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200750 ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100751 return -1;
752 }
753
754 info.iface = iface;
755 info.protocols = protocols;
756
757 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
758 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
759
760 //info.ssl_cert_filepath = cert_path;
761 //info.ssl_private_key_filepath = key_path;
762
763 info.gid = -1;
764 info.uid = -1;
765 info.options = opts;
766
767 /* create server */
768 context = libwebsocket_create_context(&info);
769 if (context == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100770 DEBUG("libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100771 return -1;
772 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200773
Tomas Cejka15c56302013-05-30 01:11:30 +0200774 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200775 ERROR("notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200776 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200777 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100778}
779
Michal Vaskoc3146782015-11-04 14:46:41 +0100780void notification_close(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100781{
Tomas Cejka98ed6cc2015-04-27 23:35:18 +0200782 if (context) {
783 libwebsocket_context_destroy(context);
784 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100785 free(pollfds);
786 free(fd_lookup);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100787
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100788 DEBUG("libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100789}
790
Tomas Cejkaba21b382013-04-13 02:37:32 +0200791
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100792/**
793 * \brief send notification if any
794 * \return < 0 on error
795 */
796int notification_handle()
797{
798 static struct timeval tv;
799 static unsigned int olds = 0;
800 int n = 0;
801
802 gettimeofday(&tv, NULL);
803
804 /*
805 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
806 * live websocket connection using the DUMB_INCREMENT protocol,
807 * as soon as it can take more packets (usually immediately)
808 */
809
810 if (((unsigned int)tv.tv_sec - olds) > 0) {
811 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
812 olds = tv.tv_sec;
813 }
814
815
816 /*
817 * this represents an existing server's single poll action
818 * which also includes libwebsocket sockets
819 */
820
821 n = poll(pollfds, count_pollfds, 50);
822 if (n < 0)
823 return n;
824
825
826 if (n) {
827 for (n = 0; n < count_pollfds; n++) {
828 if (pollfds[n].revents) {
829 /*
830 * returns immediately if the fd does not
831 * match anything under libwebsockets
832 * control
833 */
834 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
835 return 1;
836 }
837 }
838 }
839 }
840 return 0;
841}
842
843#endif
844
845
846#ifndef WITH_NOTIFICATIONS
847#ifdef TEST_NOTIFICATION_SERVER
848int main(int argc, char **argv)
849{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100850 if (notification_init(NULL, NULL) == -1) {
851 fprintf(stderr, "Error during initialization\n");
852 return 1;
853 }
854 while (!force_exit) {
855 notification_handle();
856 }
857 notification_close();
858}
859#endif
860#endif