1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* lws-api-test-lws_smd
*
* Written in 2020 by Andy Green <andy@warmcat.com>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
* This api test confirms lws_smd System Message Distribution
*/
#include <libwebsockets.h>
#define HAVE_STRUCT_TIMESPEC
#include <pthread.h>
#include <signal.h>
static int interrupted, ok, fail, _exp = 111;
static unsigned int how_many_msg = 100, usec_interval = 1000;
static lws_sorted_usec_list_t sul, sul_initial_drain;
struct lws_context *context;
static pthread_t thread_spam;
static void
timeout_cb(lws_sorted_usec_list_t *sul)
{
/* We should have completed the test before this fires */
lwsl_notice("%s: test period finished\n", __func__);
interrupted = 1;
lws_cancel_service(context);
}
static int
smd_cb1int(void *opaque, lws_smd_class_t _class, lws_usec_t timestamp,
void *buf, size_t len)
{
#if 0
lwsl_notice("%s: ts %llu, len %d\n", __func__,
(unsigned long long)timestamp, (int)len);
lwsl_hexdump_notice(buf, len);
#endif
ok++;
return 0;
}
static int
smd_cb2int(void *opaque, lws_smd_class_t _class, lws_usec_t timestamp,
void *buf, size_t len)
{
#if 0
lwsl_notice("%s: ts %llu, len %d\n", __func__,
(unsigned long long)timestamp, (int)len);
lwsl_hexdump_notice(buf, len);
#endif
ok++;
return 0;
}
/*
* This is used in an smd participant that is deregistered before the message
* can be delivered, it should never see any message
*/
static int
smd_cb3int(void *opaque, lws_smd_class_t _class, lws_usec_t timestamp,
void *buf, size_t len)
{
lwsl_err("%s: Countermanded ts %llu, len %d\n", __func__,
(unsigned long long)timestamp, (int)len);
lwsl_hexdump_err(buf, len);
fail++;
return 0;
}
static void *
_thread_spam(void *d)
{
#if defined(WIN32)
unsigned int mypid = 0;
#else
unsigned int mypid = (unsigned int)getpid();
#endif
unsigned int n = 0, atm = 0;
while (n++ < how_many_msg) {
atm++;
if (lws_smd_msg_printf(context, LWSSMDCL_SYSTEM_STATE,
"{\"s\":\"state\","
"\"pid\":%u,"
"\"msg\":%d}",
mypid, (unsigned int)n)) {
lwsl_err("%s: send attempt %d failed\n", __func__, atm);
n--;
fail++;
if (fail >= 3) {
interrupted = 1;
lws_cancel_service(context);
break;
}
}
#if defined(WIN32)
Sleep(3);
#else
usleep(usec_interval);
#endif
}
#if !defined(WIN32)
pthread_exit(NULL);
#endif
return NULL;
}
void sigint_handler(int sig)
{
interrupted = 1;
}
static void
drained_cb(lws_sorted_usec_list_t *sul)
{
/*
* spawn the test thread, it's going to spam 100 messages at 3ms
* intervals... check we got everything
*/
if (pthread_create(&thread_spam, NULL, _thread_spam, NULL))
lwsl_err("%s: failed to create the spamming thread\n", __func__);
}
static int
system_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
int current, int target)
{
// struct lws_context *context = mgr->parent;
int n;
if (current != LWS_SYSTATE_OPERATIONAL || target != LWS_SYSTATE_OPERATIONAL)
return 0;
/*
* Overflow the message queue too see if it handles it well, both
* as overflowing and in recovery. These are all still going into the
* smd buffer dll2, since we don't break for the event loop to have a
* chance to deliver them.
*/
n = 0;
while (n++ < 100)
if (lws_smd_msg_printf(context, LWSSMDCL_SYSTEM_STATE,
"{\"s\":\"state\",\"test\":\"overflow\"}"))
break;
lwsl_notice("%s: overflow test added %d messages\n", __func__, n);
if (n == 100) {
lwsl_err("%s: didn't overflow\n", __func__);
interrupted = 1;
return 1;
}
/*
* So we have some normal messages from earlier and now the rest of the
* smd buffer filled with junk overflow messages. Before we start the
* actual spamming test from another thread, we need to return to the
* event loop so these can be cleared first.
*/
lws_sul_schedule(context, 0, &sul_initial_drain, drained_cb,
5 * LWS_US_PER_MS);
lwsl_info("%s: operational\n", __func__);
return 0;
}
int
main(int argc, const char **argv)
{
lws_state_notify_link_t notifier = { { NULL, NULL, NULL },
system_notify_cb, "app" };
lws_state_notify_link_t *na[] = { ¬ifier, NULL };
int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
struct lws_context_creation_info info;
struct lws_smd_peer *userreg;
const char *p;
void *retval;
/* the normal lws init */
signal(SIGINT, sigint_handler);
if ((p = lws_cmdline_option(argc, argv, "-d")))
logs = atoi(p);
if ((p = lws_cmdline_option(argc, argv, "--count")))
how_many_msg = (unsigned int)atol(p);
if ((p = lws_cmdline_option(argc, argv, "--interval")))
usec_interval = (unsigned int)atol(p);
lws_set_log_level(logs, NULL);
lwsl_user("LWS API selftest: lws_smd: %u msgs at %uus interval\n",
how_many_msg, usec_interval);
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
info.port = CONTEXT_PORT_NO_LISTEN;
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
info.register_notifier_list = na;
context = lws_create_context(&info);
if (!context) {
lwsl_err("lws init failed\n");
return 1;
}
/* game over after this long */
lws_sul_schedule(context, 0, &sul, timeout_cb,
(how_many_msg * (usec_interval + 1000)) + (4 * LWS_US_PER_SEC));
/* register a messaging participant to hear INTERACTION class */
if (!lws_smd_register(context, NULL, 0, LWSSMDCL_INTERACTION,
smd_cb1int)) {
lwsl_err("%s: smd register 1 failed\n", __func__);
goto bail;
}
/* register a messaging participant to hear SYSTEM_STATE class */
if (!lws_smd_register(context, NULL, 0, LWSSMDCL_SYSTEM_STATE,
smd_cb2int)) {
lwsl_err("%s: smd register 2 failed\n", __func__);
goto bail;
}
/* temporarily register a messaging participant to hear a user class */
userreg = lws_smd_register(context, NULL, 0, 1 << LWSSMDCL_USER_BASE_BITNUM,
smd_cb3int);
if (!userreg) {
lwsl_err("%s: smd register userclass failed\n", __func__);
goto bail;
}
/*
* The event loop isn't started yet, so these smd messages are getting
* buffered. Later we will deliberately overrun the buffer and wait
* for that to be cleared before the spam thread test.
*/
/* generate an INTERACTION class message */
if (lws_smd_msg_printf(context, LWSSMDCL_INTERACTION,
"{\"s\":\"interaction\"}")) {
lwsl_err("%s: problem sending smd\n", __func__);
goto bail;
}
/* generate a SYSTEM_STATE class message */
if (lws_smd_msg_printf(context, LWSSMDCL_SYSTEM_STATE,
"{\"s\":\"state\"}")) {
lwsl_err("%s: problem sending smd\n", __func__);
goto bail;
}
/* no participant listens for this class, so it should be skipped */
if (lws_smd_msg_printf(context, LWSSMDCL_NETWORK, "{\"s\":\"network\"}")) {
lwsl_err("%s: problem sending smd\n", __func__);
goto bail;
}
/* generate a user class message... */
if (lws_smd_msg_printf(context, 1 << LWSSMDCL_USER_BASE_BITNUM,
"{\"s\":\"userclass\"}")) {
lwsl_err("%s: problem sending smd\n", __func__);
goto bail;
}
/*
* ... and screw that user class message up by deregistering the only
* handler before it can deliver it... it should not get delivered
* and cleanly discarded
*/
lws_smd_unregister(userreg);
/* the usual lws event loop */
while (!interrupted && lws_service(context, 0) >= 0)
;
pthread_join(thread_spam, &retval);
bail:
lws_context_destroy(context);
if (fail || ok >= _exp)
lwsl_user("Completed: PASS: %d / %d, FAIL: %d\n", ok, _exp,
fail);
else
lwsl_user("Completed: ALL PASS: %d / %d\n", ok, _exp);
return !(ok >= _exp && !fail);
}
|