blob: 8e06828f0a78f131bd7881bd9917c6222d344049 [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>
Tomas Cejkaba21b382013-04-13 02:37:32 +020030#include <pthread.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010031#include <errno.h>
Michal Vaskof35ea502016-02-24 10:44:54 +010032#include <nc_client.h>
Tomas Cejkaba21b382013-04-13 02:37:32 +020033#include <libwebsockets.h>
Michal Vaskoc3146782015-11-04 14:46:41 +010034
Michal Vaskoa53ef882015-11-24 11:02:01 +010035#include "notification_server.h"
36#include "netopeerguid.h"
Michal Vaskoc3146782015-11-04 14:46:41 +010037#include "../config.h"
Tomas Cejkad340dbf2013-03-24 20:36:57 +010038
Michal Vaskoc3146782015-11-04 14:46:41 +010039#ifdef TEST_NOTIFICATION_SERVER
Tomas Cejkaba21b382013-04-13 02:37:32 +020040static int force_exit = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010041#endif
42
43#if defined(TEST_NOTIFICATION_SERVER) || defined(WITH_NOTIFICATIONS)
Tomas Cejkad340dbf2013-03-24 20:36:57 +010044static int max_poll_elements;
45
46static struct pollfd *pollfds;
47static int *fd_lookup;
48static int count_pollfds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010049static struct libwebsocket_context *context = NULL;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010050
Tomas Cejkaba21b382013-04-13 02:37:32 +020051struct ntf_thread_config {
52 struct nc_session *session;
Michal Vaskoc3146782015-11-04 14:46:41 +010053 char *session_id;
Tomas Cejkaba21b382013-04-13 02:37:32 +020054};
55
Michal Vaskoc3146782015-11-04 14:46:41 +010056extern struct session_with_mutex *netconf_sessions_list;
Tomas Cejkaba21b382013-04-13 02:37:32 +020057static pthread_key_t thread_key;
58
Tomas Cejkad340dbf2013-03-24 20:36:57 +010059/*
60 * This demo server shows how to use libwebsockets for one or more
61 * websocket protocols in the same server
62 *
63 * It defines the following websocket protocols:
64 *
65 * dumb-increment-protocol: once the socket is opened, an incrementing
66 * ascii string is sent down it every 50ms.
67 * If you send "reset\n" on the websocket, then
68 * the incrementing number is reset to 0.
69 *
70 * lws-mirror-protocol: copies any received packet to every connection also
71 * using this protocol, including the sender
72 */
73
74enum demo_protocols {
75 /* always first */
76 PROTOCOL_HTTP = 0,
77
78 PROTOCOL_NOTIFICATION,
79
80 /* always last */
81 DEMO_PROTOCOL_COUNT
82};
83
84
85#define LOCAL_RESOURCE_PATH "."
86char *resource_path = LOCAL_RESOURCE_PATH;
87
88/*
89 * We take a strict whitelist approach to stop ../ attacks
90 */
91
92struct serveable {
93 const char *urlpath;
94 const char *mimetype;
Tomas Cejka15c56302013-05-30 01:11:30 +020095};
Tomas Cejkad340dbf2013-03-24 20:36:57 +010096
97static const struct serveable whitelist[] = {
98 { "/favicon.ico", "image/x-icon" },
99 { "/libwebsockets.org-logo.png", "image/png" },
100
101 /* last one is the default served if no match */
102 { "/test.html", "text/html" },
103};
104
105struct per_session_data__http {
106 int fd;
107};
108
109/* this protocol server (always the first one) just knows how to do HTTP */
110
111static int callback_http(struct libwebsocket_context *context,
112 struct libwebsocket *wsi,
113 enum libwebsocket_callback_reasons reason, void *user,
Michal Vaskoc3146782015-11-04 14:46:41 +0100114 void *in, size_t UNUSED(len))
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100115{
116 char client_name[128];
117 char client_ip[128];
118 char buf[256];
119 int n, m;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100120 static unsigned char buffer[4096];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100121 struct per_session_data__http *pss = (struct per_session_data__http *)user;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200122 struct libwebsocket_pollargs *pa = (struct libwebsocket_pollargs *) in;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100123
124 switch (reason) {
125 case LWS_CALLBACK_HTTP:
Michal Vaskoc3146782015-11-04 14:46:41 +0100126 for (n = 0; n < (signed) (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100127 if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
128 break;
129
130 sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath);
131
Tomas Cejkadc8f08d2015-04-29 11:08:24 +0200132 if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype, NULL, 0))
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100133 return -1; /* through completion or error, close the socket */
134
135 /*
136 * notice that the sending of the file completes asynchronously,
137 * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
138 * it's done
139 */
140
141 break;
142
143 case LWS_CALLBACK_HTTP_FILE_COMPLETION:
144// lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n");
145 /* kill the connection after we sent one file */
146 return -1;
147
148 case LWS_CALLBACK_HTTP_WRITEABLE:
149 /*
150 * we can send more of whatever it is we were sending
151 */
152
153 do {
154 n = read(pss->fd, buffer, sizeof buffer);
155 /* problem reading, close conn */
156 if (n < 0)
157 goto bail;
158 /* sent it all, close conn */
159 if (n == 0)
160 goto bail;
161 /*
162 * because it's HTTP and not websocket, don't need to take
163 * care about pre and postamble
164 */
165 m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP);
166 if (m < 0)
167 /* write failed, close conn */
168 goto bail;
169 if (m != n)
170 /* partial write, adjust */
171 lseek(pss->fd, m - n, SEEK_CUR);
172
173 } while (!lws_send_pipe_choked(wsi));
174 libwebsocket_callback_on_writable(context, wsi);
175 break;
176
177bail:
178 close(pss->fd);
179 return -1;
180
Tomas Cejka11d5d062014-07-15 13:13:52 +0200181 case LWS_CALLBACK_LOCK_POLL:
182 /*
183 * lock mutex to protect pollfd state
184 * called before any other POLL related callback
185 */
186 break;
187
188 case LWS_CALLBACK_UNLOCK_POLL:
189 /*
190 * unlock mutex to protect pollfd state when
191 * called after any other POLL related callback
192 */
193 break;
194
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100195 /*
196 * callback for confirming to continue with client IP appear in
197 * protocol 0 callback since no websocket protocol has been agreed
198 * yet. You can just ignore this if you won't filter on client IP
199 * since the default uhandled callback return is 0 meaning let the
200 * connection continue.
201 */
202
203 case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
204 libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name,
205 sizeof(client_name), client_ip, sizeof(client_ip));
206
Tomas Cejkaba21b382013-04-13 02:37:32 +0200207 //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100208 /* if we returned non-zero from here, we kill the connection */
209 break;
210
211 /*
212 * callbacks for managing the external poll() array appear in
213 * protocol 0 callback
214 */
215
216 case LWS_CALLBACK_ADD_POLL_FD:
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100217 if (count_pollfds >= max_poll_elements) {
218 lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
219 return 1;
220 }
221
Tomas Cejka11d5d062014-07-15 13:13:52 +0200222 fd_lookup[pa->fd] = count_pollfds;
223 pollfds[count_pollfds].fd = pa->fd;
224 pollfds[count_pollfds].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100225 pollfds[count_pollfds++].revents = 0;
226 break;
227
228 case LWS_CALLBACK_DEL_POLL_FD:
229 if (!--count_pollfds)
230 break;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200231 m = fd_lookup[pa->fd];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100232 /* have the last guy take up the vacant slot */
233 pollfds[m] = pollfds[count_pollfds];
234 fd_lookup[pollfds[count_pollfds].fd] = m;
235 break;
236
Tomas Cejka11d5d062014-07-15 13:13:52 +0200237 case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
238 pollfds[fd_lookup[pa->fd]].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100239 break;
240
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100241
242 default:
243 break;
244 }
245
246 return 0;
247}
248
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100249
250/* dumb_increment protocol */
251
252/*
253 * one of these is auto-created for each connection and a pointer to the
254 * appropriate instance is passed to the callback in the user parameter
255 *
256 * for this example protocol we use it to individualize the count for each
257 * connection.
258 */
259
Tomas Cejkaba21b382013-04-13 02:37:32 +0200260struct per_session_data__notif_client {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100261 int number;
Michal Vaskoc3146782015-11-04 14:46:41 +0100262 char *session_id;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200263 struct nc_session *session;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100264};
265
Michal Vasko3d8681d2015-11-05 10:23:41 +0100266struct session_with_mutex *get_ncsession_from_sid(const char *session_id)
Tomas Cejkaba21b382013-04-13 02:37:32 +0200267{
268 struct session_with_mutex *locked_session = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100269 if (session_id == NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200270 return (NULL);
271 }
Michal Vaskoc3146782015-11-04 14:46:41 +0100272 for (locked_session = netconf_sessions_list;
Michal Vaskof35ea502016-02-24 10:44:54 +0100273 locked_session && (nc_session_get_id(locked_session->session) == (unsigned)atoi(session_id));
Michal Vaskoc3146782015-11-04 14:46:41 +0100274 locked_session = locked_session->next);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200275 return locked_session;
276}
277
278/* rpc parameter is freed after the function call */
Michal Vaskof35ea502016-02-24 10:44:54 +0100279static int send_recv_process(struct nc_session *session, const char* UNUSED(operation), struct nc_rpc* rpc)
Tomas Cejkaba21b382013-04-13 02:37:32 +0200280{
Michal Vaskof35ea502016-02-24 10:44:54 +0100281 struct nc_reply *reply = NULL;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200282 char *data = NULL;
283 int ret = EXIT_SUCCESS;
284
285 /* send the request and get the reply */
Michal Vaskof35ea502016-02-24 10:44:54 +0100286 switch (netconf_send_recv_timed(session, rpc, 50000, 0, &reply)) {
287 case NC_MSG_ERROR:
288 if (nc_session_get_status(session) != NC_STATUS_RUNNING) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200289 ERROR("notifications: receiving rpc-reply failed.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200290 //cmd_disconnect(NULL);
291 ret = EXIT_FAILURE;
292 break;
293 }
Tomas Cejkacf44e522015-04-24 17:29:21 +0200294 ERROR("notifications: Unknown error occurred.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200295 ret = EXIT_FAILURE;
296 break;
297 case NC_MSG_NONE:
298 /* error occurred, but processed by callback */
299 break;
300 case NC_MSG_REPLY:
Michal Vaskof35ea502016-02-24 10:44:54 +0100301 switch (reply->type) {
302 case NC_RPL_OK:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200303 break;
Michal Vaskof35ea502016-02-24 10:44:54 +0100304 case NC_RPL_DATA:
305 lyd_print_mem(&data, ((struct nc_reply_data *)reply)->data, LYD_JSON, 0);
306 DEBUG("notifications: recv: %s.", data);
307 free(data);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200308 break;
Michal Vaskof35ea502016-02-24 10:44:54 +0100309 case NC_RPL_ERROR:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200310 /* 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 */
Michal Vaskof35ea502016-02-24 10:44:54 +0100336static void notification_fileprint(struct nc_session *session, const struct nc_notif *notif)
Tomas Cejkaba21b382013-04-13 02:37:32 +0200337{
Michal Vaskof35ea502016-02-24 10:44:54 +0100338 time_t eventtime;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200339 struct session_with_mutex *target_session = NULL;
340 notification_t *ntf = NULL;
Michal Vaskoc3146782015-11-04 14:46:41 +0100341 const char *session_id = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +0100342 char *content;
343 (void)session;
344
345 eventtime = nc_datetime2time(notif->datetime);
346 lyd_print_mem(&content, notif->tree, LYD_JSON, 0);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200347
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100348 DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content);
Tomas Cejka15c56302013-05-30 01:11:30 +0200349
Michal Vaskoc3146782015-11-04 14:46:41 +0100350 session_id = pthread_getspecific(thread_key);
351 DEBUG("notification: fileprint getspecific (%s)", session_id);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200352 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200353 ERROR("notifications: Error while locking rwlock");
Michal Vaskof35ea502016-02-24 10:44:54 +0100354 free(content);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200355 return;
356 }
Michal Vaskoc3146782015-11-04 14:46:41 +0100357 DEBUG("Get session with mutex from key %s.", session_id);
Michal Vasko3d8681d2015-11-05 10:23:41 +0100358 target_session = get_ncsession_from_sid(session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200359 if (target_session == NULL) {
Michal Vaskoc3146782015-11-04 14:46:41 +0100360 ERROR("notifications: no session found last_session_key (%s)", session_id);
Michal Vaskof35ea502016-02-24 10:44:54 +0100361 free(content);
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200362 goto unlock_glob;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200363 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200364 if (pthread_mutex_lock(&target_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200365 ERROR("notifications: Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200366 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200367
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100368 DEBUG("notification: ready to push to notifications queue");
Michal Vaskoc3146782015-11-04 14:46:41 +0100369 if (target_session->notif_count < NOTIFICATION_QUEUE_SIZE) {
370 ++target_session->notif_count;
371 target_session->notifications = realloc(target_session->notifications,
372 target_session->notif_count * sizeof *target_session->notifications);
373 if (target_session->notifications) {
374 ntf = target_session->notifications + target_session->notif_count - 1;
375 }
376 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200377 if (ntf == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200378 ERROR("notifications: Failed to allocate element ");
Michal Vaskof35ea502016-02-24 10:44:54 +0100379 free(content);
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200380 goto unlock_all;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200381 }
Tomas Cejka73286932013-05-27 22:54:35 +0200382 ntf->eventtime = eventtime;
Michal Vaskof35ea502016-02-24 10:44:54 +0100383 ntf->content = content;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200384
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100385 DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200386
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200387unlock_all:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200388 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200389 ERROR("notifications: Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200390 }
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200391unlock_glob:
392 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200393 ERROR("notifications: Error while locking rwlock");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200394 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200395}
396
397/**
398 * \brief Thread for libnetconf notifications dispatch
399 * \param [in] arg - struct ntf_thread_config * with nc_session
400 */
401void* notification_thread(void* arg)
402{
403 struct ntf_thread_config *config = (struct ntf_thread_config*)arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100404 DEBUG("notifications: in thread for libnetconf notifications");
Tomas Cejka15c56302013-05-30 01:11:30 +0200405
406 /* store hash identification of netconf session for notifications printing callback */
Michal Vaskoc3146782015-11-04 14:46:41 +0100407 if (pthread_setspecific(thread_key, config->session_id) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200408 ERROR("notifications: cannot set thread-specific hash value.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200409 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200410
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100411 DEBUG("notifications: dispatching");
Michal Vaskof35ea502016-02-24 10:44:54 +0100412 nc_recv_notif_dispatch(config->session, notification_fileprint);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100413 DEBUG("notifications: ended thread for libnetconf notifications");
Michal Vaskoc3146782015-11-04 14:46:41 +0100414 if (config->session_id != NULL) {
415 free(config->session_id);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100416 }
417 if (config != NULL) {
418 free(config);
419 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200420 return (NULL);
421}
422
423
Michal Vaskoc3146782015-11-04 14:46:41 +0100424int 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 +0200425{
426 time_t start = -1;
427 time_t stop = -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200428 char *stream = NULL;
Michal Vaskof35ea502016-02-24 10:44:54 +0100429 struct nc_rpc *rpc = NULL;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200430 pthread_t thread;
431 struct ntf_thread_config *tconfig;
432 struct nc_session *session;
433
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100434 DEBUG("notif_subscribe");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200435 if (locked_session == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100436 DEBUG("notifications: no locked_session was given.");
Tomas Cejkacf44e522015-04-24 17:29:21 +0200437 /* Close notification client */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200438 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200439 }
440
Tomas Cejka47387fd2013-06-10 20:37:46 +0200441 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200442 session = locked_session->session;
443
444 start = time(NULL) + start_time;
445 stop = time(NULL) + stop_time;
Tomas Cejka57714142014-06-22 17:59:58 +0200446 start = 0;
447 stop = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100448 DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200449
450 if (session == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200451 ERROR("notifications: NETCONF session not established.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200452 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200453 }
454
Tomas Cejkaba21b382013-04-13 02:37:32 +0200455 /* check times */
456 if (start != -1 && stop != -1 && start > stop) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200457 ERROR("notifications: Subscription start time must be lower than the end time.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200458 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200459 }
460
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100461 DEBUG("Prepare to execute subscription.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200462 /* create requests */
Michal Vaskof35ea502016-02-24 10:44:54 +0100463 rpc = nc_rpc_subscribe(stream, NULL, (start_time == -1) ? NULL : nc_time2datetime(start, NULL),
464 (stop_time == 0) ? NULL : nc_time2datetime(stop, NULL), NC_PARAMTYPE_CONST);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200465 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200466 ERROR("notifications: creating an rpc request failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200467 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200468 }
469
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100470 DEBUG("Send NC subscribe.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200471 create_err_reply_p();
Tomas Cejkaba21b382013-04-13 02:37:32 +0200472 if (send_recv_process(session, "subscribe", rpc) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200473 ERROR("Subscription RPC failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200474 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200475 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200476
477 GETSPEC_ERR_REPLY
478 if (err_reply != NULL) {
479 free_err_reply();
Tomas Cejkacf44e522015-04-24 17:29:21 +0200480 ERROR("RPC-Error received and cleaned, because we can't send it anywhere.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200481 goto operation_failed;
482 }
483
Tomas Cejkaba21b382013-04-13 02:37:32 +0200484 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200485 locked_session->ntfc_subscribed = 1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200486
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100487 DEBUG("Create config for notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200488 tconfig = malloc(sizeof(struct ntf_thread_config));
Tomas Cejkacf44e522015-04-24 17:29:21 +0200489 if (tconfig == NULL) {
490 ERROR("notifications: Allocation failed.");
491 goto operation_failed;
492 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200493 tconfig->session = session;
Michal Vaskoc3146782015-11-04 14:46:41 +0100494 tconfig->session_id = strdup(session_id);
495 DEBUG("notifications: creating libnetconf notification thread (%s).", tconfig->session_id);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200496
Tomas Cejka47387fd2013-06-10 20:37:46 +0200497 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100498 DEBUG("Create notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200499 if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200500 ERROR("notifications: creating a thread for receiving notifications failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200501 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200502 }
503 pthread_detach(thread);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100504 DEBUG("Subscription finished.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200505 return 0;
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200506
507operation_failed:
508 pthread_mutex_unlock(&locked_session->lock);
509 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200510}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100511
512static int callback_notification(struct libwebsocket_context *context,
513 struct libwebsocket *wsi,
514 enum libwebsocket_callback_reasons reason,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200515 void *user, void *in, size_t len)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100516{
Michal Vaskoc3146782015-11-04 14:46:41 +0100517 int n = 0, m = 0, i;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200518 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100519 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200520 struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user;
521
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100522 switch (reason) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100523 case LWS_CALLBACK_ESTABLISHED:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100524 DEBUG("notification client connected.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100525 break;
526
527 case LWS_CALLBACK_SERVER_WRITEABLE:
Michal Vaskoc3146782015-11-04 14:46:41 +0100528 if (pss->session_id == NULL) {
Tomas Cejkaba21b382013-04-13 02:37:32 +0200529 return 0;
530 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100531 //DEBUG("Callback server writeable.");
532 //DEBUG("lock session lock.");
Tomas Cejka866f2282014-09-18 15:20:26 +0200533 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100534 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200535 return -1;
536 }
Michal Vasko3d8681d2015-11-05 10:23:41 +0100537 //DEBUG("get session_with_mutex for %s.", pss->session_id);
538 struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200539 if (ls == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100540 DEBUG("notification: session not found");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200541 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100542 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200543 return -1;
544 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200545 return -1;
546 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200547 pthread_mutex_lock(&ls->lock);
Tomas Cejka866f2282014-09-18 15:20:26 +0200548 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100549 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkaba21b382013-04-13 02:37:32 +0200550 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200551
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100552 //DEBUG("check for closed session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200553 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100554 DEBUG("unlock session key.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200555 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100556 DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200557 return -1;
Tomas Cejka654f84e2013-04-19 11:55:01 +0200558 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200559 return -1;
560 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100561 //DEBUG("lock private lock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200562 notification_t *notif = NULL;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200563
Michal Vasko1ac11282015-11-05 10:28:07 +0100564 if (ls->notif_count) {
565 DEBUG("notification: POP notifications for session");
Michal Vasko43d94a62015-11-05 10:59:32 +0100566 for (i = ls->notif_count; i; --i) {
567 notif = ls->notifications + i - 1;
Tomas Cejka73286932013-05-27 22:54:35 +0200568
Michal Vasko1ac11282015-11-05 10:28:07 +0100569 n = 0;
570 pthread_mutex_lock(&json_lock);
571 json_object *notif_json = json_object_new_object();
572 json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime));
573 json_object_object_add(notif_json, "content", json_object_new_string(notif->content));
574 pthread_mutex_unlock(&json_lock);
Tomas Cejka15c56302013-05-30 01:11:30 +0200575
Michal Vasko1ac11282015-11-05 10:28:07 +0100576 const char *msgtext = json_object_to_json_string(notif_json);
Tomas Cejka15c56302013-05-30 01:11:30 +0200577
Michal Vasko1ac11282015-11-05 10:28:07 +0100578 //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t);
579 n = sprintf((char *)p, "%s", msgtext);
580 DEBUG("ws send %dB in %lu", n, sizeof(buf));
581 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
582 if (lws_send_pipe_choked(wsi)) {
583 libwebsocket_callback_on_writable(context, wsi);
584 break;
585 }
Michal Vaskoc3146782015-11-04 14:46:41 +0100586
Michal Vasko1ac11282015-11-05 10:28:07 +0100587 pthread_mutex_lock(&json_lock);
588 json_object_put(notif_json);
589 pthread_mutex_unlock(&json_lock);
590 free(notif->content);
Michal Vaskoc3146782015-11-04 14:46:41 +0100591 }
Michal Vasko43d94a62015-11-05 10:59:32 +0100592 ls->notif_count = 0;
593 free(ls->notifications);
594 ls->notifications = NULL;
595
Michal Vasko1ac11282015-11-05 10:28:07 +0100596 DEBUG("notification: POP notifications done");
Michal Vaskoc3146782015-11-04 14:46:41 +0100597 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200598
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100599 //DEBUG("unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200600 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100601 DEBUG("notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200602 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100603 //DEBUG("unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200604
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100605 if (m < n) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100606 DEBUG("ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200607
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100608 return -1;
609 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100610 break;
611
612 case LWS_CALLBACK_RECEIVE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100613 DEBUG("Callback receive.");
614 DEBUG("received: (%s)", (char *)in);
Michal Vaskoc3146782015-11-04 14:46:41 +0100615 if (pss->session_id == NULL) {
Michal Vasko2bff5fb2015-11-05 10:23:02 +0100616 char *sid_end;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200617 int start = -1;
618 time_t stop = time(NULL) + 30;
619
Michal Vasko2bff5fb2015-11-05 10:23:02 +0100620 sid_end = strchr(in, ' ');
621 pss->session_id = strndup(in, sid_end - (char *)in);
622
623 ++sid_end;
624 sscanf(sid_end, "%d %d", (int *) &start, (int *) &stop);
625 DEBUG("notification: SID (%s) from (%s) (%i,%i)", pss->session_id, (char *) in, (int) start, (int) stop);
Tomas Cejka15c56302013-05-30 01:11:30 +0200626
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100627 DEBUG("lock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200628 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100629 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200630 return -1;
631 }
Michal Vasko3d8681d2015-11-05 10:23:41 +0100632 DEBUG("get session with ID (%s)", pss->session_id);
633 struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200634 if (ls == NULL) {
Michal Vasko3d8681d2015-11-05 10:23:41 +0100635 DEBUG("notification: session_id not found (%s)", pss->session_id);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100636 DEBUG("unlock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200637 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100638 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200639 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100640 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200641 return -1;
642 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100643 DEBUG("lock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200644 pthread_mutex_lock(&ls->lock);
645
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100646 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200647 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100648 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200649 }
650
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100651 DEBUG("Found session to subscribe notif.");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200652 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100653 DEBUG("session already closed - handle no notification");
654 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200655 pthread_mutex_unlock(&ls->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100656 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200657 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200658 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200659 if (ls->ntfc_subscribed != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100660 DEBUG("notification: already subscribed");
661 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200662 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200663 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200664 return 0;
665 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100666 DEBUG("notification: prepare to subscribe stream");
667 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200668 pthread_mutex_unlock(&ls->lock);
669
670 /* notif_subscribe locks on its own */
Michal Vaskoc3146782015-11-04 14:46:41 +0100671 return notif_subscribe(ls, pss->session_id, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200672 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100673 if (len < 6)
674 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100675 break;
676 /*
677 * this just demonstrates how to use the protocol filter. If you won't
678 * study and reject connections based on header content, you don't need
679 * to handle this callback
680 */
681
682 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
683 //dump_handshake_info(wsi);
684 /* you could return non-zero here and kill the connection */
685 break;
Tomas Cejka866f2282014-09-18 15:20:26 +0200686 //gives segfault :-(
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100687 //case LWS_CALLBACK_CLOSED:
688 // if (pss->session_key != NULL) {
689 // free(pss->session_key);
690 // }
691 // if (pss != NULL) {
692 // free(pss);
693 // }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100694
695 default:
696 break;
697 }
698
699 return 0;
700}
701
702/* list of supported protocols and callbacks */
703
704static struct libwebsocket_protocols protocols[] = {
705 /* first protocol must always be HTTP handler */
706
707 {
708 "http-only", /* name */
709 callback_http, /* callback */
710 sizeof (struct per_session_data__http), /* per_session_data_size */
711 0, /* max frame size / rx buffer */
Michal Vaskoc3146782015-11-04 14:46:41 +0100712 0,
713 NULL,
714 NULL,
715 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100716 },
717 {
718 "notification-protocol",
719 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200720 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200721 4000,
Michal Vaskoc3146782015-11-04 14:46:41 +0100722 0,
723 NULL,
724 NULL,
725 0
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100726 },
Michal Vaskoc3146782015-11-04 14:46:41 +0100727 { NULL, NULL, 0, 0, 0, NULL, NULL, 0 } /* terminator */
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100728};
729
730
Tomas Cejkaba21b382013-04-13 02:37:32 +0200731/**
732 * initialization of notification module
733 */
Michal Vaskoc3146782015-11-04 14:46:41 +0100734int notification_init(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100735{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200736 //char cert_path[1024];
737 //char key_path[1024];
738 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100739 struct lws_context_creation_info info;
740 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200741 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100742 const char *iface = NULL;
743 int debug_level = 7;
744
745 memset(&info, 0, sizeof info);
746 info.port = NOTIFICATION_SERVER_PORT;
747
748 /* tell the library what debug level to emit and to send it to syslog */
749 lws_set_log_level(debug_level, lwsl_emit_syslog);
750
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100751 DEBUG("Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200752 //lwsl_notice("libwebsockets test server - "
753 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
754 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100755 max_poll_elements = getdtablesize();
756 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
757 fd_lookup = malloc(max_poll_elements * sizeof (int));
758 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200759 ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100760 return -1;
761 }
762
763 info.iface = iface;
764 info.protocols = protocols;
765
766 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
767 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
768
769 //info.ssl_cert_filepath = cert_path;
770 //info.ssl_private_key_filepath = key_path;
771
772 info.gid = -1;
773 info.uid = -1;
774 info.options = opts;
775
776 /* create server */
777 context = libwebsocket_create_context(&info);
778 if (context == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100779 DEBUG("libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100780 return -1;
781 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200782
Tomas Cejka15c56302013-05-30 01:11:30 +0200783 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200784 ERROR("notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200785 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200786 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100787}
788
Michal Vaskoc3146782015-11-04 14:46:41 +0100789void notification_close(void)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100790{
Tomas Cejka98ed6cc2015-04-27 23:35:18 +0200791 if (context) {
792 libwebsocket_context_destroy(context);
793 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100794 free(pollfds);
795 free(fd_lookup);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100796
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100797 DEBUG("libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100798}
799
Tomas Cejkaba21b382013-04-13 02:37:32 +0200800
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100801/**
802 * \brief send notification if any
803 * \return < 0 on error
804 */
805int notification_handle()
806{
807 static struct timeval tv;
808 static unsigned int olds = 0;
809 int n = 0;
810
811 gettimeofday(&tv, NULL);
812
813 /*
814 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
815 * live websocket connection using the DUMB_INCREMENT protocol,
816 * as soon as it can take more packets (usually immediately)
817 */
818
819 if (((unsigned int)tv.tv_sec - olds) > 0) {
820 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
821 olds = tv.tv_sec;
822 }
823
824
825 /*
826 * this represents an existing server's single poll action
827 * which also includes libwebsocket sockets
828 */
829
830 n = poll(pollfds, count_pollfds, 50);
831 if (n < 0)
832 return n;
833
834
835 if (n) {
836 for (n = 0; n < count_pollfds; n++) {
837 if (pollfds[n].revents) {
838 /*
839 * returns immediately if the fd does not
840 * match anything under libwebsockets
841 * control
842 */
843 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
844 return 1;
845 }
846 }
847 }
848 }
849 return 0;
850}
851
852#endif
853
854
855#ifndef WITH_NOTIFICATIONS
856#ifdef TEST_NOTIFICATION_SERVER
857int main(int argc, char **argv)
858{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100859 if (notification_init(NULL, NULL) == -1) {
860 fprintf(stderr, "Error during initialization\n");
861 return 1;
862 }
863 while (!force_exit) {
864 notification_handle();
865 }
866 notification_close();
867}
868#endif
869#endif