/src/FreeRDP/winpr/libwinpr/utils/collections/PubSub.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * WinPR: Windows Portable Runtime  | 
3  |  |  * Publisher/Subscriber Pattern  | 
4  |  |  *  | 
5  |  |  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
6  |  |  *  | 
7  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
8  |  |  * you may not use this file except in compliance with the License.  | 
9  |  |  * You may obtain a copy of the License at  | 
10  |  |  *  | 
11  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
12  |  |  *  | 
13  |  |  * Unless required by applicable law or agreed to in writing, software  | 
14  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
15  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
16  |  |  * See the License for the specific language governing permissions and  | 
17  |  |  * limitations under the License.  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #include <winpr/config.h>  | 
21  |  |  | 
22  |  | #include <winpr/crt.h>  | 
23  |  |  | 
24  |  | #include <winpr/collections.h>  | 
25  |  |  | 
26  |  | /**  | 
27  |  |  * Events (C# Programming Guide)  | 
28  |  |  * http://msdn.microsoft.com/en-us/library/awbftdfh.aspx  | 
29  |  |  */  | 
30  |  |  | 
31  |  | struct s_wPubSub  | 
32  |  | { | 
33  |  |   CRITICAL_SECTION lock;  | 
34  |  |   BOOL synchronized;  | 
35  |  |  | 
36  |  |   size_t size;  | 
37  |  |   size_t count;  | 
38  |  |   wEventType* events;  | 
39  |  | };  | 
40  |  |  | 
41  |  | /**  | 
42  |  |  * Properties  | 
43  |  |  */  | 
44  |  |  | 
45  |  | wEventType* PubSub_GetEventTypes(wPubSub* pubSub, size_t* count)  | 
46  | 0  | { | 
47  | 0  |   WINPR_ASSERT(pubSub);  | 
48  | 0  |   if (count)  | 
49  | 0  |     *count = pubSub->count;  | 
50  |  | 
  | 
51  | 0  |   return pubSub->events;  | 
52  | 0  | }  | 
53  |  |  | 
54  |  | /**  | 
55  |  |  * Methods  | 
56  |  |  */  | 
57  |  |  | 
58  |  | void PubSub_Lock(wPubSub* pubSub)  | 
59  | 0  | { | 
60  | 0  |   WINPR_ASSERT(pubSub);  | 
61  | 0  |   if (pubSub->synchronized)  | 
62  | 0  |     EnterCriticalSection(&pubSub->lock);  | 
63  | 0  | }  | 
64  |  |  | 
65  |  | void PubSub_Unlock(wPubSub* pubSub)  | 
66  | 0  | { | 
67  | 0  |   WINPR_ASSERT(pubSub);  | 
68  | 0  |   if (pubSub->synchronized)  | 
69  | 0  |     LeaveCriticalSection(&pubSub->lock);  | 
70  | 0  | }  | 
71  |  |  | 
72  |  | wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName)  | 
73  | 0  | { | 
74  | 0  |   wEventType* event = NULL;  | 
75  |  | 
  | 
76  | 0  |   WINPR_ASSERT(pubSub);  | 
77  | 0  |   WINPR_ASSERT(EventName);  | 
78  | 0  |   for (size_t index = 0; index < pubSub->count; index++)  | 
79  | 0  |   { | 
80  | 0  |     if (strcmp(pubSub->events[index].EventName, EventName) == 0)  | 
81  | 0  |     { | 
82  | 0  |       event = &(pubSub->events[index]);  | 
83  | 0  |       break;  | 
84  | 0  |     }  | 
85  | 0  |   }  | 
86  |  | 
  | 
87  | 0  |   return event;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, size_t count)  | 
91  | 0  | { | 
92  | 0  |   WINPR_ASSERT(pubSub);  | 
93  | 0  |   WINPR_ASSERT(events || (count == 0));  | 
94  | 0  |   if (pubSub->synchronized)  | 
95  | 0  |     PubSub_Lock(pubSub);  | 
96  |  | 
  | 
97  | 0  |   while (pubSub->count + count >= pubSub->size)  | 
98  | 0  |   { | 
99  | 0  |     size_t new_size = 0;  | 
100  | 0  |     wEventType* new_event = NULL;  | 
101  |  | 
  | 
102  | 0  |     new_size = pubSub->size * 2;  | 
103  | 0  |     new_event = (wEventType*)realloc(pubSub->events, new_size * sizeof(wEventType));  | 
104  | 0  |     if (!new_event)  | 
105  | 0  |       goto fail;  | 
106  | 0  |     pubSub->size = new_size;  | 
107  | 0  |     pubSub->events = new_event;  | 
108  | 0  |   }  | 
109  |  |  | 
110  | 0  |   CopyMemory(&pubSub->events[pubSub->count], events, count * sizeof(wEventType));  | 
111  | 0  |   pubSub->count += count;  | 
112  |  | 
  | 
113  | 0  | fail:  | 
114  | 0  |   if (pubSub->synchronized)  | 
115  | 0  |     PubSub_Unlock(pubSub);  | 
116  | 0  | }  | 
117  |  |  | 
118  |  | int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, ...)  | 
119  | 0  | { | 
120  | 0  |   wEventType* event = NULL;  | 
121  | 0  |   int status = -1;  | 
122  | 0  |   WINPR_ASSERT(pubSub);  | 
123  |  |  | 
124  | 0  |   va_list ap;  | 
125  | 0  |   va_start(ap, EventName);  | 
126  | 0  |   pEventHandler EventHandler = va_arg(ap, pEventHandler);  | 
127  |  | 
  | 
128  | 0  |   if (pubSub->synchronized)  | 
129  | 0  |     PubSub_Lock(pubSub);  | 
130  |  | 
  | 
131  | 0  |   event = PubSub_FindEventType(pubSub, EventName);  | 
132  |  | 
  | 
133  | 0  |   if (event)  | 
134  | 0  |   { | 
135  | 0  |     status = 0;  | 
136  |  | 
  | 
137  | 0  |     if (event->EventHandlerCount < MAX_EVENT_HANDLERS)  | 
138  | 0  |       event->EventHandlers[event->EventHandlerCount++] = EventHandler;  | 
139  | 0  |     else  | 
140  | 0  |       status = -1;  | 
141  | 0  |   }  | 
142  |  | 
  | 
143  | 0  |   if (pubSub->synchronized)  | 
144  | 0  |     PubSub_Unlock(pubSub);  | 
145  |  | 
  | 
146  | 0  |   va_end(ap);  | 
147  | 0  |   return status;  | 
148  | 0  | }  | 
149  |  |  | 
150  |  | int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, ...)  | 
151  | 0  | { | 
152  | 0  |   wEventType* event = NULL;  | 
153  | 0  |   int status = -1;  | 
154  | 0  |   WINPR_ASSERT(pubSub);  | 
155  | 0  |   WINPR_ASSERT(EventName);  | 
156  |  |  | 
157  | 0  |   va_list ap;  | 
158  | 0  |   va_start(ap, EventName);  | 
159  | 0  |   pEventHandler EventHandler = va_arg(ap, pEventHandler);  | 
160  |  | 
  | 
161  | 0  |   if (pubSub->synchronized)  | 
162  | 0  |     PubSub_Lock(pubSub);  | 
163  |  | 
  | 
164  | 0  |   event = PubSub_FindEventType(pubSub, EventName);  | 
165  |  | 
  | 
166  | 0  |   if (event)  | 
167  | 0  |   { | 
168  | 0  |     status = 0;  | 
169  |  | 
  | 
170  | 0  |     for (size_t index = 0; index < event->EventHandlerCount; index++)  | 
171  | 0  |     { | 
172  | 0  |       if (event->EventHandlers[index] == EventHandler)  | 
173  | 0  |       { | 
174  | 0  |         event->EventHandlers[index] = NULL;  | 
175  | 0  |         event->EventHandlerCount--;  | 
176  | 0  |         MoveMemory(&event->EventHandlers[index], &event->EventHandlers[index + 1],  | 
177  | 0  |                    (MAX_EVENT_HANDLERS - index - 1) * sizeof(pEventHandler));  | 
178  | 0  |         status = 1;  | 
179  | 0  |       }  | 
180  | 0  |     }  | 
181  | 0  |   }  | 
182  |  | 
  | 
183  | 0  |   if (pubSub->synchronized)  | 
184  | 0  |     PubSub_Unlock(pubSub);  | 
185  |  | 
  | 
186  | 0  |   va_end(ap);  | 
187  | 0  |   return status;  | 
188  | 0  | }  | 
189  |  |  | 
190  |  | int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context, const wEventArgs* e)  | 
191  | 0  | { | 
192  | 0  |   wEventType* event = NULL;  | 
193  | 0  |   int status = -1;  | 
194  |  | 
  | 
195  | 0  |   if (!pubSub)  | 
196  | 0  |     return -1;  | 
197  | 0  |   WINPR_ASSERT(e);  | 
198  |  |  | 
199  | 0  |   if (pubSub->synchronized)  | 
200  | 0  |     PubSub_Lock(pubSub);  | 
201  |  | 
  | 
202  | 0  |   event = PubSub_FindEventType(pubSub, EventName);  | 
203  |  | 
  | 
204  | 0  |   if (pubSub->synchronized)  | 
205  | 0  |     PubSub_Unlock(pubSub);  | 
206  |  | 
  | 
207  | 0  |   if (event)  | 
208  | 0  |   { | 
209  | 0  |     status = 0;  | 
210  |  | 
  | 
211  | 0  |     for (size_t index = 0; index < event->EventHandlerCount; index++)  | 
212  | 0  |     { | 
213  | 0  |       if (event->EventHandlers[index])  | 
214  | 0  |       { | 
215  | 0  |         event->EventHandlers[index](context, e);  | 
216  | 0  |         status++;  | 
217  | 0  |       }  | 
218  | 0  |     }  | 
219  | 0  |   }  | 
220  |  | 
  | 
221  | 0  |   return status;  | 
222  | 0  | }  | 
223  |  |  | 
224  |  | /**  | 
225  |  |  * Construction, Destruction  | 
226  |  |  */  | 
227  |  |  | 
228  |  | wPubSub* PubSub_New(BOOL synchronized)  | 
229  | 0  | { | 
230  | 0  |   wPubSub* pubSub = (wPubSub*)calloc(1, sizeof(wPubSub));  | 
231  |  | 
  | 
232  | 0  |   if (!pubSub)  | 
233  | 0  |     return NULL;  | 
234  |  |  | 
235  | 0  |   pubSub->synchronized = synchronized;  | 
236  |  | 
  | 
237  | 0  |   if (pubSub->synchronized && !InitializeCriticalSectionAndSpinCount(&pubSub->lock, 4000))  | 
238  | 0  |     goto fail;  | 
239  |  |  | 
240  | 0  |   pubSub->count = 0;  | 
241  | 0  |   pubSub->size = 64;  | 
242  |  | 
  | 
243  | 0  |   pubSub->events = (wEventType*)calloc(pubSub->size, sizeof(wEventType));  | 
244  | 0  |   if (!pubSub->events)  | 
245  | 0  |     goto fail;  | 
246  |  |  | 
247  | 0  |   return pubSub;  | 
248  | 0  | fail:  | 
249  | 0  |   WINPR_PRAGMA_DIAG_PUSH  | 
250  | 0  |   WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC  | 
251  | 0  |   PubSub_Free(pubSub);  | 
252  | 0  |   WINPR_PRAGMA_DIAG_POP  | 
253  | 0  |   return NULL;  | 
254  | 0  | }  | 
255  |  |  | 
256  |  | void PubSub_Free(wPubSub* pubSub)  | 
257  | 0  | { | 
258  | 0  |   if (pubSub)  | 
259  | 0  |   { | 
260  | 0  |     if (pubSub->synchronized)  | 
261  | 0  |       DeleteCriticalSection(&pubSub->lock);  | 
262  |  | 
  | 
263  | 0  |     free(pubSub->events);  | 
264  | 0  |     free(pubSub);  | 
265  | 0  |   }  | 
266  | 0  | }  |