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
|
/* Test libuEv event library
*
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
* Copyright (c) 2013-2024 Joachim Wiberg <troglobit()gmail!com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "check.h"
#include <signal.h>
typedef struct {
int counter;
} my_t;
static int in, out;
static int period = 0;
static uev_t *watchdog;
static void lifetime_cb(uev_t *w, void *arg, int events)
{
if (UEV_ERROR == events)
fprintf(stderr, "timer watcher failed, ignoring ...\n");
fprintf(stderr, "\nLifetime exceeded, program completed successfully! (arg:%p)\n", arg);
uev_exit(w->ctx);
}
/* The pipe watchdog, if it triggers we haven't received data in time. */
static void timeout_cb(uev_t *w, void *arg, int events)
{
if (UEV_ERROR == events)
fprintf(stderr, "timer watcher failed, ignoring ...\n");
watchdog = NULL;
fprintf(stderr, "\nTimeout exceeded %p\n", arg);
// uev_timer_stop(w); <-- No need to stop timers with period=0 :)
uev_exit(w->ctx);
}
static void periodic_task(uev_t *w, void *arg, int events)
{
fprintf(stderr, "|");
}
static void signal_cb(uev_t *w, void *arg, int events)
{
if (UEV_ERROR == events) {
fprintf(stderr, "Signal watcher failed, unrecoverable error.\n");
return;
}
fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v");
}
static void pipe_read_cb(uev_t *w, void *arg, int events)
{
int len;
char msg[50];
if (UEV_ERROR == events) {
error:
fprintf(stderr, "pipe watcher failed, restarting ...\n");
uev_io_start(w);
return;
}
/* Kick watchdog */
if (watchdog)
uev_timer_set(watchdog, 1000, 0);
/* Here we can read() from in or w->fd, either works. */
len = read(w->fd, msg, sizeof(msg));
if (len < 0) {
perror(__func__);
goto error;
}
if (len == 0 || UEV_HUP == events) {
fprintf(stderr, "Other end of pipe closed, no more data\n");
return;
}
// fprintf(stderr, "READ %.*s %d\n", len, msg, len);
fprintf(stderr, "%.*s.%d ", len, msg, len);
}
static void pipe_write_cb(uev_t *w, void *arg, int events)
{
my_t *my = arg;
char *msg = "TESTING";
if (UEV_ERROR == events)
fprintf(stderr, "timer watcher failed, ignoring ...\n");
/* Here we *must* use out, not w->fd, since we're a timer callback */
if (write(out, msg, my->counter) < 0) {
perror("\nFailed writing to pipe");
return;
}
// fprintf(stderr, "WRITE %.*s %d\n", my->counter, msg, my->counter);
fprintf(stderr, "%d ", my->counter);
period = my->counter + 5;
my->counter++;
uev_timer_set(w, period * 100, 0);
}
int main(void)
{
int fd[2];
my_t my = { .counter = 1 };
uev_t timeout, wdt, periodic, writer, reader, sigint_watcher, sigquit_watcher;
uev_ctx_t ctx;
/* Work load, one timer callback writes to a pipe periodically,
* and one I/O watcher that is called every time the pipe has
* some data. */
if (pipe(fd) < 0)
return 1;
/* Initialize libuEv */
uev_init(&ctx);
/* Signal watchers, Ctrl-C => SIGINT, Ctrl-\ => SIGQUIT */
uev_signal_init(&ctx, &sigint_watcher, signal_cb, NULL, SIGINT);
uev_signal_init(&ctx, &sigquit_watcher, signal_cb, NULL, SIGQUIT);
/* Total program execution time */
uev_timer_init(&ctx, &timeout, lifetime_cb, (void *)(intptr_t)2, 4000, 0);
/* Main pipe worker and consumer */
in = fd[0];
out = fd[1];
uev_io_init(&ctx, &reader, pipe_read_cb, NULL, in, UEV_READ);
uev_timer_init(&ctx, &writer, pipe_write_cb, &my, 400, 0);
/*
* Watchdog for the above timer callback, if it doesn't wake up
* and write to the pipe within a given deadline it will bark.
*/
uev_timer_init(&ctx, &wdt, timeout_cb, (void *)(intptr_t)1, 950, 0);
watchdog = &wdt;
/* Periodic background task */
uev_timer_init(&ctx, &periodic, periodic_task, NULL, 200, 200);
/* Start event loop */
uev_run(&ctx, 0);
fprintf(stderr, "Period is %d must be 10: %s\n", period, 10 == period ? "OK" : "ERROR!");
fail_unless(10 == period);
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
|