]> git.vomp.tv Git - vompclient.git/blob - eventdispatcher.cc
Timeouts, keepalives...
[vompclient.git] / eventdispatcher.cc
1 /*
2     Copyright 2007 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "eventdispatcher.h"
22
23 EventDispatcher::EventDispatcher()
24 {
25 #ifndef WIN32
26   pthread_mutex_init(&mutex, NULL);
27 #else
28   mutex = CreateMutex(NULL, FALSE, NULL);
29 #endif
30 }
31
32 void EventDispatcher::edRegister(EDReceiver* edr)
33 {
34   edLock();
35   receivers.push_back(edr);
36   edUnlock();
37 }  
38
39 bool EventDispatcher::edFindAndCall(void* userTag)
40 {
41   edLock();
42   
43   EDReceiver* edr = NULL;
44   EDRL::iterator i;
45   for(i = receivers.begin(); i != receivers.end(); i++)
46   {
47     if (ed_cb_find(*i, userTag))
48     {
49       edr = *i;
50       break; // found (by asking the EventDispatcher implementor to check if userTag is for *i
51     }
52   }
53   
54   if ((i == receivers.end()) || edr->callinprogress || edr->nomorecalls)
55   {
56     edUnlock();
57     return false;
58   }
59   
60   edr->callinprogress = true;
61   edUnlock();  
62   bool edrType = edr->call(userTag);
63   edLock();
64   edr->callinprogress = false;
65
66   if (edrType == false) // it's a multicall
67   {
68     if (edr->nomorecalls) // External has called unRegister - probably the receiver
69     {
70       // wake up the thread waiting in unregister
71       #ifndef WIN32
72       pthread_cond_signal(&edr->cond);
73       #else
74       SetEvent(edr->cond);
75       #endif
76     }
77   }
78   else // It's a single call. The receiver should be removed from the list. There will be a thread to wake up
79   {
80     for(i = receivers.begin(); i != receivers.end(); i++)
81     {
82       if (*i == edr)
83       {
84         receivers.erase(i);
85         break;
86       }
87     }
88     #ifndef WIN32
89     if (i == receivers.end()) abort(); // should never happen
90     // but it can happen under windows ... how??
91     #endif
92     
93     #ifndef WIN32
94     pthread_cond_signal(&edr->cond);
95     #else
96     SetEvent(edr->cond);
97     #endif
98   }
99   
100   edUnlock();
101   return true;
102 }
103
104 void EventDispatcher::edUnregister(EDReceiver* edr)
105 {
106   edLock();
107
108   EDRL::iterator i;
109   for(i = receivers.begin(); i != receivers.end(); i++)
110   {
111     if (*i == edr) break; // found
112   }
113
114   if (i == receivers.end()) return; // Not in the list. Already unregistered? Perhaps vdr::connectionDied already removed this streamclient
115   // FIXME, this should probably be done another way. A call to edUnregister with an object that may or may not be in the list? Not good.
116
117   if (!edr->callinprogress)
118   {
119     receivers.erase(i);
120     edUnlock();
121     return;
122   }
123
124   edr->nomorecalls = true;
125   
126   // edUnlock, wait for callinprogres=false (cond to be signalled), lock  
127 #ifndef WIN32
128   pthread_cond_wait(&edr->cond, &mutex);
129 #else
130   ReleaseMutex(mutex);
131   WaitForSingleObject(edr->cond,INFINITE);
132   WaitForSingleObject(mutex, INFINITE);
133   ResetEvent(edr->cond);
134
135 #endif
136
137   for(i = receivers.begin(); i != receivers.end(); i++)
138   {
139     if (*i == edr) break; // found
140   }
141
142   if (i == receivers.end()) abort(); // should never happen
143
144   receivers.erase(i);
145   edUnlock();
146 }
147
148 // ---------------------------------------
149
150 void EventDispatcher::edLock()
151 {
152 #ifndef WIN32
153   pthread_mutex_lock(&mutex);
154 #else
155   WaitForSingleObject(mutex, INFINITE);
156 #endif
157 }
158
159 void EventDispatcher::edUnlock()
160 {
161 #ifndef WIN32
162   pthread_mutex_unlock(&mutex);
163 #else
164   ReleaseMutex(mutex);
165 #endif
166 }
167
168 // ---------------------------------------
169
170 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
171 {
172   // For blocking version, not callback version. Call with edLock locked
173   
174 #ifndef WIN32
175   pthread_cond_init(&edr->cond, NULL);
176   pthread_cond_wait(&edr->cond, &mutex);
177 #else
178    edr->cond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
179    ReleaseMutex(mutex);
180    WaitForSingleObject(edr->cond,INFINITE);
181    ResetEvent(edr->cond);
182    WaitForSingleObject(mutex, INFINITE);
183 #endif
184 }
185
186 // -------------- EDReceiver implementation
187
188 EDReceiver::EDReceiver()
189 {
190   nomorecalls = false;
191   callinprogress = false;
192 #ifdef WIN32
193   cond=NULL;
194 #endif
195 }
196
197 EDReceiver::~EDReceiver()
198 {
199 #ifdef WIN32
200   if (cond!=NULL) CloseHandle(cond);
201 #endif
202 }