Plugin server-status cleanup
[platform/upstream/libwebsockets.git] / plugins / protocol_lws_server_status.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * The test apps are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20
21 #define LWS_DLL
22 #define LWS_INTERNAL
23 #include "../lib/libwebsockets.h"
24 #include <string.h>
25 #include <stdlib.h>
26
27 struct lws_ss_load_sample {
28         time_t t;
29         int load_x100;
30 };
31
32 struct lws_ss_dumps {
33         char buf[32768];
34         int length;
35
36         struct lws_ss_load_sample load[64];
37         int load_head;
38         int load_tail;
39 };
40
41 static struct lws_ss_dumps d;
42 static uv_timer_t timeout_watcher;
43 static struct lws_context *context;
44 static int tow_flag;
45
46 struct per_session_data__server_status {
47         int ver;
48         int pos;
49 };
50
51 static const struct lws_protocols protocols[1];
52
53 static void
54 uv_timeout_cb_server_status(uv_timer_t *w
55 #if UV_VERSION_MAJOR == 0
56                 , int status
57 #endif
58 )
59 {
60         char *p = d.buf + LWS_PRE;
61
62 #if 0
63 #ifdef LWS_HAVE_GETLOADAVG
64         double l = 0.0;
65
66         getloadavg(&l, 1);
67         d.load[d.load_head].load_x100 = (int)(l * 100);
68         d.load[d.load_head].t = lws_now_secs();
69         d.load_head++;
70         if (d.load_head == ARRAY_SIZE(d.load))
71                 d.load_head = 0;
72         if (d.load_head == d.load_tail) {
73                 d.load_tail++;
74                 if (d.load_tail == ARRAY_SIZE(d.load))
75                         d.load_tail = 0;
76         }
77 #endif
78 #endif
79
80         d.length = lws_json_dump_context(context, p,
81                                          sizeof(d.buf) - LWS_PRE);
82
83         lws_callback_on_writable_all_protocol(context, &protocols[0]);
84 }
85
86 static int
87 callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
88                            void *user, void *in, size_t len)
89 {
90         const struct lws_protocol_vhost_options *pvo =
91                         (const struct lws_protocol_vhost_options *)in;
92         int m, period = 1000;
93
94         switch (reason) {
95
96         case LWS_CALLBACK_ESTABLISHED:
97                 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
98                 lws_callback_on_writable(wsi);
99                 break;
100
101         case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
102                 if (tow_flag)
103                         break;
104                 while (pvo) {
105                         if (!strcmp(pvo->name, "update-ms"))
106                                 period = atoi(pvo->value);
107                         pvo = pvo->next;
108                 }
109                 context = lws_get_context(wsi);
110                 uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
111                 uv_timer_start(&timeout_watcher,
112                                 uv_timeout_cb_server_status, 2000, period);
113                 tow_flag = 1;
114                 break;
115
116         case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
117                 if (!tow_flag)
118                         break;
119                 uv_timer_stop(&timeout_watcher);
120                 tow_flag = 0;
121                 break;
122
123         case LWS_CALLBACK_SERVER_WRITEABLE:
124                 m = lws_write(wsi, (unsigned char *)d.buf + LWS_PRE, d.length,
125                               LWS_WRITE_TEXT);
126                 if (m < 0)
127                         return -1;
128                 break;
129
130         default:
131                 break;
132         }
133
134         return 0;
135 }
136
137 static const struct lws_protocols protocols[] = {
138         {
139                 "lws-server-status",
140                 callback_lws_server_status,
141                 sizeof(struct per_session_data__server_status),
142                 1024,
143         },
144 };
145
146 LWS_EXTERN LWS_VISIBLE int
147 init_protocol_lws_server_status(struct lws_context *context,
148                              struct lws_plugin_capability *c)
149 {
150         if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
151                 lwsl_err("Plugin API %d, library API %d",
152                          LWS_PLUGIN_API_MAGIC, c->api_magic);
153                 return 1;
154         }
155
156         c->protocols = protocols;
157         c->count_protocols = ARRAY_SIZE(protocols);
158         c->extensions = NULL;
159         c->count_extensions = 0;
160
161         return 0;
162 }
163
164 LWS_EXTERN LWS_VISIBLE int
165 destroy_protocol_lws_server_status(struct lws_context *context)
166 {
167         return 0;
168 }
169