EV: expose current event queue state to the RWHO command
[citadel] / citadel / event_client.h
1 /*
2  *
3  * Copyright (c) 1998-2012 by the citadel.org team
4  *
5  *  This program is open source software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License, version 3.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  */
13
14 #ifndef __EVENT_CLIENT_H__
15 #define __EVENT_CLIENT_H__
16 #define EV_COMPAT3 0
17 #include "sysconfig.h"
18 #include <ev.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netdb.h>
22 #include <arpa/nameser.h>
23 #include <ares.h>
24 #include <curl/curl.h>
25
26 #ifndef __ASYNCIO__
27 #define __ASYNCIO__
28 typedef struct AsyncIO AsyncIO;
29 #endif
30 #ifndef __CIT_CONTEXT__
31 #define __CIT_CONTEXT__
32 typedef struct CitContext CitContext;
33 #endif
34
35 typedef enum __eIOState { 
36         eDBQ,
37         eQDBNext,
38         eDBAttach,
39         eDBNext,
40         eDBStop,
41         eDBX,
42         eDBTerm,
43         eIOQ,
44         eIOAttach,
45         eIOConnectSock,
46         eIOAbort,
47         eIOTimeout,
48         eIOConnfail,
49         eIOConnfailNow,
50         eIOConnNow,
51         eIOConnWait,
52         eCurlQ,
53         eCurlStart,
54         eCurlShutdown,
55         eCurlNewIO,
56         eCurlGotIO,
57         eCurlGotData,
58         eCurlGotStatus,
59         eCaresStart,
60         eCaresDoneIO,
61         eCaresFinished,
62         eCaresX,
63         eKill,
64         eExit
65 }eIOState;
66
67 typedef enum _eNextState {
68         eSendDNSQuery,
69         eReadDNSReply,
70
71         eDBQuery,
72
73         eConnect,
74         eSendReply,
75         eSendMore,
76         eSendFile,
77
78         eReadMessage,
79         eReadMore,
80         eReadPayload,
81         eReadFile,
82
83         eTerminateConnection,
84         eAbort
85 }eNextState;
86
87 void SetEVState(AsyncIO *IO, eIOState State);
88
89 typedef eNextState (*IO_CallBack)(AsyncIO *IO);
90 typedef eReadState (*IO_LineReaderCallback)(AsyncIO *IO);
91 typedef void (*ParseDNSAnswerCb)(AsyncIO*, unsigned char*, int);
92 typedef void (*FreeDNSReply)(void *DNSData);
93
94
95 typedef struct __ReadAsyncMsg {
96         StrBuf *MsgBuf;
97         size_t maxlen;          /* maximum message length */
98
99         const char *terminator; /* token signalling EOT */
100         long tlen;
101         int dodot;
102
103         int flushing;
104 /* if we read maxlen, read until nothing more arives and ignore this. */
105
106         int crlf;               /* CRLF newlines instead of LF */
107 } ReadAsyncMsg;
108
109
110 typedef struct _DNSQueryParts {
111         ParseDNSAnswerCb DNS_CB;
112         IO_CallBack PostDNS;
113
114         const char *QueryTYPE;
115         const char *QStr;
116         int DNSStatus;
117         void *VParsedDNSReply;
118         FreeDNSReply DNSReplyFree;
119         void *Data;
120 } DNSQueryParts;
121
122 typedef struct _evcurl_request_data
123 {
124         CURL                    *chnd;
125         struct curl_slist       *headers;
126         char                     errdesc[CURL_ERROR_SIZE];
127
128         int                      attached;
129
130         char                    *PlainPostData;
131         long                     PlainPostDataLen;
132         StrBuf                  *PostData;
133
134         StrBuf                  *ReplyData;
135         long                     httpcode;
136 } evcurl_request_data;
137
138 /* DNS Related */
139 typedef struct __evcares_data {
140         ev_tstamp Start;
141         ev_io recv_event,
142                 send_event;
143         ev_timer timeout;           /* timeout while requesting ips */
144         short int SourcePort;
145
146         struct ares_options Options;
147         ares_channel Channel;
148         DNSQueryParts *Query;
149
150         IO_CallBack Fail;      /* the dns lookup didn't work out. */
151 } evcares_data;
152
153 struct AsyncIO {
154         long ID;
155         ev_tstamp Now;
156         ev_tstamp StartIO;
157         ev_tstamp StartDB;
158         eNextState NextState;
159
160         /* connection related */
161         ParsedURL *ConnectMe;
162
163         /* read/send related... */
164         StrBuf *IOBuf;
165         IOBuffer SendBuf,
166                 RecvBuf;
167
168         FDIOBuffer IOB;
169         /* when sending from / reading into files, this is used. */
170
171         /* our events... */
172         ev_cleanup abort_by_shutdown, /* server wants to go down... */
173                 db_abort_by_shutdown; /* server wants to go down... */
174         ev_timer conn_fail,           /* connection establishing timed out */
175                 rw_timeout;           /* timeout while sending data */
176         ev_idle unwind_stack,         /* get c-ares out of the stack */
177                 db_unwind_stack,      /* wait for next db operation... */
178                 conn_fail_immediate;  /* unwind stack, but fail immediately. */
179         ev_io recv_event,             /* receive data from the client */
180                 send_event,           /* send more data to the client */
181                 conn_event;           /* Connection successfully established */
182
183         StrBuf *ErrMsg; /* if we fail to connect, or lookup, error goes here. */
184
185         /* Citadel application callbacks... */
186         IO_CallBack ReadDone, /* Theres new data to read... */
187                 SendDone,     /* we may send more data */
188                 Terminate,    /* shutting down... */
189                 DBTerminate,  /* shutting down... */
190                 Timeout,      /* Timeout handler;may also be conn. timeout */
191                 ConnFail,     /* What to do when one connection failed? */
192                 ShutdownAbort,/* we're going down. make your piece. */
193                 NextDBOperation; /* Perform Database IO */
194
195         /* if we have linereaders, maybe we want to read more lines before
196          * the real application logic is called? */
197         IO_LineReaderCallback LineReader;
198
199         evcares_data DNS;
200
201         evcurl_request_data HttpReq;
202
203         /* Saving / loading a message async from / to disk */
204         ReadAsyncMsg *ReadMsg;
205         struct CtdlMessage *AsyncMsg;
206         struct recptypes *AsyncRcp;
207
208         /* Context specific data; Hint: put AsyncIO in there */
209         void *Data;        /* application specific data */
210         CitContext *CitContext;  /* Citadel Session context... */
211 };
212
213 typedef struct _IOAddHandler {
214         AsyncIO *IO;
215         IO_CallBack EvAttch;
216 } IOAddHandler;
217
218
219
220 extern int DebugEventLoop;
221 extern int DebugCAres;
222
223 #define EDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (DebugEventLoop != 0))
224
225 #define CCID ((CitContext*)IO->CitContext)->cs_pid
226
227 #define EVQ_syslog(LEVEL, FORMAT, ...)                                  \
228         EDBGLOG (LEVEL) syslog(LEVEL, "IOQ " FORMAT, __VA_ARGS__)
229
230 #define EVQM_syslog(LEVEL, FORMAT)                      \
231         EDBGLOG (LEVEL) syslog(LEVEL, "IO " FORMAT)
232
233 #define EV_syslog(LEVEL, FORMAT, ...)                                   \
234         EDBGLOG (LEVEL) syslog(LEVEL, "IO[%ld]CC[%d] " FORMAT, IO->ID, CCID, __VA_ARGS__)
235
236 #define EVM_syslog(LEVEL, FORMAT)                                       \
237         EDBGLOG (LEVEL) syslog(LEVEL, "IO[%ld]CC[%d] " FORMAT, IO->ID, CCID)
238
239 #define EVNC_syslog(LEVEL, FORMAT, ...)                                 \
240         EDBGLOG (LEVEL) syslog(LEVEL, "IO[%ld] " FORMAT, IO->ID, __VA_ARGS__)
241
242 #define EVNCM_syslog(LEVEL, FORMAT) EDBGLOG (LEVEL) syslog(LEVEL, "IO[%ld]" FORMAT, IO->ID)
243
244
245 #define CDBGLOG() if (DebugCAres != 0)
246 #define CEDBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (DebugCAres != 0))
247 #define EV_DNS_LOG_START(a)                                                     \
248         CDBGLOG () {syslog(LOG_DEBUG, "IO[%ld]CC[%d] + Starting " #a " %s %p FD %d", IO->ID, CCID, __FUNCTION__, &IO->a, IO->a.fd); \
249                     EV_backtrace(IO);}
250
251 #define EV_DNS_LOG_STOP(a)                                                      \
252         CDBGLOG () { syslog(LOG_DEBUG, "IO[%ld]CC[%d] - Stopping " #a " %s %p FD %d", IO->ID, CCID, __FUNCTION__, &IO->a, IO->a.fd); \
253                      EV_backtrace(IO);}
254
255 #define EV_DNS_LOG_INIT(a)                                                      \
256         CDBGLOG () { syslog(LOG_DEBUG, "IO[%ld]CC[%d] * Init " #a " %s %p FD %d", IO->ID, CCID, __FUNCTION__, &IO->a, IO->a.fd); \
257                      EV_backtrace(IO);}
258
259 #define EV_DNS_LOGT_START(a)                                                    \
260         CDBGLOG () { syslog(LOG_DEBUG, "IO[%ld]CC[%d] + Starting " #a " %s %p", IO->ID, CCID, __FUNCTION__, &IO->a); \
261                      EV_backtrace(IO);}
262
263 #define EV_DNS_LOGT_STOP(a)                                                     \
264         CDBGLOG () { syslog(LOG_DEBUG, "IO[%ld]CC[%d] - Stopping " #a " %s %p", IO->ID, CCID, __FUNCTION__, &IO->a); \
265                      EV_backtrace(IO); }
266
267 #define EV_DNS_LOGT_INIT(a)                                                     \
268         CDBGLOG () { syslog(LOG_DEBUG, "IO[%ld]CC[%d] * Init " #a " %p", IO->ID, CCID, &IO->a); \
269                      EV_backtrace(IO);}
270
271 #define EV_DNS_syslog(LEVEL, FORMAT, ...)                               \
272         CEDBGLOG (LEVEL) syslog(LEVEL, "IO[%ld]CC[%d] " FORMAT, IO->ID, CCID, __VA_ARGS__)
273
274 #define EVM_DNS_syslog(LEVEL, FORMAT)                                   \
275         CEDBGLOG (LEVEL) syslog(LEVEL, "IO[%ld]CC[%d] " FORMAT, IO->ID, CCID)
276
277 void FreeAsyncIOContents(AsyncIO *IO);
278
279 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB);
280 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB);
281 void StopDBWatchers(AsyncIO *IO);
282 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB);
283 eNextState QueueCurlContext(AsyncIO *IO);
284
285 eNextState EvConnectSock(AsyncIO *IO,
286                          double conn_timeout,
287                          double first_rw_timeout,
288                          int ReadFirst);
289 void IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents);
290
291 int QueueQuery(ns_type Type,
292                const char *name,
293                AsyncIO *IO,
294                DNSQueryParts *QueryParts,
295                IO_CallBack PostDNS);
296
297 void QueueGetHostByName(AsyncIO *IO,
298                         const char *Hostname,
299                         DNSQueryParts *QueryParts,
300                         IO_CallBack PostDNS);
301
302 void QueryCbDone(AsyncIO *IO);
303
304 void StopClient(AsyncIO *IO);
305
306 void StopClientWatchers(AsyncIO *IO, int CloseFD);
307
308 void SetNextTimeout(AsyncIO *IO, double timeout);
309
310 #include <curl/curl.h>
311
312 #define OPT(s, v) \
313         do { \
314                 sta = curl_easy_setopt(chnd, (CURLOPT_##s), (v));       \
315                 if (sta)  {                                             \
316                         EVQ_syslog(LOG_ERR,                             \
317                                "error setting option " #s               \
318                                " on curl handle: %s",                   \
319                                curl_easy_strerror(sta));                \
320         } } while (0)
321
322 void InitIOStruct(AsyncIO *IO,
323                   void *Data,
324                   eNextState NextState,
325                   IO_LineReaderCallback LineReader,
326                   IO_CallBack DNS_Fail,
327                   IO_CallBack SendDone,
328                   IO_CallBack ReadDone,
329                   IO_CallBack Terminate,
330                   IO_CallBack DBTerminate,
331                   IO_CallBack ConnFail,
332                   IO_CallBack Timeout,
333                   IO_CallBack ShutdownAbort);
334
335 int InitcURLIOStruct(AsyncIO *IO,
336                      void *Data,
337                      const char* Desc,
338                      IO_CallBack SendDone,
339                      IO_CallBack Terminate,
340                      IO_CallBack DBTerminate,
341                      IO_CallBack ShutdownAbort);
342 void KillAsyncIOContext(AsyncIO *IO);
343 void StopCurlWatchers(AsyncIO *IO);
344
345
346 eNextState ReAttachIO(AsyncIO *IO,
347                       void *pData,
348                       int ReadFirst);
349
350 void EV_backtrace(AsyncIO *IO);
351 ev_tstamp ctdl_ev_now (void);
352
353 #endif /* __EVENT_CLIENT_H__ */