sipxmedialib  Version 3.3
MsgQueue.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 
12 #ifndef _MsgQueue_h
13 #define _MsgQueue_h
14 
15 #include "rtcp/RtcpConfig.h"
16 
17 // Include
18 #ifdef WIN32
19 #include "TLinkedList.h"
20 #pragma warning(disable:4275)
21 #elif defined(_VXWORKS)
22 #include <msgQLib.h>
23 #elif defined(__pingtel_on_posix__)
24 #include "os/OsMsgQ.h"
25 #else
26 #error Unsupported target platform.
27 #endif
28 
29 
30 #include "Message.h"
31 
32 
33 // Constant Message Type
34 const unsigned long NOFLUSH=0;
35 const unsigned long FLUSH=1;
36 const unsigned long ONE_SECOND=1000;
37 
38 
39 /*|><|************************************************************************
40 *
41 * Class Name: CMsgQueue
42 *
43 * Inheritance:
44 *
45 * Methods:
46 *
47 * Attributes:
48 *
49 * Description: CMsgQueue is a thread safe class which shall provide
50 * general purpose message delivery, storage, retrieval and
51 * processing services. At the heart of the class shall be
52 * a FIFO linked list for storing messages, an event object
53 * and an apartment style thread. The apartment thread
54 * shall block on an event object waiting for incoming
55 * messages to be posted to the linked list. When a
56 * message is posted to the list, the event object shall be
57 * set causing the thread to wake up. Upon waking up, the
58 * thread shall take the first message off the linked list
59 * and call a general purpose processing method defined as
60 * a pure virtual. The processing method shall accept a
61 * message type and an opaque pointer as arguments. It
62 * shall be the responsibility of the user to implement
63 * this method and the message processing logic associated
64 * with their object.
65 *
66 * The CMsgQueue thread shall continue removing messages
67 * from the linked list FIFO until none remain. When the
68 * linked list is emptied, the event object shall be
69 * cleared and the CMsgQueue thread shall block waiting for
70 * the next message to be delivered.
71 *
72 * The CMessageQueue shall implement a set of private
73 * services for performing internal management of the
74 * linked list, event object and thread in addition to
75 * public services for posting, retreiving and flushing
76 * messages.
77 *
78 *
79 *
80 *
81 ************************************************************************|<>|*/
82 
83 class CMsgQueue
84 #ifdef WIN32
85  : public CTLinkedList<CMessage *> // Inherit Linked List Services
86 #endif
87 {
88 
89  public: // Public Member Functions
90 
91 /*|><|************************************************************************
92 *
93 * Method Name: CMsgQueue - Constructor
94 *
95 *
96 * Inputs: None
97 *
98 * Outputs: None
99 *
100 * Returns: None
101 *
102 * Description: The CMsgQueue constructor shall perform some very basic
103 * setup which shall include the initialization of internal
104 * head, tail and entry counter data members. All other
105 * failure prone setup shall be performed in the
106 * Initialize() method.
107 *
108 * Usage Notes:
109 *
110 *
111 ************************************************************************|<>|*/
112  CMsgQueue();
113 
114 
115 /*|><|************************************************************************
116 *
117 * Method Name: CMsgQueue - Destructor
118 *
119 *
120 * Inputs: None
121 *
122 * Outputs: None
123 *
124 * Returns: None
125 *
126 * Description: The destructor for the CMsgQueue object. This method
127 * shall have the responsibility of suspending message
128 * processing and flushing all messages held within the
129 * message queue. The flush shall return all allocated
130 * memory back to the memory pool through using the
131 * services provide in CMemoryPool. The destructor shall
132 * also arrange for the graceful termination of the
133 * apartment thread through signalling the terminate event.
134 *
135 * Usage Notes:
136 *
137 *
138 ************************************************************************|<>|*/
139  virtual ~CMsgQueue();
140 
141 
142 /*|><|************************************************************************
143 *
144 * Method Name: Initialize
145 *
146 *
147 * Inputs: None
148 *
149 * Outputs: None
150 *
151 * Returns: bool
152 *
153 * Description: The Initialize method shall perform the fault suceptible
154 * initialization associated with this class. The
155 * initialization sequence shall include:
156 *
157 * - Creation of the Message and Terminate Event objects.
158 * _ Creation of the Apartment thread
159 *
160 * All of the above initialization steps must succeed for
161 * Initialize to return success.
162 *
163 * Usage Notes:
164 *
165 *
166 ************************************************************************|<>|*/
167  virtual bool Initialize();
168 
169 /*|><|************************************************************************
170 *
171 * Method Name: Shutdown
172 *
173 *
174 * Inputs: None
175 *
176 * Outputs: None
177 *
178 * Returns: bool
179 *
180 * Description: The Shutdown method shall trigger all MsgQueue processing to
181 * cease. This is done as part of a two phase termination
182 * consisting of succession and destruction.
183 *
184 * Usage Notes:
185 *
186 *
187 ************************************************************************|<>|*/
188  virtual bool Shutdown();
189 
190 
191 /*|><|************************************************************************
192 *
193 * Method Name: Post
194 *
195 *
196 * Inputs: CMessage *
197 *
198 * Outputs: None
199 *
200 * Returns: bool
201 *
202 * Description: The Post method provides a general purpose
203 * facility to post a message of a particular type with
204 * optional arguments to the CMsgQueue linked list.
205 *
206 * Post Message shall set the message event object to inform
207 * the apartment thread of the arrival of a new message.
208 *
209 * Usage Notes:
210 *
211 *
212 ************************************************************************|<>|*/
213  bool Post(CMessage *pMessage);
214 
215 
216  protected: // Protected Member Functions
217 
218 /*|><|************************************************************************
219 *
220 * Method Name: ProcessMessage
221 *
222 *
223 * Inputs: CMessage *
224 *
225 * Outputs: None
226 *
227 * Returns: bool
228 *
229 * Description: This is a pure virtual function provide by CMsgQueue as
230 * a means for dispatching messages removed from the queue
231 * for processing. It shall be the responsibility of the
232 * derived class to implement this method.
233 *
234 * Usage Notes:
235 *
236 *
237 ************************************************************************|<>|*/
238  virtual bool ProcessMessage(CMessage *)=0;
239 
240 
241 
242 /*|><|************************************************************************
243 *
244 * Method Name: TerminateProcessing
245 *
246 *
247 * Inputs: None
248 *
249 * Outputs: None
250 *
251 * Returns: bool
252 *
253 * Description: Terminates thread processing for this object.
254 *
255 * Usage Notes:
256 *
257 *
258 ************************************************************************|<>|*/
259  void TerminateProcessing(unsigned long dwMode=NOFLUSH);
260 
261 /*|><|************************************************************************
262 *
263 * Method Name: FlushMessages
264 *
265 *
266 * Inputs: None
267 *
268 * Outputs: None
269 *
270 * Returns: bool
271 *
272 * Description: Removes all messages from the CMsgQueue and releases the
273 * memory associated with each entry.
274 *
275 * Usage Notes:
276 *
277 *
278 ************************************************************************|<>|*/
279  void FlushMessages(void);
280 
281  private:
282 
283 #if defined(_WIN32)
284 /*|><|************************************************************************
285 *
286 * Method Name: CreateMessageEvent
287 *
288 *
289 * Inputs: None
290 *
291 * Outputs: None
292 *
293 * Returns: bool
294 *
295 * Description: This method shall create a manually resetable event object to
296 * be used for informing the MsgQueue object of the arrival of a
297 * new message.
298 *
299 * Usage Notes:
300 *
301 *
302 ************************************************************************|<>|*/
303  bool CreateMessageEvent();
304 
305 /*|><|************************************************************************
306 *
307 * Method Name: DestroyMessageEvent
308 *
309 *
310 * Inputs: None
311 *
312 * Outputs: None
313 *
314 * Returns: None
315 *
316 * Description: This method shall destroy a manually resetable event object
317 * used for informing the MsgQueue object of the arrival of a new
318 * message.
319 *
320 * Usage Notes:
321 *
322 *
323 ************************************************************************|<>|*/
324  void DestroyMessageEvent();
325 
326 /*|><|************************************************************************
327 *
328 * Method Name: CreateThreadEvents
329 *
330 *
331 * Inputs: None
332 *
333 * Outputs: None
334 *
335 * Returns: bool
336 *
337 * Description: This method shall create a manually resetable event object to
338 * be used for informing the parent of thread state changes.
339 *
340 * Usage Notes:
341 *
342 *
343 ************************************************************************|<>|*/
344  bool CreateThreadEvents();
345 
346 /*|><|************************************************************************
347 *
348 * Method Name: DestroyThreadEvents
349 *
350 *
351 * Inputs: None
352 *
353 * Outputs: None
354 *
355 * Returns: None
356 *
357 * Description: This method shall destroy a manually resetable event object
358 * used for informing the parent of thread state changes.
359 *
360 * Usage Notes:
361 *
362 *
363 ************************************************************************|<>|*/
364  void DestroyThreadEvents();
365 
366 /*|><|************************************************************************
367 *
368 * Method Name: CreateMessageThread
369 *
370 *
371 * Inputs: None
372 *
373 * Outputs: None
374 *
375 * Returns: bool
376 *
377 * Description: This is a private method which shall create an apartment style
378 * processing thread for the purpose dispatching and processing
379 * messages that have been posted to a client's message list.
380 *
381 * Usage Notes:
382 *
383 *
384 ************************************************************************|<>|*/
385  bool CreateMessageThread();
386 
387 /*|><|************************************************************************
388 *
389 * Method Name: InitMessageThread
390 *
391 *
392 * Inputs: None
393 *
394 * Outputs: None
395 *
396 * Returns: None
397 *
398 * Description: This is a private method which shall initialize the message
399 * thread. Initialization shall involve the creation and setting
400 * of a thread event used to signal the main thread of state
401 * change. Upon successful event creation, the init method shall
402 * call the MessageLoop() which shall pend on and process incoming
403 * messages. A failed initialization shall result in thread
404 * termination.
405 *
406 *
407 * Usage Notes:
408 *
409 *
410 ************************************************************************|<>|*/
411  static unsigned int _stdcall InitMessageThread(void * vpArgument);
412 
413 
414 /*|><|************************************************************************
415 *
416 * Method Name: TerminateMessaging
417 *
418 *
419 * Inputs: None
420 *
421 * Outputs: None
422 *
423 * Returns: bool
424 *
425 * Description: Terminates any further message processing activity
426 * for this object.
427 *
428 * Usage Notes:
429 *
430 *
431 ************************************************************************|<>|*/
432  void TerminateMessaging(unsigned long dwMode);
433 
434 
435 
436 /*|><|************************************************************************
437 *
438 * Method Name: TerminateMessageThread
439 *
440 *
441 * Inputs: None
442 *
443 * Outputs: None
444 *
445 * Returns: None
446 *
447 * Description: This is a private method which shall shall terminate the
448 * message thread. In so doing, it shall destroy both message and
449 * thread events used to signal state change as well as remove all
450 * messages which may remain on the message queue.
451 *
452 *
453 * Usage Notes:
454 *
455 *
456 ************************************************************************|<>|*/
457  void TerminateMessageThread();
458 
459 
460 #elif defined(_VXWORKS)
461 
478 bool CMsgQueue::CreateMessageTask();
479 
480 
481 /*|><|************************************************************************
482 *
483 * Method Name: InitMessageThread
484 *
485 *
486 * Inputs: None
487 *
488 * Outputs: None
489 *
490 * Returns: None
491 *
492 * Description: This is a private method which shall initialize the message
493 * thread. Initialization shall involve the creation and setting
494 * of a thread event used to signal the main thread of state
495 * change. Upon successful event creation, the init method shall
496 * call the MessageLoop() which shall pend on and process
497 * incoming messages. A failed initialization shall result in
498 * thread termination.
499 *
500 *
501 * Usage Notes:
502 *
503 *
504 ************************************************************************|<>|*/
505  static int InitMessageThread(int argument1, int arg2, int arg3,
506  int arg4, int arg5, int arg6, int arg7, int arg8,
507  int arg9, int arg10);
508 
509 #elif defined(__pingtel_on_posix__)
510 /* nothing needed? */
511 #else
512 #error Unsupported target platform.
513 #endif
514 
515 /*|><|************************************************************************
516 *
517 * Method Name: MessageLoop
518 *
519 *
520 * Inputs: None
521 *
522 * Outputs: None
523 *
524 * Returns: None
525 *
526 * Description: This is a private method which shall pend on incoming messages
527 * delivered to the inheriting object. Signals from either the
528 * Message Event or Thread event shall cause the MessageLoop to
529 * leave its wait state and begin processing. If a message event
530 * is detected, the MessageLoop shall iterate through each message
531 * on the list and dispatch those messages to the client defined
532 * ProcessMessage() method. If a terminate event is detected, the
533 * message processing loop shall be exited and the thread shall be
534 * gracefully terminated.
535 *
536 *
537 * Usage Notes:
538 *
539 *
540 ************************************************************************|<>|*/
541  void MessageLoop();
542 
543 
544 
545  private:
546 
547 #if defined(_WIN32)
548 /*|><|************************************************************************
549 *
550 * Attribute Name: m_hMessageThread
551 *
552 * Type: HANDLE
553 *
554 * Description: The thread handle is used to store the handle generated
555 * in response to a successful thread creation request.
556 *
557 ************************************************************************|<>|*/
558  HANDLE m_hMessageThread;
559 
560 /*|><|************************************************************************
561 *
562 * Attribute Name: m_hMessageEvent
563 *
564 * Type: HANDLE
565 *
566 * Description: The message event is a manually resetable event which
567 * shall be used to signal the addition of a new message in
568 * the Message Queue.
569 *
570 ************************************************************************|<>|*/
571  HANDLE m_hMessageEvent;
572 
573 /*|><|************************************************************************
574 *
575 * Attribute Name: m_hThreadEvent
576 *
577 * Type: HANDLE
578 *
579 * Description: The thread event shall be used by used to signal thread
580 * creation and shall trigger the onset of thread termination.
581 *
582 ************************************************************************|<>|*/
583  HANDLE m_hThreadEvent;
584 
585 /*|><|************************************************************************
586 *
587 * Attribute Name: m_hTerminateEvent
588 *
589 * Type: HANDLE
590 *
591 * Description: The terminate event shall be used to signal the gracefully
592 * termination of the apartment thread to the waiting destructor.
593 *
594 ************************************************************************|<>|*/
595  HANDLE m_hTerminateEvent;
596 
597 #elif defined(_VXWORKS)
598 
599 /*|><|************************************************************************
600 *
601 * Attribute Name: m_ulMsgQID
602 *
603 * Type: MSG_Q_ID
604 *
605 * Description: The message queue handle used to identify the vxWorks FIFO
606 * on which events messages are pushed and popped.
607 *
608 ************************************************************************|<>|*/
609  MSG_Q_ID m_ulMsgQID;
610 
611 /*|><|************************************************************************
612 *
613 * Attribute Name: m_iMsgTaskID
614 *
615 * Type: int
616 *
617 * Description: The task ID for the thread used to process messages that
618 * are pending on the FIFO.
619 *
620 ************************************************************************|<>|*/
621  int m_iMsgTaskID;
622 
623 #elif defined(__pingtel_on_posix__)
624  OsMsgQ * m_pMsgQ;
625  static void * InitMessageThread(void * argument1);
626 #else
627 #error Unsupported target platform.
628 #endif
629 
630 };
631 
632 #endif
void TerminateProcessing(unsigned long dwMode=NOFLUSH)
const unsigned long FLUSH
Definition: MsgQueue.h:35
virtual bool ProcessMessage(CMessage *)=0
virtual bool Shutdown()
void FlushMessages(void)
virtual bool Initialize()
const unsigned long NOFLUSH
Definition: MsgQueue.h:34
Definition: MsgQueue.h:83
void * MSG_Q_ID
Definition: MpTypes.h:40
const unsigned long ONE_SECOND
Definition: MsgQueue.h:36
virtual ~CMsgQueue()
void MessageLoop()
Definition: Message.h:43
Definition: TLinkedList.h:44
bool Post(CMessage *pMessage)