1 | /* |
---|
2 | * websuemf.c -- GoAhead Micro Embedded Management Framework |
---|
3 | * |
---|
4 | * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved. |
---|
5 | * |
---|
6 | * See the file "license.txt" for usage and redistribution license requirements |
---|
7 | */ |
---|
8 | |
---|
9 | /********************************** Description *******************************/ |
---|
10 | |
---|
11 | /* |
---|
12 | * This modules provides compatibility with the full GoAhead EMF. |
---|
13 | */ |
---|
14 | |
---|
15 | /*********************************** Includes *********************************/ |
---|
16 | |
---|
17 | #include "ejIntrn.h" |
---|
18 | #include "wsIntrn.h" |
---|
19 | |
---|
20 | /*********************************** Defines **********************************/ |
---|
21 | |
---|
22 | /* |
---|
23 | * This structure stores scheduled events. |
---|
24 | */ |
---|
25 | typedef struct { |
---|
26 | void (*routine)(void *arg, int id); |
---|
27 | void *arg; |
---|
28 | time_t at; |
---|
29 | int schedid; |
---|
30 | } sched_t; |
---|
31 | |
---|
32 | /*********************************** Locals ***********************************/ |
---|
33 | |
---|
34 | static sched_t **sched; |
---|
35 | static int schedMax; |
---|
36 | |
---|
37 | /************************************* Code ***********************************/ |
---|
38 | /* |
---|
39 | * Evaluate a script |
---|
40 | */ |
---|
41 | |
---|
42 | int scriptEval(int engine, char_t *cmd, char_t **result, int chan) |
---|
43 | { |
---|
44 | int ejid; |
---|
45 | |
---|
46 | if (engine == EMF_SCRIPT_EJSCRIPT) { |
---|
47 | ejid = (int) chan; |
---|
48 | if (ejEval(ejid, cmd, NULL) ) { |
---|
49 | return 0; |
---|
50 | } else { |
---|
51 | return -1; |
---|
52 | } |
---|
53 | } |
---|
54 | return -1; |
---|
55 | } |
---|
56 | |
---|
57 | /******************************************************************************/ |
---|
58 | /* |
---|
59 | * Compare strings, ignoring case: normal strcmp return codes. |
---|
60 | * |
---|
61 | * WARNING: It is not good form to increment or decrement pointers inside a |
---|
62 | * "call" to tolower et al. These can be MACROS, and have undesired side |
---|
63 | * effects. |
---|
64 | */ |
---|
65 | |
---|
66 | int strcmpci(char_t *s1, char_t *s2) |
---|
67 | { |
---|
68 | int rc; |
---|
69 | |
---|
70 | a_assert(s1 && s2); |
---|
71 | if (s1 == NULL || s2 == NULL) { |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | if (s1 == s2) { |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | do { |
---|
80 | rc = gtolower(*s1) - gtolower(*s2); |
---|
81 | if (*s1 == '\0') { |
---|
82 | break; |
---|
83 | } |
---|
84 | s1++; |
---|
85 | s2++; |
---|
86 | } while (rc == 0); |
---|
87 | return rc; |
---|
88 | } |
---|
89 | |
---|
90 | /******************************************************************************/ |
---|
91 | /* |
---|
92 | * This function is called when a scheduled process time has come. |
---|
93 | */ |
---|
94 | |
---|
95 | void TimerProc(int schedid) |
---|
96 | { |
---|
97 | sched_t *s; |
---|
98 | |
---|
99 | a_assert(0 <= schedid && schedid < schedMax); |
---|
100 | s = sched[schedid]; |
---|
101 | a_assert(s); |
---|
102 | |
---|
103 | (s->routine)(s->arg, s->schedid); |
---|
104 | } |
---|
105 | |
---|
106 | /******************************************************************************/ |
---|
107 | /* |
---|
108 | * Schedule an event in delay milliseconds time. We will use 1 second |
---|
109 | * granularity for webServer. |
---|
110 | */ |
---|
111 | |
---|
112 | int emfSchedCallback(int delay, emfSchedProc *proc, void *arg) |
---|
113 | { |
---|
114 | sched_t *s; |
---|
115 | int schedid; |
---|
116 | |
---|
117 | if ((schedid = hAllocEntry((void***) &sched, &schedMax, |
---|
118 | sizeof(sched_t))) < 0) { |
---|
119 | return -1; |
---|
120 | } |
---|
121 | s = sched[schedid]; |
---|
122 | s->routine = proc; |
---|
123 | s->arg = arg; |
---|
124 | s->schedid = schedid; |
---|
125 | |
---|
126 | /* |
---|
127 | * Round the delay up to seconds. |
---|
128 | */ |
---|
129 | s->at = ((delay + 500) / 1000) + time(0); |
---|
130 | |
---|
131 | return schedid; |
---|
132 | } |
---|
133 | |
---|
134 | /******************************************************************************/ |
---|
135 | /* |
---|
136 | * Reschedule to a new delay. |
---|
137 | */ |
---|
138 | |
---|
139 | void emfReschedCallback(int schedid, int delay) |
---|
140 | { |
---|
141 | sched_t *s; |
---|
142 | |
---|
143 | if (sched == NULL || schedid == -1 || schedid >= schedMax || |
---|
144 | (s = sched[schedid]) == NULL) { |
---|
145 | return; |
---|
146 | } |
---|
147 | s->at = ((delay + 500) / 1000) + time(0); |
---|
148 | } |
---|
149 | |
---|
150 | /******************************************************************************/ |
---|
151 | |
---|
152 | void emfUnschedCallback(int schedid) |
---|
153 | { |
---|
154 | sched_t *s; |
---|
155 | |
---|
156 | if (sched == NULL || schedid == -1 || schedid >= schedMax || |
---|
157 | (s = sched[schedid]) == NULL) { |
---|
158 | return; |
---|
159 | } |
---|
160 | bfree(B_L, s); |
---|
161 | schedMax = hFree((void***) &sched, schedid); |
---|
162 | } |
---|
163 | |
---|
164 | /******************************************************************************/ |
---|
165 | /* |
---|
166 | * Take the tasks off the queue in a round robin fashion. |
---|
167 | */ |
---|
168 | |
---|
169 | void emfSchedProcess() |
---|
170 | { |
---|
171 | sched_t *s; |
---|
172 | int schedid; |
---|
173 | static int next = 0; |
---|
174 | |
---|
175 | /* |
---|
176 | * If schedMax is 0, there are no tasks scheduled, so just return. |
---|
177 | */ |
---|
178 | if (schedMax <= 0) { |
---|
179 | return; |
---|
180 | } |
---|
181 | |
---|
182 | /* |
---|
183 | * If next >= schedMax, the schedule queue was reduced in our absence |
---|
184 | * so reset next to 0 to start from the begining of the queue again. |
---|
185 | */ |
---|
186 | if (next >= schedMax) { |
---|
187 | next = 0; |
---|
188 | } |
---|
189 | |
---|
190 | schedid = next; |
---|
191 | for (;;) { |
---|
192 | if ((s = sched[schedid]) != NULL && (int)s->at <= (int)time(0)) { |
---|
193 | TimerProc(schedid); |
---|
194 | next = schedid + 1; |
---|
195 | return; |
---|
196 | } |
---|
197 | if (++schedid >= schedMax) { |
---|
198 | schedid = 0; |
---|
199 | } |
---|
200 | if (schedid == next) { |
---|
201 | /* |
---|
202 | * We've gone all the way through the queue without finding |
---|
203 | * anything to do so just return. |
---|
204 | */ |
---|
205 | return; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | /******************************************************************************/ |
---|