summaryrefslogtreecommitdiffstats
path: root/c/src/libnetworking/rtems_webserver/websuemf.c
blob: 2315d9d234eba4b393baa7a5160d35cc151d99d8 (plain) (blame)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * websuemf.c -- GoAhead Micro Embedded Management Framework
 *
 * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved.
 *
 * See the file "license.txt" for usage and redistribution license requirements
 */

/********************************** Description *******************************/

/*
 *	This modules provides compatibility with the full GoAhead EMF.
 */

/*********************************** Includes *********************************/

#include	"ejIntrn.h"
#include	"wsIntrn.h"

/*********************************** Defines **********************************/

/*
 *	This structure stores scheduled events.
 */
typedef struct {
	void	(*routine)(void *arg, int id);
	void	*arg;
	time_t	at;
	int		schedid;
} sched_t;

/*********************************** Locals ***********************************/

static sched_t		**sched;
static int			schedMax;

/************************************* Code ***********************************/
/*
 *	Evaluate a script
 */

int scriptEval(int engine, char_t *cmd, char_t **result, int chan)
{
	int		ejid;

	if (engine == EMF_SCRIPT_EJSCRIPT) {
		ejid = (int) chan;
		if (ejEval(ejid, cmd, NULL) ) {
			return 0;
		} else {
			return -1;
		}
	}
	return -1;
}

/******************************************************************************/
/*
 * Compare strings, ignoring case:  normal strcmp return codes.
 *
 *	WARNING: It is not good form to increment or decrement pointers inside a
 *	"call" to tolower et al. These can be MACROS, and have undesired side
 *	effects.
 */

int strcmpci(char_t *s1, char_t *s2)
{
	int		rc;

	a_assert(s1 && s2);
	if (s1 == NULL || s2 == NULL) {
		return 0;
	}

	if (s1 == s2) {
		return 0;
	}

	do {
		rc = gtolower(*s1) - gtolower(*s2);
		if (*s1 == '\0') {
			break;
		}
		s1++;
		s2++;
	} while (rc == 0);
	return rc;
}

/******************************************************************************/
/*
 *	This function is called when a scheduled process time has come.
 */

void TimerProc(int schedid)
{
	sched_t	*s;

	a_assert(0 <= schedid && schedid < schedMax);
	s = sched[schedid];
	a_assert(s);

	(s->routine)(s->arg, s->schedid);
}

/******************************************************************************/
/*
 *	Schedule an event in delay milliseconds time. We will use 1 second
 *	granularity for webServer.
 */

int emfSchedCallback(int delay, emfSchedProc *proc, void *arg)
{
	sched_t	*s;
	int		schedid;

	if ((schedid = hAllocEntry((void***) &sched, &schedMax,
		sizeof(sched_t))) < 0) {
		return -1;
	}
	s = sched[schedid];
	s->routine = proc;
	s->arg = arg;
	s->schedid = schedid;

/*
 *	Round the delay up to seconds.
 */
	s->at = ((delay + 500) / 1000) + time(0);

	return schedid;
}

/******************************************************************************/
/*
 *	Reschedule to a new delay.
 */

void emfReschedCallback(int schedid, int delay)
{
	sched_t	*s;

	if (sched == NULL || schedid == -1 || schedid >= schedMax || 
		(s = sched[schedid]) == NULL) {
		return;
	}
	s->at = ((delay + 500) / 1000) + time(0);
}

/******************************************************************************/

void emfUnschedCallback(int schedid)
{
	sched_t	*s;

	if (sched == NULL || schedid == -1 || schedid >= schedMax || 
		(s = sched[schedid]) == NULL) {
		return;
	}
	bfree(B_L, s);
	schedMax = hFree((void***) &sched, schedid);
}

/******************************************************************************/
/*
 *	Take the tasks off the queue in a round robin fashion.
 */

void emfSchedProcess()
{
	sched_t		*s;
	int			schedid;
	static int	next = 0;	

/*
 *	If schedMax is 0, there are no tasks scheduled, so just return.
 */
	if (schedMax <= 0) {
		return;
	}

/*
 *	If next >= schedMax, the schedule queue was reduced in our absence
 *	so reset next to 0 to start from the begining of the queue again.
 */
	if (next >= schedMax) {
		next = 0;
	}

	schedid = next;
	for (;;) {
		if ((s = sched[schedid]) != NULL &&	(int)s->at <= (int)time(0)) {
			TimerProc(schedid);
			next = schedid + 1;
			return;
		}
		if (++schedid >= schedMax) {
			schedid = 0;
		}
		if (schedid == next) {
/*
 *			We've gone all the way through the queue without finding 
 *			anything to do so just return.
 */
			return;
		}
	}
}

/******************************************************************************/