chiark / gitweb /
Merge and end branch-hostside-wip-2008-01-25 PROPERLY; cvs up -j branch-hostside...
[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   int limit; /* 0 means obc_init will set a default */
30   OutBufferError *error;
31   /* set/used by obc_..., oprintf, etc., only */
32   int done_of_head, total;
33   struct { OutBuffer *head, *tail; } obs;
34 };
35
36 void obc_init(OutBufferChain *ch);
37 int obc_tryflush(OutBufferChain *ch);
38  /* returns 0 for all flushed or errno, including particularly EWOULDBLOCK */
39
40 void ovprintf(OutBufferChain *ch, const char *fmt, va_list al)
41      __attribute__((format(printf,2,0)));
42 void oprintf(OutBufferChain *ch, const char *msg, ...)
43      __attribute__((format(printf,2,3)));
44 void owrite(OutBufferChain *ch, const char *data, int l);
45 void voerror(OutBufferChain *ch, const char *fmt, va_list al)
46      __attribute__((format(printf,2,0)));
47
48 /*---------- from cmdinput.c ----------*/
49
50 typedef void CommandInputCallback(ParseState *ps, CommandInput *cmdi);
51
52 struct CommandInput {
53   OutBufferChain out;
54   CommandInputCallback *doline;
55   oop_read *rd;
56 };
57
58 void cmdin_new(CommandInput *cmdi, int readfd);
59   /* fill in cmdi->out's `set by user' fields before calling cmdin_new,
60    * as cmdin_new will call obc_init and will use cmdi->out->fd. */
61
62 /*---------- from eventhelp.c ----------*/
63
64 extern oop_source *events;
65
66 typedef struct TimeoutEvent TimeoutEvent;
67 typedef void TimeoutEventFn(TimeoutEvent*);
68 struct TimeoutEvent {       /* Undefined   Idle      Running     set by   */
69   int running;              /*  any         0         1           toev_   */
70   int duration; /*ms*/      /*  any         any[1]    any[1]      caller  */
71   TimeoutEventFn *callback; /*  any         any       valid[2]    caller  */
72   struct timeval abs;       /*  any         any       valid       toev_   */
73 };  /* [1] duration must be >=0 or -1 when toev_start is called;
74      * [2] callback may be modified while timeout is running;
75      *      value used is that prevailing when timeout happens
76      * when the timeout happens, TimeoutEvent's state goes from R to I
77      * and then callback member is read and the function called
78      */
79
80 void toev_init(TimeoutEvent*);    /* U -> I */
81 void toev_start(TimeoutEvent*);   /* IR -> R; reads duration */
82   /* if duration is -1 then is same as toev_stop */
83 void toev_stop(TimeoutEvent*);    /* IR -> I */
84
85 #endif /*DAEMONS_H*/