]> git.vomp.tv Git - vompclient.git/blob - eventdispatcher.cc
Updates to new streaming protocol and live tv
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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       // FIXME
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     if (i == receivers.end()) abort(); // should never happen
89     
90     #ifndef WIN32
91     pthread_cond_signal(&edr->cond);
92     #else
93     // FIXME
94     #endif
95   }
96   
97   edUnlock();
98   return true;
99 }
100
101 void EventDispatcher::edUnregister(EDReceiver* edr)
102 {
103   edLock();
104
105   EDRL::iterator i;
106   for(i = receivers.begin(); i != receivers.end(); i++)
107   {
108     if (*i == edr) break; // found
109   }
110
111   if (i == receivers.end()) abort(); // should never happen
112
113   if (!edr->callinprogress)
114   {
115     receivers.erase(i);
116     edUnlock();
117     return;
118   }
119
120   edr->nomorecalls = true;
121   
122   // edUnlock, wait for callinprogres=false (cond to be signalled), lock  
123 #ifndef WIN32
124   pthread_cond_wait(&edr->cond, &mutex);
125 #else
126   // FIXME
127 #endif
128
129   for(i = receivers.begin(); i != receivers.end(); i++)
130   {
131     if (*i == edr) break; // found
132   }
133
134   if (i == receivers.end()) abort(); // should never happen
135
136   receivers.erase(i);
137   edUnlock();
138 }
139
140 // ---------------------------------------
141
142 void EventDispatcher::edLock()
143 {
144 #ifndef WIN32
145   pthread_mutex_lock(&mutex);
146 #else
147   WaitForSingleObject(mutex, INFINITE);
148 #endif
149 }
150
151 void EventDispatcher::edUnlock()
152 {
153 #ifndef WIN32
154   pthread_mutex_unlock(&mutex);
155 #else
156   ReleaseMutex(mutex);
157 #endif
158 }
159
160 // ---------------------------------------
161
162 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
163 {
164   // For blocking version, not callback version. Call with edLock locked
165   
166 #ifndef WIN32
167   pthread_cond_init(&edr->cond, NULL);
168   pthread_cond_wait(&edr->cond, &mutex);
169 #else
170   // FIXME
171 #endif
172 }
173
174 // -------------- EDReceiver implementation
175
176 EDReceiver::EDReceiver()
177 {
178   nomorecalls = false;
179   callinprogress = false;
180 }
181
182