protocol_lws_server_status.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #define LWS_DLL
  21. #define LWS_INTERNAL
  22. #include "../lib/libwebsockets.h"
  23. #include <string.h>
  24. #include <stdlib.h>
  25. struct lws_ss_load_sample {
  26. time_t t;
  27. int load_x100;
  28. };
  29. struct lws_ss_dumps {
  30. char buf[32768];
  31. int length;
  32. struct lws_ss_load_sample load[64];
  33. int load_head;
  34. int load_tail;
  35. };
  36. static struct lws_ss_dumps d;
  37. static uv_timer_t timeout_watcher;
  38. static struct lws_context *context;
  39. static int tow_flag;
  40. struct per_session_data__server_status {
  41. int ver;
  42. int pos;
  43. };
  44. static const struct lws_protocols protocols[1];
  45. static void
  46. uv_timeout_cb_server_status(uv_timer_t *w
  47. #if UV_VERSION_MAJOR == 0
  48. , int status
  49. #endif
  50. )
  51. {
  52. char *p = d.buf + LWS_PRE;
  53. #if 0
  54. #ifdef LWS_HAVE_GETLOADAVG
  55. double l = 0.0;
  56. getloadavg(&l, 1);
  57. d.load[d.load_head].load_x100 = (int)(l * 100);
  58. d.load[d.load_head].t = lws_now_secs();
  59. d.load_head++;
  60. if (d.load_head == ARRAY_SIZE(d.load))
  61. d.load_head = 0;
  62. if (d.load_head == d.load_tail) {
  63. d.load_tail++;
  64. if (d.load_tail == ARRAY_SIZE(d.load))
  65. d.load_tail = 0;
  66. }
  67. #endif
  68. #endif
  69. d.length = lws_json_dump_context(context, p,
  70. sizeof(d.buf) - LWS_PRE);
  71. lws_callback_on_writable_all_protocol(context, &protocols[0]);
  72. }
  73. static int
  74. callback_lws_server_status(struct lws *wsi, enum lws_callback_reasons reason,
  75. void *user, void *in, size_t len)
  76. {
  77. const struct lws_protocol_vhost_options *pvo =
  78. (const struct lws_protocol_vhost_options *)in;
  79. int m, period = 1000;
  80. switch (reason) {
  81. case LWS_CALLBACK_ESTABLISHED:
  82. lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
  83. lws_callback_on_writable(wsi);
  84. break;
  85. case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
  86. if (tow_flag)
  87. break;
  88. while (pvo) {
  89. if (!strcmp(pvo->name, "update-ms"))
  90. period = atoi(pvo->value);
  91. pvo = pvo->next;
  92. }
  93. context = lws_get_context(wsi);
  94. uv_timer_init(lws_uv_getloop(context, 0), &timeout_watcher);
  95. uv_timer_start(&timeout_watcher,
  96. uv_timeout_cb_server_status, 2000, period);
  97. tow_flag = 1;
  98. break;
  99. case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
  100. if (!tow_flag)
  101. break;
  102. uv_timer_stop(&timeout_watcher);
  103. tow_flag = 0;
  104. break;
  105. case LWS_CALLBACK_SERVER_WRITEABLE:
  106. m = lws_write(wsi, (unsigned char *)d.buf + LWS_PRE, d.length,
  107. LWS_WRITE_TEXT);
  108. if (m < 0)
  109. return -1;
  110. break;
  111. default:
  112. break;
  113. }
  114. return 0;
  115. }
  116. static const struct lws_protocols protocols[] = {
  117. {
  118. "lws-server-status",
  119. callback_lws_server_status,
  120. sizeof(struct per_session_data__server_status),
  121. 1024,
  122. },
  123. };
  124. LWS_EXTERN LWS_VISIBLE int
  125. init_protocol_lws_server_status(struct lws_context *context,
  126. struct lws_plugin_capability *c)
  127. {
  128. if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
  129. lwsl_err("Plugin API %d, library API %d",
  130. LWS_PLUGIN_API_MAGIC, c->api_magic);
  131. return 1;
  132. }
  133. c->protocols = protocols;
  134. c->count_protocols = ARRAY_SIZE(protocols);
  135. c->extensions = NULL;
  136. c->count_extensions = 0;
  137. return 0;
  138. }
  139. LWS_EXTERN LWS_VISIBLE int
  140. destroy_protocol_lws_server_status(struct lws_context *context)
  141. {
  142. return 0;
  143. }