chiark / gitweb /
merge changes from trunk into our branch; cvs up -j branchpoint-hostside-wip-2006...
[trains.git] / hostside / daemons.h
1 /*
2  * declarations common to
3  *  - realtime daemon
4  *  - multiplexer daemon
5  */
6
7 #ifndef DAEMONS_H
8 #define DAEMONS_H
9
10 #include <oop.h>
11 #include <oop-read.h>
12
13 #include "common.h"
14
15 typedef struct CommandInput CommandInput;
16
17 /*---------- from obc.c ----------*/
18
19 typedef struct OutBuffer OutBuffer;
20 typedef struct OutBufferChain OutBufferChain;
21 typedef void OutBufferError(OutBufferChain*, const char *e1, const char *e2
22                             /* on error: both e1 and e2 non-0. say `$e1: $e2'
23                              * on eof: both e1 and e2 =0. */);
24
25 struct OutBufferChain {
26   /* set by user: */
27   char *desc;
28   int fd;
29   OutBufferError *error;
30   /* set/used by obc_..., oprintf, etc., only */
31   int done_of_head;
32   struct { OutBuffer *head, *tail; } obs;
33 };
34
35 void obc_init(OutBufferChain *ch);
36 int obc_tryflush(OutBufferChain *ch);
37  /* returns 0 for all flushed or errno, including particularly EWOULDBLOCK */
38
39 void ovprintf(OutBufferChain *ch, const char *fmt, va_list al)
40      __attribute__((format(printf,2,0)));
41 void oprintf(OutBufferChain *ch, const char *msg, ...)
42      __attribute__((format(printf,2,3)));
43 void owrite(OutBufferChain *ch, const char *data, int l);
44 void voerror(OutBufferChain *ch, const char *fmt, va_list al)
45      __attribute__((format(printf,2,0)));
46
47 /*---------- from cmdinput.c ----------*/
48
49 typedef void CommandInputCallback(ParseState *ps, CommandInput *cmdi);
50
51 struct CommandInput {
52   OutBufferChain out;
53   CommandInputCallback *doline;
54   oop_read *rd;
55 };
56
57 void cmdin_new(CommandInput *cmdi, int readfd);
58   /* fill in cmdi->out's `set by user' fields before calling cmdin_new,
59    * as cmdin_new will call obc_init and will use cmdi->out->fd. */
60
61 /*---------- from eventhelp.c ----------*/
62
63 extern oop_source *events;
64
65 typedef struct TimeoutEvent TimeoutEvent;
66 typedef void TimeoutEventFn(TimeoutEvent*);
67 struct TimeoutEvent {       /* Undefined   Idle      Running     set by   */
68   int running;              /*  any         0         1           toev_   */
69   int duration; /*ms*/      /*  any         any[1]    any[1]      caller  */
70   TimeoutEventFn *callback; /*  any         any       valid[2]    caller  */
71   struct timeval abs;       /*  any         any       valid       toev_   */
72 };  /* [1] duration must be >=0 or -1 when toev_start is called;
73      * [2] callback may be modified while timeout is running;
74      *      value used is that prevailing when timeout happens
75      * when the timeout happens, TimeoutEvent's state goes from R to I
76      * and then callback member is read and the function called
77      */
78
79 void toev_init(TimeoutEvent*);    /* U -> I */
80 void toev_start(TimeoutEvent*);   /* IR -> R; reads duration */
81   /* if duration is -1 then is same as toev_stop */
82 void toev_stop(TimeoutEvent*);    /* IR -> I */
83
84 #endif /*DAEMONS_H*/