blob: 36013fd78d4b4c70e4d26e96fcf8da63be79c8a6 [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{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200455 struct session_with_mutex *target_session = NULL;
456 notification_t *ntf = NULL;
457 char *session_hash = NULL;
458
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200459 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 +0200460
Tomas Cejkaba21b382013-04-13 02:37:32 +0200461 session_hash = pthread_getspecific(thread_key);
462 if (http_server != NULL) {
463 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: fileprint getspecific (%s)", session_hash);
464 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200465 if (pthread_rwlock_wrlock(&session_lock) != 0) {
466 #ifndef TEST_NOTIFICATION_SERVER
Tomas Cejka47387fd2013-06-10 20:37:46 +0200467 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200468 #endif
469 return;
470 }
471 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 +0200472 target_session = get_ncsession_from_key(session_hash);
473 if (target_session == NULL) {
474 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 +0200475 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200476 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200477 return;
478 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200479 return;
480 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200481 if (pthread_mutex_lock(&target_session->lock) != 0) {
482 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock");
483 }
484 if (pthread_rwlock_unlock(&session_lock) != 0) {
485 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock");
486 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200487
Tomas Cejkaba21b382013-04-13 02:37:32 +0200488 if (target_session->notifications == NULL) {
489 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "target_session->notifications is NULL");
490 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200491 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 +0200492 return;
493 }
494 return;
495 }
496 if (http_server != NULL) {
497 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: ready to push to notifications queue");
498 }
499 ntf = (notification_t *) apr_array_push(target_session->notifications);
500 if (ntf == NULL) {
501 ap_log_error (APLOG_MARK, APLOG_ERR, 0, http_server, "Failed to allocate element ");
502 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200503 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200504 return;
505 }
506 return;
507 }
Tomas Cejka73286932013-05-27 22:54:35 +0200508 ntf->eventtime = eventtime;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200509 ntf->content = strdup(content);
510
Tomas Cejka47387fd2013-06-10 20:37:46 +0200511 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 +0200512
513 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200514 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200515 }
516}
517
518/**
519 * \brief Thread for libnetconf notifications dispatch
520 * \param [in] arg - struct ntf_thread_config * with nc_session
521 */
522void* notification_thread(void* arg)
523{
524 struct ntf_thread_config *config = (struct ntf_thread_config*)arg;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200525 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "notifications: in thread for libnetconf notifications");
Tomas Cejka15c56302013-05-30 01:11:30 +0200526
527 /* store hash identification of netconf session for notifications printing callback */
528 if (pthread_setspecific(thread_key, config->session_hash) != 0) {
529 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: cannot set thread-specific hash value.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200530 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200531
Tomas Cejkaba21b382013-04-13 02:37:32 +0200532 if (http_server != NULL) {
533 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notifications: dispatching");
534 }
535 ncntf_dispatch_receive(config->session, notification_fileprint);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200536 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "notifications: ended thread for libnetconf notifications");
537 //free(config);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200538 return (NULL);
539}
540
541
542int notif_subscribe(struct session_with_mutex *locked_session, const char *session_hash, time_t start_time, time_t stop_time)
543{
544 time_t start = -1;
545 time_t stop = -1;
546 struct nc_filter *filter = NULL;
547 char *stream = NULL;
548 nc_rpc *rpc = NULL;
549 pthread_t thread;
550 struct ntf_thread_config *tconfig;
551 struct nc_session *session;
552
Tomas Cejka47387fd2013-06-10 20:37:46 +0200553 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notif_subscribe");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200554 if (locked_session == NULL) {
555 if (http_server != NULL) {
556 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: no locked_session was given.");
557 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200558 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
559 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200560 }
561
Tomas Cejka47387fd2013-06-10 20:37:46 +0200562 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200563 session = locked_session->session;
564
565 start = time(NULL) + start_time;
566 stop = time(NULL) + stop_time;
567 if (http_server != NULL) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: history: %u %u", (unsigned int) start, (unsigned int) stop);
568 }
569
570 if (session == NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200571 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: NETCONF session not established.");
572 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200573 }
574
575 /* check if notifications are allowed on this session */
576 if (nc_session_notif_allowed(session) == 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200577 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Notification subscription is not allowed on this session.");
578 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200579 }
580 /* check times */
581 if (start != -1 && stop != -1 && start > stop) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200582 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Subscription start time must be lower than the end time.");
583 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200584 }
585
Tomas Cejka47387fd2013-06-10 20:37:46 +0200586 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Prepare to execute subscription.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200587 /* create requests */
588 rpc = nc_rpc_subscribe(stream, filter, (start_time == 0)?NULL:&start, (stop_time == 0)?NULL:&stop);
589 nc_filter_free(filter);
590 if (rpc == NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200591 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating an rpc request failed.");
592 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200593 }
594
Tomas Cejka47387fd2013-06-10 20:37:46 +0200595 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Send NC subscribe.");
596 /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */
Tomas Cejkaba21b382013-04-13 02:37:32 +0200597 if (send_recv_process(session, "subscribe", rpc) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200598 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Subscription RPC failed.");
599 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200600 }
601 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200602 locked_session->ntfc_subscribed = 1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200603
Tomas Cejka47387fd2013-06-10 20:37:46 +0200604 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Create config for notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200605 tconfig = malloc(sizeof(struct ntf_thread_config));
606 tconfig->session = session;
607 tconfig->session_hash = strdup(session_hash);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200608 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 +0200609
Tomas Cejka47387fd2013-06-10 20:37:46 +0200610 pthread_mutex_unlock(&locked_session->lock);
611 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Create notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200612 if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200613 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating a thread for receiving notifications failed");
614 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200615 }
616 pthread_detach(thread);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200617 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Subscription finished.");
618 return 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200619}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100620
621static int callback_notification(struct libwebsocket_context *context,
622 struct libwebsocket *wsi,
623 enum libwebsocket_callback_reasons reason,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200624 void *user, void *in, size_t len)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100625{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200626 int n = 0;
627 int m = 0;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200628 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100629 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200630 struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user;
631
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100632 switch (reason) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100633 case LWS_CALLBACK_ESTABLISHED:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200634 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification client connected.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100635 break;
636
637 case LWS_CALLBACK_SERVER_WRITEABLE:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200638 if (pss->session_key == NULL) {
639 return 0;
640 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200641 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Callback server writeable.");
642 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock session lock.");
643 if (pthread_rwlock_wrlock (&session_lock) != 0) {
644 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
645 return -1;
646 }
647 //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 +0200648 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
649 if (ls == NULL) {
650 if (http_server != NULL) {
651 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notification: session not found");
652 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200653 if (pthread_rwlock_unlock (&session_lock) != 0) {
654 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
655 return -1;
656 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200657 return -1;
658 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200659 pthread_mutex_lock(&ls->lock);
660 if (pthread_rwlock_unlock (&session_lock) != 0) {
661 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 +0200662 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200663
Tomas Cejka47387fd2013-06-10 20:37:46 +0200664 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "check for closed session.");
665 if (ls->closed == 1) {
666 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session key.");
667 if (pthread_rwlock_unlock (&session_lock) != 0) {
668 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking unlock: %d (%s)", errno, strerror(errno));
669 return -1;
Tomas Cejka654f84e2013-04-19 11:55:01 +0200670 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200671 return -1;
672 }
673 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock private lock.");
674 notification_t *notif = NULL;
675 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "check for uninitialized notification list.");
676 if (ls->notifications == NULL) {
677 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notification: no notifications array");
678 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock.");
679 if (pthread_mutex_unlock(&ls->lock) != 0) {
680 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: cannot unlock session");
681 }
682 return -1;
683 }
684 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "check for empty notification list.");
685 if (!apr_is_empty_array(ls->notifications)) {
686 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: POP notifications for session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200687
Tomas Cejka654f84e2013-04-19 11:55:01 +0200688 while ((notif = (notification_t *) apr_array_pop(ls->notifications)) != NULL) {
Tomas Cejka654f84e2013-04-19 11:55:01 +0200689 n = 0;
Tomas Cejka73286932013-05-27 22:54:35 +0200690 json_object *notif_json = json_object_new_object();
Tomas Cejka8800d652013-07-16 10:47:08 +0200691 json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime));
Tomas Cejka73286932013-05-27 22:54:35 +0200692 json_object_object_add(notif_json, "content", json_object_new_string(notif->content));
693
Tomas Cejka00635972013-06-03 15:10:52 +0200694 const char *msgtext = json_object_to_json_string(notif_json);
Tomas Cejka15c56302013-05-30 01:11:30 +0200695
Tomas Cejka47387fd2013-06-10 20:37:46 +0200696 //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t);
Tomas Cejka15c56302013-05-30 01:11:30 +0200697 n = sprintf((char *)p, "%s", msgtext);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200698 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 +0200699 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200700 if (lws_send_pipe_choked(wsi)) {
701 libwebsocket_callback_on_writable(context, wsi);
702 break;
703 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200704
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200705 json_object_put(notif_json);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200706 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200707 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: POP notifications done");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200708 }
709
Tomas Cejka47387fd2013-06-10 20:37:46 +0200710 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200711 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200712 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200713 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200714 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200715
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100716 if (m < n) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200717 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200718
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100719 return -1;
720 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100721 break;
722
723 case LWS_CALLBACK_RECEIVE:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200724 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Callback receive.");
725 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "received: (%s)", (char *)in);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200726 if (pss->session_key == NULL) {
727 char session_key_buf[41];
728 int start = -1;
729 time_t stop = time(NULL) + 30;
730
731 strncpy((char *) session_key_buf, (const char *) in, 40);
732 session_key_buf[40] = '\0';
733 pss->session_key = strdup(session_key_buf);
734 sscanf(in+40, "%d %d", (int *) &start, (int *) &stop);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200735 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 +0200736
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200737 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock session lock");
738 if (pthread_rwlock_rdlock (&session_lock) != 0) {
739 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock: %d (%s)", errno, strerror(errno));
740 return -1;
741 }
742 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 +0200743 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
744 if (ls == NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200745 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 +0200746 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
747 if (pthread_rwlock_unlock (&session_lock) != 0) {
748 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 +0200749 }
750 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
751 return -1;
752 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200753 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "lock private lock");
754 pthread_mutex_lock(&ls->lock);
755
756 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
757 if (pthread_rwlock_unlock (&session_lock) != 0) {
758 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
759 }
760
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200761 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Found session to subscribe notif.");
762 if (ls->closed == 1) {
763 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "session already closed - handle no notification");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200764 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
765 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200766 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
767 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200768 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200769 if (ls->ntfc_subscribed != 0) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200770 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: already subscribed");
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 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200774 return 0;
775 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200776 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: prepare to subscribe stream");
777 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200778 pthread_mutex_unlock(&ls->lock);
779
780 /* notif_subscribe locks on its own */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200781 return notif_subscribe(ls, pss->session_key, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200782 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100783 if (len < 6)
784 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100785 break;
786 /*
787 * this just demonstrates how to use the protocol filter. If you won't
788 * study and reject connections based on header content, you don't need
789 * to handle this callback
790 */
791
792 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
793 //dump_handshake_info(wsi);
794 /* you could return non-zero here and kill the connection */
795 break;
796
797 default:
798 break;
799 }
800
801 return 0;
802}
803
804/* list of supported protocols and callbacks */
805
806static struct libwebsocket_protocols protocols[] = {
807 /* first protocol must always be HTTP handler */
808
809 {
810 "http-only", /* name */
811 callback_http, /* callback */
812 sizeof (struct per_session_data__http), /* per_session_data_size */
813 0, /* max frame size / rx buffer */
814 },
815 {
816 "notification-protocol",
817 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200818 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200819 4000,
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100820 },
821 { NULL, NULL, 0, 0 } /* terminator */
822};
823
824
Tomas Cejkaba21b382013-04-13 02:37:32 +0200825/**
826 * initialization of notification module
827 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200828int notification_init(apr_pool_t * pool, server_rec * server)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100829{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200830 //char cert_path[1024];
831 //char key_path[1024];
832 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100833 struct lws_context_creation_info info;
834 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200835 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100836 const char *iface = NULL;
837 int debug_level = 7;
838
839 memset(&info, 0, sizeof info);
840 info.port = NOTIFICATION_SERVER_PORT;
841
842 /* tell the library what debug level to emit and to send it to syslog */
843 lws_set_log_level(debug_level, lwsl_emit_syslog);
844
Tomas Cejka47387fd2013-06-10 20:37:46 +0200845 http_server = server;
846 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200847 //lwsl_notice("libwebsockets test server - "
848 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
849 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100850 max_poll_elements = getdtablesize();
851 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
852 fd_lookup = malloc(max_poll_elements * sizeof (int));
853 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejka15c56302013-05-30 01:11:30 +0200854 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 +0100855 return -1;
856 }
857
858 info.iface = iface;
859 info.protocols = protocols;
860
861 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
862 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
863
864 //info.ssl_cert_filepath = cert_path;
865 //info.ssl_private_key_filepath = key_path;
866
867 info.gid = -1;
868 info.uid = -1;
869 info.options = opts;
870
871 /* create server */
872 context = libwebsocket_create_context(&info);
873 if (context == NULL) {
Tomas Cejka15c56302013-05-30 01:11:30 +0200874 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100875 return -1;
876 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200877
878 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notifications: init of pthread_key_create.");
879 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200880 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200881 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200882 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100883}
884
885void notification_close()
886{
887 libwebsocket_context_destroy(context);
888
Tomas Cejka15c56302013-05-30 01:11:30 +0200889 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100890}
891
Tomas Cejkaba21b382013-04-13 02:37:32 +0200892
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100893/**
894 * \brief send notification if any
895 * \return < 0 on error
896 */
897int notification_handle()
898{
899 static struct timeval tv;
900 static unsigned int olds = 0;
901 int n = 0;
902
903 gettimeofday(&tv, NULL);
904
905 /*
906 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
907 * live websocket connection using the DUMB_INCREMENT protocol,
908 * as soon as it can take more packets (usually immediately)
909 */
910
911 if (((unsigned int)tv.tv_sec - olds) > 0) {
912 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
913 olds = tv.tv_sec;
914 }
915
916
917 /*
918 * this represents an existing server's single poll action
919 * which also includes libwebsocket sockets
920 */
921
922 n = poll(pollfds, count_pollfds, 50);
923 if (n < 0)
924 return n;
925
926
927 if (n) {
928 for (n = 0; n < count_pollfds; n++) {
929 if (pollfds[n].revents) {
930 /*
931 * returns immediately if the fd does not
932 * match anything under libwebsockets
933 * control
934 */
935 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
936 return 1;
937 }
938 }
939 }
940 }
941 return 0;
942}
943
944#endif
945
946
947#ifndef WITH_NOTIFICATIONS
948#ifdef TEST_NOTIFICATION_SERVER
949int main(int argc, char **argv)
950{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100951 if (notification_init(NULL, NULL) == -1) {
952 fprintf(stderr, "Error during initialization\n");
953 return 1;
954 }
955 while (!force_exit) {
956 notification_handle();
957 }
958 notification_close();
959}
960#endif
961#endif