2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
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.
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
23 #include "../lib/libwebsockets.h"
27 struct lws_ss_load_sample {
36 struct lws_ss_load_sample load[64];
41 static struct lws_ss_dumps d;
42 static uv_timer_t timeout_watcher;
43 static struct lws_context *context;
46 struct per_session_data__server_status {
51 static const struct lws_protocols protocols[1];
54 uv_timeout_cb_server_status(uv_timer_t *w
55 #if UV_VERSION_MAJOR == 0
60 char *p = d.buf + LWS_PRE;
63 #ifdef LWS_HAVE_GETLOADAVG
67 d.load[d.load_head].load_x100 = (int)(l * 100);
68 d.load[d.load_head].t = lws_now_secs();
70 if (d.load_head == ARRAY_SIZE(d.load))
72 if (d.load_head == d.load_tail) {
74 if (d.load_tail == ARRAY_SIZE(d.load))
80 d.length = lws_json_dump_context(context, p,
81 sizeof(d.buf) - LWS_PRE);
83 lws_callback_on_writable_all_protocol(context, &protocols[0]);
87 callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
88 void *user, void *in, size_t len)
90 const struct lws_protocol_vhost_options *pvo =
91 (const struct lws_protocol_vhost_options *)in;
96 case LWS_CALLBACK_ESTABLISHED:
97 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
98 lws_callback_on_writable(wsi);
101 case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
105 if (!strcmp(pvo->name, "update-ms"))
106 period = atoi(pvo->value);
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);
116 case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
119 uv_timer_stop(&timeout_watcher);
123 case LWS_CALLBACK_SERVER_WRITEABLE:
124 m = lws_write(wsi, (unsigned char *)d.buf + LWS_PRE, d.length,
137 static const struct lws_protocols protocols[] = {
140 callback_lws_server_status,
141 sizeof(struct per_session_data__server_status),
146 LWS_EXTERN LWS_VISIBLE int
147 init_protocol_lws_server_status(struct lws_context *context,
148 struct lws_plugin_capability *c)
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);
156 c->protocols = protocols;
157 c->count_protocols = ARRAY_SIZE(protocols);
158 c->extensions = NULL;
159 c->count_extensions = 0;
164 LWS_EXTERN LWS_VISIBLE int
165 destroy_protocol_lws_server_status(struct lws_context *context)