Debian Codegraph @

hfaxd/Trigger.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "port.h"
27 #include "Sys.h"
28 #include "config.h"
29
30 #include "HylaFAXServer.h"
31 #include "Dispatcher.h"
32 #include "ModemExt.h"
33 #include "JobExt.h"
34 #include "FaxRecvInfo.h"
35 #include "FaxSendInfo.h"
36
37 /*
38 * Setup a trigger to monitor server events and
39 * return ascii/binary event information on a
40 * data connection.
41 */
42 void
43 HylaFAXServer::triggerCmd(const char* fmt ...)
44 {
45 /*
46 * We setup the connection before registering the
47 * trigger to reduce lost event reports. If we setup
48 * the trigger first then events could come in and
49 * be discarded while we were setting up the data
50 * connection. Either way we may still lose some
51 * events if, for example, the client wants to
52 * fully monitor a submitted job using a single
53 * control channel. The only way to reliably do
54 * this is to register a trigger before a job is
55 * submitted (sigh).
56 */
57 int code;
58 FILE* dout = openDataConn("w", code);
59 if (dout != NULL) {
60 va_list ap;
61 va_start(ap, fmt);
62 trigSpec = fxStr::vformat(fmt, ap);
63 va_end(ap);
64 reply(code, "%s for trigger \"%s\".", dataConnMsg(code),
65 (const char*) trigSpec);
66 state |= S_LOGTRIG; // force events to be reported
67 fxStr emsg;
68 if (loadTrigger(emsg)) {
69 if (setjmp(urgcatch) == 0) {
70 state |= S_TRANSFER;
71 /*
72 * The only way out of this is a client
73 * ABORt request or if the control channel
74 * connection is broken.
75 */
76 for (;;)
77 Dispatcher::instance().dispatch();
78 } else
79 perror_reply(426, "Data connection", errno);
80 state &= ~S_TRANSFER;
81 (void) cancelTrigger(emsg);
82 } else
83 reply(504, "Cannot register trigger: %s.", (const char*) emsg);
84 state &= ~S_LOGTRIG;
85 closeDataConn(dout);
86 }
87 }
88
89 /*
90 * Create a trigger for the specified events
91 * and return the identifier sent back by the
92 * scheduler.
93 */
94 bool
95 HylaFAXServer::newTrigger(fxStr& emsg, const char* fmt, ...)
96 {
97 va_list ap;
98 va_start(ap, fmt);
99 bool b = vnewTrigger(emsg, fmt, ap);
100 va_end(ap);
101 return (b);
102 }
103 bool
104 HylaFAXServer::vnewTrigger(fxStr& emsg, const char* fmt, va_list ap)
105 {
106 trigSpec = fxStr::vformat(fmt, ap);
107 return (loadTrigger(emsg));
108 }
109
110 /*
111 * Send the current trigger specification to the scheduler
112 * and stash the returned trigger ID. This is used to
113 * create triggers the first time as well as to re-load an
114 * active trigger if the scheduler is restarted.
115 */
116 bool
117 HylaFAXServer::loadTrigger(fxStr& emsg)
118 {
119 if (sendQueuerACK(emsg, "T%s", (const char*) trigSpec) &&
120 fifoResponse.length() > 2) {
121 tid = atoi(&fifoResponse[2]);
122 return (true);
123 } else
124 return (false);
125 }
126
127 /*
128 * Inform the spooler that it should cancel
129 * a previously created trigger.
130 */
131 bool
132 HylaFAXServer::cancelTrigger(fxStr& emsg)
133 {
134 trigSpec = ""; // avoid spontaneous reloads
135 return sendQueuer(emsg, "D%u", tid);
136 }
137
138 /*
139 * Handle a trigger event received while waiting
140 * for a job or group of jobs. The status information
141 * is sent back to the client over the data channel
142 * if S_LOGTRIG is set in the server state.
143 */
144 void
145 HylaFAXServer::triggerEvent(const TriggerMsgHeader& h, const char* cp)
146 {
147 #define EventType(e) ((e)>>4)
148 int evt = EventType(h.event);
149 if (evt == EventType(JOB_BASE) || evt == EventType(SEND_BASE)) {
150 JobExt job;
151 cp = job.decode(cp);
152 /*
153 * Propagate job state for clients that are doing
154 * things like waiting for a job to complete. This
155 * may be fairly costly if someone traces all jobs
156 * as it'll cause the in-memory job database to grow.
157 */
158 fxStr emsg;
159 Job* jp = findJob(job.jobid, emsg);
160 if (jp) {
161 jp->tts = job.tts;
162 jp->killtime = job.killtime;
163 jp->state = job.state;
164 jp->pri = job.pri;
165 jp->commid = job.commid;
166 }
167 if (IS(LOGTRIG) && type == TYPE_A) {
168 if (evt == EventType(JOB_BASE))
169 logJobEventMsg(h, job);
170 else
171 logSendEventMsg(h, job, cp);
172 }
173 } else if (evt == EventType(MODEM_BASE) || evt == EventType(RECV_BASE)) {
174 ModemExt modem;
175 cp = modem.decode(cp);
176
177 if (evt == EventType(RECV_BASE))
178 {
179 FaxRecvInfo ri;
180 ri.decode(cp);
181 /*
182 * Update/record receive queue status.
183 */
184 RecvInfo* rip = recvq[ri.qfile];
185 if (!rip)
186 recvq[ri.qfile] = rip = new RecvInfo(ri.qfile);
187 if (rip->recvTime == 0)
188 rip->recvTime = h.tstamp;
189 if (h.event == Trigger::RECV_END) {
190 rip->beingReceived = false;
191 rip->reason = ri.reason;
192 } else
193 rip->beingReceived = true;
194 rip->sender = ri.sender;
195 rip->subaddr = ri.subaddr;
196 rip->npages = ri.npages;
197 rip->time = h.tstamp - rip->recvTime;
198 rip->commid = ri.commid;
199 if (IS(LOGTRIG) && type == TYPE_A)
200 logRecvEventMsg(h, ri, cp);
201 } else if (IS(LOGTRIG) && type == TYPE_A)
202 logModemEventMsg(h, modem, cp);
203 } else {
204 logError("Unrecognized trigger event message %u.", h.event);
205 return;
206 }
207 if (IS(LOGTRIG) && type == TYPE_I) {
208 /*
209 * Binary trigger logging causes the raw event
210 * stream to pass through to the user. This is
211 * probably not a great idea as it causes clients
212 * to be aware of the server's internal message
213 * formats but for some applications it may be
214 * appropriate. We support it now to better
215 * understand the implications.
216 */
217 (void) Sys::write(data, cp - sizeof (h), h.length);
218 }
219 #undef EventType
220 }
221
222 /*
223 * Ascii Trigger Event Logging Support.
224 */
225 #define unknownEvent "Unknown event" // XXX for HP CC
226
227 void
228 HylaFAXServer::logEventMsg(const TriggerMsgHeader& h, fxStr& msg)
229 {
230 struct tm& tm = *cvtTime(h.tstamp);
231 msg.insert(fxStr::format("%02d%02d%02d %d "
232 , tm.tm_hour
233 , tm.tm_min
234 , tm.tm_sec
235 , 100+h.event
236 ));
237 msg.append("\r\n");
238 (void) Sys::write(data, msg, msg.length());
239 }
240
241 /*
242 * Format and log a job event.
243 */
244 void
245 HylaFAXServer::logJobEventMsg(const TriggerMsgHeader& h, const JobExt& job)
246 {
247 static const char* jobNames[16] = {
248 "Created",
249 "Suspended",
250 "Ready to send",
251 "Sleeping awaiting time-to-send",
252 "Marked dead",
253 "Being processed by scheduler",
254 "Corpus reaped",
255 "Activated",
256 "Rejected",
257 "Killed",
258 "Blocked by another job",
259 "Delayed by time-of-day restriction or similar",
260 "Parameters altered",
261 "Timed out",
262 "Preparation started",
263 "Preparation finished",
264 };
265 fxStr msg = fxStr::format("JOB %s (dest %s pri %u com %s): %s"
266 , (const char*) job.jobid
267 , (const char*) job.dest
268 , job.pri
269 , (const char*) job.commid
270 , jobNames[h.event&0xf]
271 );
272 logEventMsg(h, msg);
273 }
274
275 static void
276 addParams(fxStr& msg, const Class2Params& params)
277 {
278 msg.append(fxStr::format("<%s, %s, %s, %s, %s>"
279 , (params.ln == LN_A4 ? "A4" : params.ln == LN_B4 ? "B4" : "INF")
280 , params.verticalResName()
281 , params.dataFormatName()
282 , params.bitRateName()
283 , params.scanlineTimeName()
284 ));
285 }
286
287 /*
288 * Format and log a send event.
289 */
290 void
291 HylaFAXServer::logSendEventMsg(const TriggerMsgHeader& h, const JobExt& job, const char* cp)
292 {
293 fxStr msg = fxStr::format("JOB %s (dest %s pri %u com %s): SEND FAX: "
294 , (const char*) job.jobid
295 , (const char*) job.dest
296 , job.pri
297 , (const char*) job.commid
298 );
299 if (h.event != Trigger::SEND_POLLRCVD) {
300 static const char* sendNames[16] = {
301 "Begin attempt", // SEND_BEGIN
302 "Call placed (off-hook)", // SEND_CALL
303 "Connected to remote device", // SEND_CONNECTED
304 unknownEvent, // SEND_PAGE
305 unknownEvent, // SEND_DOC
306 unknownEvent, // SEND_POLLRCVD
307 unknownEvent, // SEND_POLLDONE
308 "Finished attempt", // SEND_END
309 "Reformat documents because of capabilities mismatch",
310 "Requeue job", // SEND_REQUEUE
311 "Job completed successfully", // SEND_DONE
312 unknownEvent,
313 unknownEvent,
314 unknownEvent,
315 unknownEvent,
316 unknownEvent
317 };
318 FaxSendInfo si;
319 switch (h.event) {
320 case Trigger::SEND_PAGE: // page sent
321 si.decode(cp);
322 msg.append(fxStr::format("Page %u sent in %s (file %s) "
323 , si.npages
324 , fmtTime(si.time)
325 , (const char*) si.qfile
326 ));
327 addParams(msg, si.params);
328 break;
329 case Trigger::SEND_DOC: // document sent
330 si.decode(cp);
331 msg.append(fxStr::format("Document sent in %s (file %s)"
332 , fmtTime(si.time)
333 , (const char*) si.qfile
334 ));
335 break;
336 case Trigger::SEND_POLLDONE: // polling operation done
337 si.decode(cp);
338 msg.append(fxStr::format("Poll completed in %s (file %s)"
339 , fmtTime(si.time)
340 , (const char*) si.qfile
341 ));
342 break;
343 default:
344 msg.append(sendNames[h.event&15]);
345 break;
346 }
347 } else {
348 FaxRecvInfo ri;
349 ri.decode(cp);
350 msg.append(fxStr::format("Recv polled document from %s, %u pages in %s, file %s"
351 , (const char*) ri.sender
352 , ri.npages
353 , fmtTime((time_t) ri.time)
354 , (const char*) ri.qfile
355 ));
356 }
357 logEventMsg(h, msg);
358 }
359
360 /*
361 * Format and log a modem event.
362 */
363 void
364 HylaFAXServer::logModemEventMsg(const TriggerMsgHeader& h, const ModemExt& modem, const char* cp)
365 {
366 static const char* modemNames[16] = {
367 "Assigned to job",
368 "Released by job",
369 "Marked down",
370 "Marked ready",
371 "Marked busy",
372 "Considered wedged",
373 "In-use by an outbound job",
374 "Inbound data call begin",
375 "Inbound data call completed",
376 "Inbound voice call begin",
377 "Inbound voice call completed",
378 "Caller-id information: ",
379 unknownEvent,
380 unknownEvent,
381 unknownEvent,
382 unknownEvent
383 };
384 fxStr msg = fxStr::format("MODEM %s: %s"
385 , (const char*) modem.devID
386 , modemNames[h.event&15]
387 );
388 if (h.event == Trigger::MODEM_CID)
389 msg.append(cp);
390 logEventMsg(h, msg);
391 }
392
393 /*
394 * Format and log a facsimile receive event.
395 */
396 void
397 HylaFAXServer::logRecvEventMsg(const TriggerMsgHeader& h, const FaxRecvInfo& ri, const char*)
398 {
399 fxStr msg = fxStr::format("RECV FAX: ");
400 switch (h.event) {
401 case Trigger::RECV_BEGIN:
402 msg.append("Call started");
403 break;
404 case Trigger::RECV_END:
405 msg.append("Call ended");
406 break;
407 case Trigger::RECV_START:
408 msg.append(fxStr::format("Session started (com %s), TSI \"%s\" "
409 , (const char*) ri.commid
410 , (const char*) ri.sender
411 ));
412 addParams(msg, ri.params);
413 break;
414 case Trigger::RECV_PAGE:
415 msg.append(fxStr::format("From %s (com %s), page %u in %s "
416 , (const char*) ri.sender
417 , (const char*) ri.commid
418 , ri.npages
419 , fmtTime((time_t) ri.time)
420 ));
421 addParams(msg, ri.params);
422 break;
423 case Trigger::RECV_DOC:
424 msg.append(fxStr::format("From %s (com %s), %u pages in %s, file %s"
425 , (const char*) ri.sender
426 , (const char*) ri.commid
427 , ri.npages
428 , fmtTime((time_t) ri.time)
429 , (const char*) ri.qfile
430 ));
431 break;
432 default:
433 msg.append(unknownEvent);
434 break;
435 }
436 logEventMsg(h, msg);
437 }