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