root/src/noit_check_tools.c

Revision 4b55797971a3f98431112b9eb43d2a76c7ba3e2b, 4.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

no functional change.

Just split out the noit_check_tools that may need noit_check information from
the tools that operate on more primitive objects. This will prevent stratcon
from needing to pull in the kitchen sink to get convenience functions.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "noit_check_tools.h"
35 #include "noit_check_tools_shared.h"
36 #include "utils/noit_str.h"
37
38 #include <assert.h>
39
40 typedef struct {
41   noit_module_t *self;
42   noit_check_t *check;
43   dispatch_func_t dispatch;
44 } recur_closure_t;
45
46 static int
47 noit_check_recur_handler(eventer_t e, int mask, void *closure,
48                               struct timeval *now) {
49   recur_closure_t *rcl = closure;
50   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
51   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
52                            rcl->dispatch);
53   rcl->dispatch(rcl->self, rcl->check);
54   free(rcl);
55   return 0;
56 }
57
58 int
59 noit_check_schedule_next(noit_module_t *self,
60                          struct timeval *last_check, noit_check_t *check,
61                          struct timeval *now, dispatch_func_t dispatch) {
62   eventer_t newe;
63   struct timeval period, earliest;
64   recur_closure_t *rcl;
65
66   assert(check->fire_event == NULL);
67   if(check->period == 0) return 0;
68   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
69
70   /* If we have an event, we know when we intended it to fire.  This means
71    * we should schedule that point + period.
72    */
73   if(now)
74     memcpy(&earliest, now, sizeof(earliest));
75   else
76     gettimeofday(&earliest, NULL);
77   period.tv_sec = check->period / 1000;
78   period.tv_usec = (check->period % 1000) * 1000;
79
80   newe = eventer_alloc();
81   memcpy(&newe->whence, last_check, sizeof(*last_check));
82   add_timeval(newe->whence, period, &newe->whence);
83   if(compare_timeval(newe->whence, earliest) < 0)
84     memcpy(&newe->whence, &earliest, sizeof(earliest));
85   newe->mask = EVENTER_TIMER;
86   newe->callback = noit_check_recur_handler;
87   rcl = calloc(1, sizeof(*rcl));
88   rcl->self = self;
89   rcl->check = check;
90   rcl->dispatch = dispatch;
91   newe->closure = rcl;
92
93   eventer_add(newe);
94   check->fire_event = newe;
95   return 0;
96 }
97
98 void
99 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
100   struct timeval __now, p_int;
101   eventer_t e;
102   e = eventer_alloc();
103   e->fd = -1;
104   e->mask = EVENTER_ASYNCH;
105   gettimeofday(&__now, NULL);
106   memcpy(&e->whence, &__now, sizeof(__now));
107   p_int.tv_sec = check->timeout / 1000;
108   p_int.tv_usec = (check->timeout % 1000) * 1000;
109   add_timeval(e->whence, p_int, &e->whence);
110   e->callback = callback;
111   e->closure =  check->closure;
112   eventer_add(e);
113 }
114
115 void
116 noit_check_tools_init() {
117   noit_check_tools_shared_init();
118   eventer_name_callback("noit_check_recur_handler", noit_check_recur_handler);
119 }
120
Note: See TracBrowser for help on using the browser.