1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 #include <isc/app.h>
13 #include <isc/mem.h>
14 #include <isc/print.h>
15 #include <isc/ratelimiter.h>
16 #include <isc/task.h>
17 #include <isc/time.h>
18 #include <isc/timer.h>
19 #include <isc/util.h>
20 
21 isc_ratelimiter_t *rlim = NULL;
22 isc_taskmgr_t *taskmgr = NULL;
23 isc_timermgr_t *timermgr = NULL;
24 isc_task_t *g_task = NULL;
25 isc_mem_t *mctx = NULL;
26 
27 static void
28 utick(isc_task_t *task, isc_event_t *event);
29 static void
30 shutdown_rl(isc_task_t *task, isc_event_t *event);
31 static void
32 shutdown_all(isc_task_t *task, isc_event_t *event);
33 
34 typedef struct {
35 	int milliseconds;
36 	void (*fun)(isc_task_t *, isc_event_t *);
37 } schedule_t;
38 
39 schedule_t schedule[] = { { 100, utick },	 { 200, utick },
40 			  { 300, utick },	 { 3000, utick },
41 			  { 3100, utick },	 { 3200, utick },
42 			  { 3300, shutdown_rl }, { 5000, utick },
43 			  { 6000, shutdown_all } };
44 
45 #define NEVENTS (int)(sizeof(schedule) / sizeof(schedule[0]))
46 
47 isc_timer_t *timers[NEVENTS];
48 
49 static void
ltick(isc_task_t * task,isc_event_t * event)50 ltick(isc_task_t *task, isc_event_t *event) {
51 	UNUSED(task);
52 	printf("** ltick%s **\n",
53 	       (event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0 ? " ("
54 								      "canceled"
55 								      ")"
56 								    : "");
57 	isc_event_free(&event);
58 }
59 
60 static void
utick(isc_task_t * task,isc_event_t * event)61 utick(isc_task_t *task, isc_event_t *event) {
62 	isc_result_t result;
63 	UNUSED(task);
64 	event->ev_action = ltick;
65 	event->ev_sender = NULL;
66 	result = isc_ratelimiter_enqueue(rlim, g_task, &event);
67 	printf("enqueue: %s\n", result == ISC_R_SUCCESS ? "ok" : "failed");
68 }
69 
70 static void
shutdown_rl(isc_task_t * task,isc_event_t * event)71 shutdown_rl(isc_task_t *task, isc_event_t *event) {
72 	UNUSED(task);
73 	UNUSED(event);
74 	printf("shutdown ratelimiter\n");
75 	isc_ratelimiter_shutdown(rlim);
76 }
77 
78 static void
shutdown_all(isc_task_t * task,isc_event_t * event)79 shutdown_all(isc_task_t *task, isc_event_t *event) {
80 	int i;
81 	UNUSED(task);
82 	UNUSED(event);
83 	printf("shutdown all\n");
84 	for (i = 0; i < NEVENTS; i++) {
85 		isc_timer_detach(&timers[i]);
86 	}
87 
88 	isc_app_shutdown();
89 }
90 
91 int
main(int argc,char * argv[])92 main(int argc, char *argv[]) {
93 	isc_interval_t linterval;
94 	int i;
95 
96 	UNUSED(argc);
97 	UNUSED(argv);
98 
99 	isc_app_start();
100 	isc_interval_set(&linterval, 1, 0);
101 
102 	isc_mem_create(&mctx);
103 	RUNTIME_CHECK(isc_taskmgr_create(mctx, 3, 0, NULL, &taskmgr) ==
104 		      ISC_R_SUCCESS);
105 	RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) == ISC_R_SUCCESS);
106 	RUNTIME_CHECK(isc_task_create(taskmgr, 0, &g_task) == ISC_R_SUCCESS);
107 
108 	RUNTIME_CHECK(isc_ratelimiter_create(mctx, timermgr, g_task, &rlim) ==
109 		      ISC_R_SUCCESS);
110 
111 	RUNTIME_CHECK(isc_ratelimiter_setinterval(rlim, &linterval) ==
112 		      ISC_R_SUCCESS);
113 
114 	for (i = 0; i < NEVENTS; i++) {
115 		isc_interval_t uinterval;
116 		int ms = schedule[i].milliseconds;
117 		isc_interval_set(&uinterval, ms / 1000, (ms % 1000) * 1000000);
118 		timers[i] = NULL;
119 		RUNTIME_CHECK(isc_timer_create(timermgr, isc_timertype_once,
120 					       NULL, &uinterval, g_task,
121 					       schedule[i].fun, NULL,
122 					       &timers[i]) == ISC_R_SUCCESS);
123 	}
124 
125 	isc_app_run();
126 
127 	isc_task_destroy(&g_task);
128 
129 	isc_ratelimiter_detach(&rlim);
130 
131 	isc_timermgr_destroy(&timermgr);
132 	isc_taskmgr_destroy(&taskmgr);
133 
134 	isc_mem_stats(mctx, stdout);
135 
136 	isc_app_finish();
137 	return (0);
138 }
139