Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MessageQueueStatusThread.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_MessageQueueStatusThread_h_
00003 #define INCLUDED_MessageQueueStatusThread_h_
00004 
00005 #ifdef PLATFORM_APERIOS
00006 #  warning MessageQueueStatusThread is not Aperios compatable, this is not going to compile
00007 #else
00008 
00009 #include "IPC/SemaphoreManager.h"
00010 #include "IPC/Thread.h"
00011 #include <list>
00012 
00013 class MessageQueueBase;
00014 
00015 //! description of MessageQueueStatusThread
00016 class MessageQueueStatusThread : protected Thread {
00017 public:
00018   //!An interface to allow you to receive callbacks when a message has been read from a MessageQueue, subscribed via an external class which is monitoring the queue's MessageQueueBase::pollStatus() (e.g. LoadFileThread)
00019   class StatusListener {
00020   public:
00021     //! destructor -- does nothing
00022     virtual ~StatusListener() {}
00023     
00024     //! Called after a message has been read by all receivers, and thus has been removed from the queue
00025     /*! Don't assume that because you receive this callback there is space in
00026     *  the queue -- an earlier listener may have already added a message, or
00027     *  the queue might have been already waiting to send a message if
00028     *  the queue's overflowPolicy is MessageQueueBase::WAIT
00029     *
00030     *  @param which The MessageQueueBase which has had message(s) read
00031     *  @param howmany The number of message which have been cleared */
00032     virtual void messagesRead(MessageQueueBase& which, unsigned int howmany)=0;
00033   };
00034   
00035   
00036   //! constructor
00037   MessageQueueStatusThread()
00038     : Thread(), statusListeners(), queue(NULL), semid(), numRead()
00039   {}
00040   //! constructor, automatically starts the thread with the specified queue, and an optional initial listener
00041   explicit MessageQueueStatusThread(MessageQueueBase& mq, StatusListener* listener=NULL)
00042     : Thread(), statusListeners(), queue(NULL), semid(), numRead()
00043   {
00044     setMessageQueue(mq);
00045     addStatusListener(listener);
00046   }
00047   //! destructor, remove ourself from queue
00048   ~MessageQueueStatusThread();
00049 
00050   //! Request updates to StatusListener callbacks
00051   virtual void addStatusListener(StatusListener* l);
00052   //! Unsubscribes a StatusListener from future updates
00053   virtual void removeStatusListener(StatusListener* l);
00054   
00055   //! (re)sets the message queue being listened to
00056   virtual void setMessageQueue(MessageQueueBase& mq);
00057   //! returns the current queue
00058   virtual MessageQueueBase* getMessageQueue();
00059   
00060 protected:
00061   //! start the thread
00062   virtual bool launched();
00063   //! wait for the queue's message read semaphore to be raised, and notify listeners
00064   virtual void* run();
00065   //! indicates it's time to stop monitoring the queue (need to raise the semaphore so run() will notice the stop)
00066   virtual Thread& stop();
00067   //! cleanup
00068   virtual void cancelled();
00069   
00070   //! Notifies statusListeners that a message has been read by all MessageQueue receivers
00071   virtual void fireMessagesRead(unsigned int howmany);
00072 
00073   //! MessageQueueBase::StatusListeners currently subscribed from addStatusListener()
00074   std::list<StatusListener*> statusListeners;
00075   
00076   //! The MessageQueue that this thread is monitoring
00077   MessageQueueBase* queue;
00078   
00079   //! the semaphore which is being monitored, raised by #queue when a message is read
00080   SemaphoreManager::semid_t semid;
00081   
00082   //! the number of messages read sent last time the semaphore (#semid) was raised
00083   unsigned int numRead;
00084   
00085 private:
00086   MessageQueueStatusThread(const MessageQueueStatusThread&); //!< don't call, shouldn't copy
00087   MessageQueueStatusThread& operator=(const MessageQueueStatusThread&); //!< don't call, shouldn't assign
00088 };
00089 
00090 /*! @file
00091  * @brief 
00092  * @author Ethan Tira-Thompson (ejt) (Creator)
00093  */
00094 
00095 #endif
00096 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:45 2016 by Doxygen 1.6.3