File: Trigger.c%2B%2B

package info (click to toggle)
hylafax 1%3A4.1.1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,400 kB
  • ctags: 7,270
  • sloc: sh: 15,895; ansic: 12,661; makefile: 1,439; cpp: 850
file content (424 lines) | stat: -rw-r--r-- 11,922 bytes parent folder | download | duplicates (2)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*	$Id: Trigger.c++,v 1.2 1999/06/13 07:41:05 robert Exp $ */
/*
 * Copyright (c) 1995-1996 Sam Leffler
 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
 * HylaFAX is a trademark of Silicon Graphics
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */
#include "faxApp.h"
#include "Trigger.h"
#include "TriggerRef.h"
#include "HylaClient.h"
#include "Sys.h"
#include "Job.h"
#include "Modem.h"
#include "StackBuffer.h"
#include "config.h"

#include <ctype.h>
#include <errno.h>

/*
 * Trigger Support.
 */
#define	EventClass(e)	(e>>4)

u_int	Trigger::tidFree[TRIGGER_MAXWDS];
u_int	Trigger::tidRotor = 0;
Trigger* Trigger::triggers[TRIGGER_MAXTID];
QLink	Trigger::wildcards[TRIGGER_MAXEVENT>>4];

Trigger::Trigger(trid_t t, const fxStr& fifoName) : tid(t)
{
    refs = 0;
    interests[JOB_BASE>>4]   = 0;
    interests[SEND_BASE>>4]  = 0;
    interests[RECV_BASE>>4]  = 0;
    interests[MODEM_BASE>>4] = 0;
    client = &HylaClient::getClient(fifoName);
    client->inc();

    triggers[tid] = this;		// register in master table
}

Trigger::~Trigger()
{
    if (refs > 0)
	purgeWildRefs();
    if (refs != 0)			// NB: should be no other references
	logError("Trigger %u deleted with %u references", tid, refs);
    /*
     * Remove references to this trigger from the master
     * table and clear the tid from the allocation bitmap.
     * We release the reference on the client and if this
     * is the last reference to the client we purge it.
     */
    triggers[tid] = NULL;
    // NB: we mimic the logic below to avoid byte-order issues
    u_int i = tid / TRIGGER_BPW;
    ((u_char*) &tidFree[i])[i>>3] &= ~(1<<(i&7));
    client->dec();
}

/*
 * Remove all references to this that appear
 * on a reference list (used for wildcards).
 */
void
Trigger::purgeWildRefs()
{
    for (u_int i = 0; i < EventClass(TRIGGER_MAXEVENT) && refs > 0; i++) {
	QLink& w = wildcards[i];
	if (!w.isEmpty())
	    TriggerRef::purge(w, this);
    }
}

/*
 * Delete all the triggers associated
 * with the specified client.
 */
void
Trigger::purgeClient(HylaClient* hc)
{
    u_int refs = hc->refs;
    for (u_int w = 0; w < TRIGGER_MAXWDS; w++) {
	if (Trigger::tidFree[w] != 0) {
	    u_int n = TRIGGER_BPW-1; 
	    u_int i = w*TRIGGER_BPW;
	    do {
		Trigger* t = triggers[i++];
		if (t && t->client == hc) {
		    if (t->cancel())
			delete t;
		    if (--refs == 0)		// found all triggers
			return;
		    // check if this block is now empty
		    if (Trigger::tidFree[w] == 0)
			break;
		}
	    } while (--n);
	}
    }
    logError("Lost trigger for %s", (const char*) hc->fifoName);
}

/*
 * Create a trigger for the client process listening on
 * the specified FIFO.  The interests are given by the
 * specification string.
 */
void
Trigger::create(const fxStr& fifoName, const char* spec)
{
    trid_t tid = tidNextFree();
    if (tid == TRIGGER_MAXTID) {
	HylaClient::getClient(fifoName).send("T!", 3);
	logError("TRIGGER: tid table overflow");
    } else {
	Trigger* tr = new Trigger(tid, fifoName);
	if (!tr->parse(spec)) {
	    tr->send("T!");
	    delete tr;
	} else
	    tr->send("T*%u", tid);
    }
}

/*
 * Return a free trigger id if one is available.
 * If the table is full return TRIGGER_MAXTID
 */
trid_t
Trigger::tidNextFree()
{
    u_int r = tidRotor;
    if (tidFree[r] == (u_int) -1) {
	/*
	 * This word is full, move the rotor
	 * forward looking for a word with space.
	 */
	r = (r+1) % TRIGGER_MAXWDS;
	while (r != tidRotor && tidFree[r] == (u_int) -1)
	    r = (r+1) % TRIGGER_MAXWDS;
	if (r == tidRotor)
	    return (TRIGGER_MAXTID);
	tidRotor = r;
    }
    static u_char ffc[256] = {
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
        0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 8,
    };
    // locate free id in bitmask
    u_char* bp = (u_char*) &tidFree[r];
    r *= TRIGGER_BPW;
    if (bp[0] != 0xff) { bp += 0; r +=  0; } else
    if (bp[1] != 0xff) { bp += 1; r +=  8; } else
    if (bp[2] != 0xff) { bp += 2; r += 16; } else
    		       { bp += 3, r += 24; }
    u_char b = ffc[bp[0]];
    bp[0] |= 1<<b;
    return (r+b);
}

/*
 * Parse a trigger specification.  Syntax is:
 *
 *   [<class>['<'id'>'][<mask>|'*']]*
 *
 * <class> defines a class of events and is one of:
 *
 *   J	job-related events
 *   S	send-related events
 *   R	receive-related events
 *   M	modem-related events
 *
 * <mask> is a 4-hex-digit mask of trigger events as defined
 * in the Trigger class.  Bit 0 corresponds to <class>_BASE,
 * bit 1 to <class>_BASE+1, etc.  If '*' is specified then any
 * event in the class is matched.
 *
 * An <id> can be used to restrict matches to a specific
 * job or modem.  Eventually this will need to be generalized
 * for job groups.
 *
 * Thus an example specification that would catch any event
 * for the modem on ttyf2 would be ``M<ttyf2>*'', and to be
 * notified when job 1932 is requeued or completes one would
 * use ``J<1932>4c60''.
 */
bool
Trigger::parse(const char* spec0)
{
    const char* spec = spec0;
    while (*spec) {
	u_int base;
	switch (spec[0]) {
	case 'J':	base = JOB_BASE; break;
	case 'S':	base = SEND_BASE; break;
	case 'R':	base = RECV_BASE; break;
	case 'M':	base = MODEM_BASE; break;
	default:	spec++; continue;
	}
	const char* cp = spec+1;
	fxStr id;
	if (cp[0] == '<') {
	    const char* tp;
	    for (tp = ++cp; *tp != '>'; tp++)
		if (*tp == '\0') {			// XXX syntax error
		    syntaxError(spec0, "missing '>'");
		    return (false);
		}
	    id = fxStr(cp, tp-cp);
	    cp = tp+1;
	}
	int c = *cp++;
	u_short& m = interests[base>>4];
	if (m != 0) {
	    syntaxError(spec0, "interests conflict");
	    return (false);
	}
	if (c == '*') {
	    m = 0xffff;
	} else if (isxdigit(c)) {
	    u_int v = 0;
	    for (u_int i = 0; i < 4; i++) {
		u_int bits = isdigit(c) ? c-'0' :
		    (islower(c) ? 10+(c-'a') : 10+(c-'A'));
		v = (v<<4) | bits;
		c = *cp++;
	    } 
	    m = v;
	} else {
	     syntaxError(spec0, "non-hex event mask");
	     return (false);
	}
	TriggerRef* tr = new TriggerRef(*this);
	/*
	 * Place the trigger reference on the appropriate list.
	 * If no modem or job is specified then we match any
	 * job/modem by placing the reference on the wildcards list.
	 * Otherwise we locate the job or modem and hook this
	 * reference to the list that hangs off the object.
	 */
	if (id == "") {
	    tr->insert(wildcards[EventClass(base)]);
	} else if (base == MODEM_BASE || base == RECV_BASE) {
	    /*
	     * NB: you can install a trigger on a modem before
	     *     the modem ``exists''; maybe this is bad?
	     */
	    tr->insert(Modem::getModemByID(id).triggers);
	} else if (base == JOB_BASE || base == SEND_BASE) {
	    Job* job = Job::getJobByID(id);
	    if (!job) {
		logError("TRIGGER: job %s does not exist", (const char*) id);
		return (false);
	    }
	    tr->insert(job->triggers);
	}
	spec = cp;
    }
    return (true);
}
void
Trigger::syntaxError(const char* spec, const char* msg)
{
    logError("TRIGGER: syntax error, %s in \"%s\"", msg, spec);
}

/*
 * Cancel (delete) the trigger with the specified id.
 */
bool
Trigger::cancel(const char* cp)
{
    trid_t tid = (trid_t) strtoul(cp, NULL, 10);
    if (tid < TRIGGER_MAXTID) {
	Trigger* t = triggers[tid];
	if (t) {
	    if (t->cancel())
		delete t;
	    return (true);
	}
    }
    return (false);
}

/*
 * Cancel a trigger.  Because a trigger may have references
 * scattered many places this can be hard.  If all the
 * references are on the wildcard lists then we can just
 * remove those and delete it.  Otherwise have to just
 * clear all the interests (so no future events will be
 * posted to the client) and wait for the object holding
 * the reference to go away.
 */
bool
Trigger::cancel()
{
    purgeWildRefs();			// wildcard references
    if (refs == 0)
	return (true);
    if (interests[MODEM_BASE>>4] != 0) {
	/*
	 * Must explicitly search and purge references
	 * associated with a modem since modems are too
	 * long-lived to wait for them to be deleted.
	 */
	for (ModemIter iter(Modem::list); iter.notDone(); iter++) {
	    TriggerRef::purge(iter.modem().triggers, this);
	    if (refs == 0)
		return (true);
	}
    }
    // clear interests so no more messages are sent
    memset(interests, 0, sizeof (interests));
    return (false);
}

/*
 * Printf-like interface to send a trigger message.
 */
void
Trigger::send(const char* fmt ...)
{
    va_list ap;
    va_start(ap, fmt);
    fxStr msg(fxStr::vformat(fmt, ap));
    (void) client->send(msg, msg.length()+1);
    va_end(ap);
}

/*
 * Post an event to interested parties.
 */
void
Trigger::post(TriggerEvent e, const QLink& tr, const QLink& any, fxStackBuffer& msg)
{
    TriggerMsgHeader& hdr = *((TriggerMsgHeader*) &msg[0]);
    hdr.length = (u_short) msg.getLength();
    hdr.event = (u_short) e;
    hdr.tstamp = Sys::now();

    u_short mask = 1<<(e&15);
    const QLink* ql;
    for (ql = tr.next; ql != &tr; ql = ql->next) {
	TriggerRef* tr = (TriggerRef*) ql;
	if (tr->ref.interests[e>>4] & mask) {
	    HylaClient& client = *tr->ref.client;
	    hdr.seqnum = client.getSeqnum();
	    client.send(msg, hdr.length);
	}
    }
    for (ql = any.next; ql != &any; ql = ql->next) {
	TriggerRef* tr = (TriggerRef*) ql;
	if (tr->ref.interests[e>>4] & mask) {
	    HylaClient& client = *tr->ref.client;
	    hdr.seqnum = client.getSeqnum();
	    client.send(msg, hdr.length);
	}
    }
}

static TriggerMsgHeader hdr = { '!', ' ' };

void
Trigger::post(TriggerEvent e, const Job& job, const char* info)
{
    const QLink& any = wildcards[EventClass(e)];
    if (!job.triggers.isEmpty() || !any.isEmpty()) {
	fxStackBuffer msg;
	msg.put((const char*) &hdr, sizeof (hdr));
	job.encode(msg);
	if (info)
	    msg.put(info);
	post(e, job.triggers, any, msg);
    }
}
void
Trigger::post(TriggerEvent e, const Modem& modem, const char* info)
{
    const QLink& any = wildcards[EventClass(e)];
    if (!modem.triggers.isEmpty() || !any.isEmpty()) {
	fxStackBuffer msg;
	msg.put((const char*) &hdr, sizeof (hdr));
	if (info)
	    msg.put(info);
	else
	    modem.encode(msg);
	post(e, modem.triggers, any, msg);
    }
}