blob: bbf373e37aec7e2bad949acb3b3e9643482f0e44 [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>
32#include <libnetconf.h>
33#include <libwebsockets.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010034#include "notification_module.h"
Tomas Cejkaba21b382013-04-13 02:37:32 +020035#include "mod_netconf.h"
Tomas Cejkad340dbf2013-03-24 20:36:57 +010036
37#ifndef TEST_NOTIFICATION_SERVER
38#include <httpd.h>
39#include <http_log.h>
Tomas Cejkaba21b382013-04-13 02:37:32 +020040#include <apr_hash.h>
41#include <apr_tables.h>
42
43#else
44static int force_exit = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010045#endif
46
47#if defined(TEST_NOTIFICATION_SERVER) || defined(WITH_NOTIFICATIONS)
Tomas Cejkad340dbf2013-03-24 20:36:57 +010048static int max_poll_elements;
49
50static struct pollfd *pollfds;
51static int *fd_lookup;
52static int count_pollfds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010053static struct libwebsocket_context *context = NULL;
54static server_rec *http_server = NULL;
55
Tomas Cejkaba21b382013-04-13 02:37:32 +020056struct ntf_thread_config {
57 struct nc_session *session;
58 char *session_hash;
59};
60
Tomas Cejka47387fd2013-06-10 20:37:46 +020061extern apr_hash_t *netconf_sessions_list;
Tomas Cejkaba21b382013-04-13 02:37:32 +020062static pthread_key_t thread_key;
63
Tomas Cejkad340dbf2013-03-24 20:36:57 +010064/*
65 * This demo server shows how to use libwebsockets for one or more
66 * websocket protocols in the same server
67 *
68 * It defines the following websocket protocols:
69 *
70 * dumb-increment-protocol: once the socket is opened, an incrementing
71 * ascii string is sent down it every 50ms.
72 * If you send "reset\n" on the websocket, then
73 * the incrementing number is reset to 0.
74 *
75 * lws-mirror-protocol: copies any received packet to every connection also
76 * using this protocol, including the sender
77 */
78
79enum demo_protocols {
80 /* always first */
81 PROTOCOL_HTTP = 0,
82
83 PROTOCOL_NOTIFICATION,
84
85 /* always last */
86 DEMO_PROTOCOL_COUNT
87};
88
89
90#define LOCAL_RESOURCE_PATH "."
91char *resource_path = LOCAL_RESOURCE_PATH;
92
93/*
94 * We take a strict whitelist approach to stop ../ attacks
95 */
96
97struct serveable {
98 const char *urlpath;
99 const char *mimetype;
Tomas Cejka15c56302013-05-30 01:11:30 +0200100};
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100101
102static const struct serveable whitelist[] = {
103 { "/favicon.ico", "image/x-icon" },
104 { "/libwebsockets.org-logo.png", "image/png" },
105
106 /* last one is the default served if no match */
107 { "/test.html", "text/html" },
108};
109
110struct per_session_data__http {
111 int fd;
112};
113
114/* this protocol server (always the first one) just knows how to do HTTP */
115
116static int callback_http(struct libwebsocket_context *context,
117 struct libwebsocket *wsi,
118 enum libwebsocket_callback_reasons reason, void *user,
119 void *in, size_t len)
120{
121 char client_name[128];
122 char client_ip[128];
123 char buf[256];
124 int n, m;
125 unsigned char *p;
126 static unsigned char buffer[4096];
127 struct stat stat_buf;
128 struct per_session_data__http *pss = (struct per_session_data__http *)user;
129 int fd = (int)(long)in;
130
131 switch (reason) {
132 case LWS_CALLBACK_HTTP:
133
134 /* check for the "send a big file by hand" example case */
135
136 if (!strcmp((const char *)in, "/leaf.jpg")) {
137 char leaf_path[1024];
138 snprintf(leaf_path, sizeof(leaf_path), "%s/leaf.jpg", resource_path);
139
140 /* well, let's demonstrate how to send the hard way */
141
142 p = buffer;
143
144 pss->fd = open(leaf_path, O_RDONLY);
145
146 if (pss->fd < 0)
147 return -1;
148
149 fstat(pss->fd, &stat_buf);
150
151 /*
152 * we will send a big jpeg file, but it could be
153 * anything. Set the Content-Type: appropriately
154 * so the browser knows what to do with it.
155 */
156
157 p += sprintf((char *)p,
158 "HTTP/1.0 200 OK\x0d\x0a"
159 "Server: libwebsockets\x0d\x0a"
160 "Content-Type: image/jpeg\x0d\x0a"
161 "Content-Length: %u\x0d\x0a\x0d\x0a",
162 (unsigned int)stat_buf.st_size);
163
164 /*
165 * send the http headers...
166 * this won't block since it's the first payload sent
167 * on the connection since it was established
168 * (too small for partial)
169 */
170
171 n = libwebsocket_write(wsi, buffer,
172 p - buffer, LWS_WRITE_HTTP);
173
174 if (n < 0) {
175 close(pss->fd);
176 return -1;
177 }
178 /*
179 * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
180 */
181 libwebsocket_callback_on_writable(context, wsi);
182 break;
183 }
184
185 /* if not, send a file the easy way */
186
187 for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
188 if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
189 break;
190
191 sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath);
192
193 if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype))
194 return -1; /* through completion or error, close the socket */
195
196 /*
197 * notice that the sending of the file completes asynchronously,
198 * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
199 * it's done
200 */
201
202 break;
203
204 case LWS_CALLBACK_HTTP_FILE_COMPLETION:
205// lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n");
206 /* kill the connection after we sent one file */
207 return -1;
208
209 case LWS_CALLBACK_HTTP_WRITEABLE:
210 /*
211 * we can send more of whatever it is we were sending
212 */
213
214 do {
215 n = read(pss->fd, buffer, sizeof buffer);
216 /* problem reading, close conn */
217 if (n < 0)
218 goto bail;
219 /* sent it all, close conn */
220 if (n == 0)
221 goto bail;
222 /*
223 * because it's HTTP and not websocket, don't need to take
224 * care about pre and postamble
225 */
226 m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP);
227 if (m < 0)
228 /* write failed, close conn */
229 goto bail;
230 if (m != n)
231 /* partial write, adjust */
232 lseek(pss->fd, m - n, SEEK_CUR);
233
234 } while (!lws_send_pipe_choked(wsi));
235 libwebsocket_callback_on_writable(context, wsi);
236 break;
237
238bail:
239 close(pss->fd);
240 return -1;
241
242 /*
243 * callback for confirming to continue with client IP appear in
244 * protocol 0 callback since no websocket protocol has been agreed
245 * yet. You can just ignore this if you won't filter on client IP
246 * since the default uhandled callback return is 0 meaning let the
247 * connection continue.
248 */
249
250 case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
251 libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name,
252 sizeof(client_name), client_ip, sizeof(client_ip));
253
Tomas Cejkaba21b382013-04-13 02:37:32 +0200254 //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100255 /* if we returned non-zero from here, we kill the connection */
256 break;
257
258 /*
259 * callbacks for managing the external poll() array appear in
260 * protocol 0 callback
261 */
262
263 case LWS_CALLBACK_ADD_POLL_FD:
264
265 if (count_pollfds >= max_poll_elements) {
266 lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
267 return 1;
268 }
269
270 fd_lookup[fd] = count_pollfds;
271 pollfds[count_pollfds].fd = fd;
272 pollfds[count_pollfds].events = (int)(long)len;
273 pollfds[count_pollfds++].revents = 0;
274 break;
275
276 case LWS_CALLBACK_DEL_POLL_FD:
277 if (!--count_pollfds)
278 break;
279 m = fd_lookup[fd];
280 /* have the last guy take up the vacant slot */
281 pollfds[m] = pollfds[count_pollfds];
282 fd_lookup[pollfds[count_pollfds].fd] = m;
283 break;
284
285 case LWS_CALLBACK_SET_MODE_POLL_FD:
286 pollfds[fd_lookup[fd]].events |= (int)(long)len;
287 break;
288
289 case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
290 pollfds[fd_lookup[fd]].events &= ~(int)(long)len;
291 break;
292
293 default:
294 break;
295 }
296
297 return 0;
298}
299
Tomas Cejkaba21b382013-04-13 02:37:32 +0200300/**
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100301 * this is just an example of parsing handshake headers, you don't need this
302 * in your code unless you will filter allowing connections by the header
303 * content
304 */
Tomas Cejkaba21b382013-04-13 02:37:32 +0200305//static void dump_handshake_info(struct libwebsocket *wsi)
306//{
307// int n;
308// static const char *token_names[WSI_TOKEN_COUNT] = {
309// /*[WSI_TOKEN_GET_URI] =*/ "GET URI",
310// /*[WSI_TOKEN_HOST] =*/ "Host",
311// /*[WSI_TOKEN_CONNECTION] =*/ "Connection",
312// /*[WSI_TOKEN_KEY1] =*/ "key 1",
313// /*[WSI_TOKEN_KEY2] =*/ "key 2",
314// /*[WSI_TOKEN_PROTOCOL] =*/ "Protocol",
315// /*[WSI_TOKEN_UPGRADE] =*/ "Upgrade",
316// /*[WSI_TOKEN_ORIGIN] =*/ "Origin",
317// /*[WSI_TOKEN_DRAFT] =*/ "Draft",
318// /*[WSI_TOKEN_CHALLENGE] =*/ "Challenge",
319//
320// /* new for 04 */
321// /*[WSI_TOKEN_KEY] =*/ "Key",
322// /*[WSI_TOKEN_VERSION] =*/ "Version",
323// /*[WSI_TOKEN_SWORIGIN] =*/ "Sworigin",
324//
325// /* new for 05 */
326// /*[WSI_TOKEN_EXTENSIONS] =*/ "Extensions",
327//
328// /* client receives these */
329// /*[WSI_TOKEN_ACCEPT] =*/ "Accept",
330// /*[WSI_TOKEN_NONCE] =*/ "Nonce",
331// /*[WSI_TOKEN_HTTP] =*/ "Http",
332// /*[WSI_TOKEN_MUXURL] =*/ "MuxURL",
333// };
334// char buf[256];
335//
336// for (n = 0; n < WSI_TOKEN_COUNT; n++) {
337// if (!lws_hdr_total_length(wsi, n))
338// continue;
339//
340// //lws_hdr_copy(wsi, buf, sizeof buf, n);
341//
342// //fprintf(stderr, " %s = %s\n", token_names[n], buf);
343// }
344//}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100345
346/* dumb_increment protocol */
347
348/*
349 * one of these is auto-created for each connection and a pointer to the
350 * appropriate instance is passed to the callback in the user parameter
351 *
352 * for this example protocol we use it to individualize the count for each
353 * connection.
354 */
355
Tomas Cejkaba21b382013-04-13 02:37:32 +0200356struct per_session_data__notif_client {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100357 int number;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200358 char *session_key;
359 struct nc_session *session;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100360};
361
Tomas Cejkaba21b382013-04-13 02:37:32 +0200362struct session_with_mutex *get_ncsession_from_key(const char *session_key)
363{
364 struct session_with_mutex *locked_session = NULL;
365 if (session_key == NULL) {
366 return (NULL);
367 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200368 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200369 return locked_session;
370}
371
372/* rpc parameter is freed after the function call */
373static int send_recv_process(struct nc_session *session, const char* operation, nc_rpc* rpc)
374{
375 nc_reply *reply = NULL;
376 char *data = NULL;
377 int ret = EXIT_SUCCESS;
378
379 /* send the request and get the reply */
380 switch (nc_session_send_recv(session, rpc, &reply)) {
381 case NC_MSG_UNKNOWN:
382 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
383 #ifndef TEST_NOTIFICATION_SERVER
384 if (http_server != NULL) {
385 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: receiving rpc-reply failed.");
386 }
387 #endif
388 //cmd_disconnect(NULL);
389 ret = EXIT_FAILURE;
390 break;
391 }
392 #ifndef TEST_NOTIFICATION_SERVER
393 if (http_server != NULL) {
394 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Unknown error occurred.");
395 }
396 #endif
397 ret = EXIT_FAILURE;
398 break;
399 case NC_MSG_NONE:
400 /* error occurred, but processed by callback */
401 break;
402 case NC_MSG_REPLY:
403 switch (nc_reply_get_type(reply)) {
404 case NC_REPLY_OK:
405 break;
406 case NC_REPLY_DATA:
407 #ifndef TEST_NOTIFICATION_SERVER
408 if (http_server != NULL) {
409 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notifications: recv: %s.", data = nc_reply_get_data (reply));
410 free(data);
411 }
412 #endif
413 break;
414 case NC_REPLY_ERROR:
415 /* wtf, you shouldn't be here !?!? */
416 #ifndef TEST_NOTIFICATION_SERVER
417 if (http_server != NULL) {
418 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: operation failed, but rpc-error was not processed.");
419 }
420 #endif
421 ret = EXIT_FAILURE;
422 break;
423 default:
424 #ifndef TEST_NOTIFICATION_SERVER
425 if (http_server != NULL) {
426 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: unexpected operation result.");
427 }
428 #endif
429 ret = EXIT_FAILURE;
430 break;
431 }
432 break;
433 default:
434 #ifndef TEST_NOTIFICATION_SERVER
435 if (http_server != NULL) {
436 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Unknown error occurred.");
437 }
438 #endif
439 ret = EXIT_FAILURE;
440 break;
441 }
442 nc_rpc_free(rpc);
443 nc_reply_free(reply);
444
445 return (ret);
446}
447
448/**
449 * \brief Callback to store incoming notification
450 * \param [in] eventtime - time when notification occured
451 * \param [in] content - content of notification
452 */
453static void notification_fileprint (time_t eventtime, const char* content)
454{
455 char t[128];
456 struct session_with_mutex *target_session = NULL;
457 notification_t *ntf = NULL;
458 char *session_hash = NULL;
459
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200460 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Accepted notif: %lu %s\n", (unsigned long int) eventtime, content);
Tomas Cejka15c56302013-05-30 01:11:30 +0200461
Tomas Cejkaba21b382013-04-13 02:37:32 +0200462 session_hash = pthread_getspecific(thread_key);
463 if (http_server != NULL) {
464 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: fileprint getspecific (%s)", session_hash);
465 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200466 if (pthread_rwlock_wrlock(&session_lock) != 0) {
467 #ifndef TEST_NOTIFICATION_SERVER
Tomas Cejka47387fd2013-06-10 20:37:46 +0200468 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200469 #endif
470 return;
471 }
472 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Get session with mutex from key %s.", session_hash);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200473 target_session = get_ncsession_from_key(session_hash);
474 if (target_session == NULL) {
475 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "no session found last_session_key (%s)", session_hash);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200476 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200477 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200478 return;
479 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200480 return;
481 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200482 if (pthread_mutex_lock(&target_session->lock) != 0) {
483 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock");
484 }
485 if (pthread_rwlock_unlock(&session_lock) != 0) {
486 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock");
487 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200488
489 t[0] = 0;
490 strftime(t, sizeof(t), "%c", localtime(&eventtime));
491
Tomas Cejkaba21b382013-04-13 02:37:32 +0200492 if (target_session->notifications == NULL) {
493 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "target_session->notifications is NULL");
494 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200495 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkaba21b382013-04-13 02:37:32 +0200496 return;
497 }
498 return;
499 }
500 if (http_server != NULL) {
501 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: ready to push to notifications queue");
502 }
503 ntf = (notification_t *) apr_array_push(target_session->notifications);
504 if (ntf == NULL) {
505 ap_log_error (APLOG_MARK, APLOG_ERR, 0, http_server, "Failed to allocate element ");
506 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200507 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200508 return;
509 }
510 return;
511 }
Tomas Cejka73286932013-05-27 22:54:35 +0200512 ntf->eventtime = eventtime;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200513 ntf->content = strdup(content);
514
Tomas Cejka47387fd2013-06-10 20:37:46 +0200515 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notifikace");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200516
517 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200518 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200519 }
520}
521
522/**
523 * \brief Thread for libnetconf notifications dispatch
524 * \param [in] arg - struct ntf_thread_config * with nc_session
525 */
526void* notification_thread(void* arg)
527{
528 struct ntf_thread_config *config = (struct ntf_thread_config*)arg;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200529 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "notifications: in thread for libnetconf notifications");
Tomas Cejka15c56302013-05-30 01:11:30 +0200530
531 /* store hash identification of netconf session for notifications printing callback */
532 if (pthread_setspecific(thread_key, config->session_hash) != 0) {
533 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: cannot set thread-specific hash value.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200534 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200535
Tomas Cejkaba21b382013-04-13 02:37:32 +0200536 if (http_server != NULL) {
537 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notifications: dispatching");
538 }
539 ncntf_dispatch_receive(config->session, notification_fileprint);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200540 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "notifications: ended thread for libnetconf notifications");
541 //free(config);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200542 return (NULL);
543}
544
545
546int notif_subscribe(struct session_with_mutex *locked_session, const char *session_hash, time_t start_time, time_t stop_time)
547{
548 time_t start = -1;
549 time_t stop = -1;
550 struct nc_filter *filter = NULL;
551 char *stream = NULL;
552 nc_rpc *rpc = NULL;
553 pthread_t thread;
554 struct ntf_thread_config *tconfig;
555 struct nc_session *session;
556
Tomas Cejka47387fd2013-06-10 20:37:46 +0200557 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notif_subscribe");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200558 if (locked_session == NULL) {
559 if (http_server != NULL) {
560 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: no locked_session was given.");
561 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200562 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
563 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200564 }
565
Tomas Cejka47387fd2013-06-10 20:37:46 +0200566 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200567 session = locked_session->session;
568
569 start = time(NULL) + start_time;
570 stop = time(NULL) + stop_time;
571 if (http_server != NULL) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: history: %u %u", (unsigned int) start, (unsigned int) stop);
572 }
573
574 if (session == NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200575 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: NETCONF session not established.");
576 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200577 }
578
579 /* check if notifications are allowed on this session */
580 if (nc_session_notif_allowed(session) == 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200581 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Notification subscription is not allowed on this session.");
582 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200583 }
584 /* check times */
585 if (start != -1 && stop != -1 && start > stop) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200586 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Subscription start time must be lower than the end time.");
587 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200588 }
589
Tomas Cejka47387fd2013-06-10 20:37:46 +0200590 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Prepare to execute subscription.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200591 /* create requests */
592 rpc = nc_rpc_subscribe(stream, filter, (start_time == 0)?NULL:&start, (stop_time == 0)?NULL:&stop);
593 nc_filter_free(filter);
594 if (rpc == NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200595 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating an rpc request failed.");
596 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200597 }
598
Tomas Cejka47387fd2013-06-10 20:37:46 +0200599 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Send NC subscribe.");
600 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaba21b382013-04-13 02:37:32 +0200601 if (send_recv_process(session, "subscribe", rpc) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200602 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Subscription RPC failed.");
603 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200604 }
605 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200606 locked_session->ntfc_subscribed = 1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200607
Tomas Cejka47387fd2013-06-10 20:37:46 +0200608 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Create config for notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200609 tconfig = malloc(sizeof(struct ntf_thread_config));
610 tconfig->session = session;
611 tconfig->session_hash = strdup(session_hash);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200612 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating libnetconf notification thread (%s).", tconfig->session_hash);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200613
Tomas Cejka47387fd2013-06-10 20:37:46 +0200614 pthread_mutex_unlock(&locked_session->lock);
615 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Create notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200616 if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200617 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating a thread for receiving notifications failed");
618 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200619 }
620 pthread_detach(thread);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200621 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Subscription finished.");
622 return 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200623}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100624
625static int callback_notification(struct libwebsocket_context *context,
626 struct libwebsocket *wsi,
627 enum libwebsocket_callback_reasons reason,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200628 void *user, void *in, size_t len)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100629{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200630 int n = 0;
631 int m = 0;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200632 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100633 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200634 struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user;
635
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100636 switch (reason) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100637 case LWS_CALLBACK_ESTABLISHED:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200638 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification client connected.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100639 break;
640
641 case LWS_CALLBACK_SERVER_WRITEABLE:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200642 if (pss->session_key == NULL) {
643 return 0;
644 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200645 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Callback server writeable.");
646 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock session lock.");
647 if (pthread_rwlock_wrlock (&session_lock) != 0) {
648 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
649 return -1;
650 }
651 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "get session_with_mutex for %s.", pss->session_key);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200652 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
653 if (ls == NULL) {
654 if (http_server != NULL) {
655 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notification: session not found");
656 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200657 if (pthread_rwlock_unlock (&session_lock) != 0) {
658 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
659 return -1;
660 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200661 return -1;
662 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200663 pthread_mutex_lock(&ls->lock);
664 if (pthread_rwlock_unlock (&session_lock) != 0) {
665 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkaba21b382013-04-13 02:37:32 +0200666 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200667
Tomas Cejka47387fd2013-06-10 20:37:46 +0200668 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "check for closed session.");
669 if (ls->closed == 1) {
670 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session key.");
671 if (pthread_rwlock_unlock (&session_lock) != 0) {
672 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking unlock: %d (%s)", errno, strerror(errno));
673 return -1;
Tomas Cejka654f84e2013-04-19 11:55:01 +0200674 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200675 return -1;
676 }
677 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock private lock.");
678 notification_t *notif = NULL;
679 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "check for uninitialized notification list.");
680 if (ls->notifications == NULL) {
681 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notification: no notifications array");
682 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock.");
683 if (pthread_mutex_unlock(&ls->lock) != 0) {
684 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: cannot unlock session");
685 }
686 return -1;
687 }
688 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "check for empty notification list.");
689 if (!apr_is_empty_array(ls->notifications)) {
690 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: POP notifications for session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200691
Tomas Cejka654f84e2013-04-19 11:55:01 +0200692 while ((notif = (notification_t *) apr_array_pop(ls->notifications)) != NULL) {
693 char t[128];
Tomas Cejka73286932013-05-27 22:54:35 +0200694 memset(&t, 0, 128);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200695 strftime(t, sizeof(t), "%c", localtime(&notif->eventtime));
696 n = 0;
Tomas Cejka73286932013-05-27 22:54:35 +0200697 json_object *notif_json = json_object_new_object();
698 json_object_object_add(notif_json, "eventtime", json_object_new_string(t));
699 json_object_object_add(notif_json, "content", json_object_new_string(notif->content));
700
Tomas Cejka00635972013-06-03 15:10:52 +0200701 const char *msgtext = json_object_to_json_string(notif_json);
Tomas Cejka15c56302013-05-30 01:11:30 +0200702
Tomas Cejka47387fd2013-06-10 20:37:46 +0200703 //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t);
Tomas Cejka15c56302013-05-30 01:11:30 +0200704 n = sprintf((char *)p, "%s", msgtext);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200705 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "ws send %dB in %lu", n, sizeof(buf));
Tomas Cejka15c56302013-05-30 01:11:30 +0200706 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200707 if (lws_send_pipe_choked(wsi)) {
708 libwebsocket_callback_on_writable(context, wsi);
709 break;
710 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200711
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200712 json_object_put(notif_json);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200713 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200714 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: POP notifications done");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200715 }
716
Tomas Cejka47387fd2013-06-10 20:37:46 +0200717 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200718 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200719 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200720 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200721 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200722
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100723 if (m < n) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200724 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200725
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100726 return -1;
727 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100728 break;
729
730 case LWS_CALLBACK_RECEIVE:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200731 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Callback receive.");
732 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "received: (%s)", (char *)in);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200733 if (pss->session_key == NULL) {
734 char session_key_buf[41];
735 int start = -1;
736 time_t stop = time(NULL) + 30;
737
738 strncpy((char *) session_key_buf, (const char *) in, 40);
739 session_key_buf[40] = '\0';
740 pss->session_key = strdup(session_key_buf);
741 sscanf(in+40, "%d %d", (int *) &start, (int *) &stop);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200742 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "notification: get key (%s) from (%s) (%i,%i)", pss->session_key, (char *) in, (int) start, (int) stop);
Tomas Cejka15c56302013-05-30 01:11:30 +0200743
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200744 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock session lock");
745 if (pthread_rwlock_rdlock (&session_lock) != 0) {
746 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
747 return -1;
748 }
749 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "get session from key (%s)", pss->session_key);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200750 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
751 if (ls == NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200752 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notification: session_key not found (%s)", pss->session_key);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200753 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
754 if (pthread_rwlock_unlock (&session_lock) != 0) {
755 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200756 }
757 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
758 return -1;
759 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200760 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock private lock");
761 pthread_mutex_lock(&ls->lock);
762
763 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
764 if (pthread_rwlock_unlock (&session_lock) != 0) {
765 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
766 }
767
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200768 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Found session to subscribe notif.");
769 if (ls->closed == 1) {
770 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "session already closed - handle no notification");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200771 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
772 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200773 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
774 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200775 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200776 if (ls->ntfc_subscribed != 0) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200777 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: already subscribed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200778 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
779 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200780 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200781 return 0;
782 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200783 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: prepare to subscribe stream");
784 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200785 pthread_mutex_unlock(&ls->lock);
786
787 /* notif_subscribe locks on its own */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200788 return notif_subscribe(ls, pss->session_key, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200789 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100790 if (len < 6)
791 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100792 break;
793 /*
794 * this just demonstrates how to use the protocol filter. If you won't
795 * study and reject connections based on header content, you don't need
796 * to handle this callback
797 */
798
799 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
800 //dump_handshake_info(wsi);
801 /* you could return non-zero here and kill the connection */
802 break;
803
804 default:
805 break;
806 }
807
808 return 0;
809}
810
811/* list of supported protocols and callbacks */
812
813static struct libwebsocket_protocols protocols[] = {
814 /* first protocol must always be HTTP handler */
815
816 {
817 "http-only", /* name */
818 callback_http, /* callback */
819 sizeof (struct per_session_data__http), /* per_session_data_size */
820 0, /* max frame size / rx buffer */
821 },
822 {
823 "notification-protocol",
824 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200825 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200826 4000,
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100827 },
828 { NULL, NULL, 0, 0 } /* terminator */
829};
830
831
Tomas Cejkaba21b382013-04-13 02:37:32 +0200832/**
833 * initialization of notification module
834 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200835int notification_init(apr_pool_t * pool, server_rec * server)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100836{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200837 //char cert_path[1024];
838 //char key_path[1024];
839 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100840 struct lws_context_creation_info info;
841 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200842 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100843 const char *iface = NULL;
844 int debug_level = 7;
845
846 memset(&info, 0, sizeof info);
847 info.port = NOTIFICATION_SERVER_PORT;
848
849 /* tell the library what debug level to emit and to send it to syslog */
850 lws_set_log_level(debug_level, lwsl_emit_syslog);
851
Tomas Cejka47387fd2013-06-10 20:37:46 +0200852 http_server = server;
853 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200854 //lwsl_notice("libwebsockets test server - "
855 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
856 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100857 max_poll_elements = getdtablesize();
858 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
859 fd_lookup = malloc(max_poll_elements * sizeof (int));
860 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejka15c56302013-05-30 01:11:30 +0200861 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100862 return -1;
863 }
864
865 info.iface = iface;
866 info.protocols = protocols;
867
868 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
869 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
870
871 //info.ssl_cert_filepath = cert_path;
872 //info.ssl_private_key_filepath = key_path;
873
874 info.gid = -1;
875 info.uid = -1;
876 info.options = opts;
877
878 /* create server */
879 context = libwebsocket_create_context(&info);
880 if (context == NULL) {
Tomas Cejka15c56302013-05-30 01:11:30 +0200881 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100882 return -1;
883 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200884
885 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notifications: init of pthread_key_create.");
886 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200887 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200888 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200889 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100890}
891
892void notification_close()
893{
894 libwebsocket_context_destroy(context);
895
Tomas Cejka15c56302013-05-30 01:11:30 +0200896 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100897}
898
Tomas Cejkaba21b382013-04-13 02:37:32 +0200899
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100900/**
901 * \brief send notification if any
902 * \return < 0 on error
903 */
904int notification_handle()
905{
906 static struct timeval tv;
907 static unsigned int olds = 0;
908 int n = 0;
909
910 gettimeofday(&tv, NULL);
911
912 /*
913 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
914 * live websocket connection using the DUMB_INCREMENT protocol,
915 * as soon as it can take more packets (usually immediately)
916 */
917
918 if (((unsigned int)tv.tv_sec - olds) > 0) {
919 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
920 olds = tv.tv_sec;
921 }
922
923
924 /*
925 * this represents an existing server's single poll action
926 * which also includes libwebsocket sockets
927 */
928
929 n = poll(pollfds, count_pollfds, 50);
930 if (n < 0)
931 return n;
932
933
934 if (n) {
935 for (n = 0; n < count_pollfds; n++) {
936 if (pollfds[n].revents) {
937 /*
938 * returns immediately if the fd does not
939 * match anything under libwebsockets
940 * control
941 */
942 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
943 return 1;
944 }
945 }
946 }
947 }
948 return 0;
949}
950
951#endif
952
953
954#ifndef WITH_NOTIFICATIONS
955#ifdef TEST_NOTIFICATION_SERVER
956int main(int argc, char **argv)
957{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100958 if (notification_init(NULL, NULL) == -1) {
959 fprintf(stderr, "Error during initialization\n");
960 return 1;
961 }
962 while (!force_exit) {
963 notification_handle();
964 }
965 notification_close();
966}
967#endif
968#endif