2 * Copyright (c) 1998-2009 by the citadel.org team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <libcitadel.h>
51 #include "citserver.h"
54 #include "ctdl_module.h"
56 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
58 #include "event_client.h"
60 int event_add_pipe[2] = {-1, -1};
62 citthread_mutex_t EventQueueMutex; /* locks the access to the following vars: */
63 HashList *QueueEvents = NULL;
65 HashList *InboundEventQueue = NULL;
66 HashList *InboundEventQueues[2] = { NULL, NULL };
68 struct ev_loop *event_base;
69 struct ev_io queue_add_event;
71 static void QueueEventAddCallback(struct ev_loop *loop, ev_io *watcher, int revents)
80 /* get the control command... */
81 read(watcher->fd, buf, 1);
84 citthread_mutex_lock(&EventQueueMutex);
86 if (InboundEventQueues[0] == InboundEventQueue) {
87 InboundEventQueue = InboundEventQueues[1];
88 q = InboundEventQueues[0];
91 InboundEventQueue = InboundEventQueues[0];
92 q = InboundEventQueues[1];
94 citthread_mutex_unlock(&EventQueueMutex);
96 It = GetNewHashPos(q, 0);
97 while (GetNextHashPos(q, It, &len, &Key, &v))
103 DeleteHashContent(&q);
104 /// TODO: add it to QueueEvents
107 /////event_del(&queue_add_event);
108 close(event_add_pipe[0]);
109 /// TODO; flush QueueEvents fd's and delete it.
110 ev_io_stop(event_base, &queue_add_event);
111 ev_unloop(event_base, EVUNLOOP_ALL);
113 /* Unblock the other side */
115 CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Read done.\n");
119 void InitEventQueue(void)
121 struct rlimit LimitSet;
123 /// event_base = ev_default_loop(0);
125 base = event_base_new();
127 return NULL; / *XXXerr*/
129 citthread_mutex_init(&EventQueueMutex, NULL);
131 if (pipe(event_add_pipe) != 0) {
132 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for libev queueing: %s\n", strerror(errno));
135 LimitSet.rlim_cur = 1;
136 LimitSet.rlim_max = 1;
137 setrlimit(event_add_pipe[1], &LimitSet);
139 QueueEvents = NewHash(1, Flathash);
140 InboundEventQueues[0] = NewHash(1, Flathash);
141 InboundEventQueues[1] = NewHash(1, Flathash);
142 InboundEventQueue = InboundEventQueues[0];
145 * this thread operates the select() etc. via libev.
149 void *client_event_thread(void *arg)
151 struct CitContext libevent_client_CC;
153 CtdlFillSystemContext(&libevent_client_CC, "LibEvent Thread");
154 // citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
155 CtdlLogPrintf(CTDL_DEBUG, "client_event_thread() initializing\n");
157 event_set(&queue_add_event,
160 QueueEventAddCallback,
163 event_add(&queue_add_event, NULL);
166 ev_io_init(&queue_add_event, QueueEventAddCallback, event_add_pipe[0], EV_READ);
167 ev_io_start(event_base, &queue_add_event);
170 event_base = ev_default_loop (EVFLAG_AUTO);
171 /// ev_loop(event_base, 0);
173 ev_io_init(&queue_add_event, QueueEventAddCallback, event_add_pipe[0], EV_READ);
174 ev_io_start(event_base, &queue_add_event);
176 ev_loop (event_base, 0);
177 CtdlClearSystemContext();
178 ev_default_destroy ();
180 DeleteHash(&QueueEvents);
181 InboundEventQueue = NULL;
182 DeleteHash(&InboundEventQueues[0]);
183 DeleteHash(&InboundEventQueues[1]);
184 citthread_mutex_destroy(&EventQueueMutex);
192 CTDL_MODULE_INIT(event_client)
194 #ifdef EXPERIMENTAL_SMTP_EVENT_CLIENT
198 CtdlThreadCreate("Client event", CTDLTHREAD_BIGSTACK, client_event_thread, NULL);
199 /// todo register shutdown callback.