00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef BASICMSGQUEUEMUX_H
00029 #define BASICMSGQUEUEMUX_H
00030
00031 #include <BasicUtils/BasicThread.h>
00032 #include <BasicUtils/BasicFunctor.h>
00033 #include "basicipcmsgqueue.h"
00034 #include "basicmuxmessage.h"
00035
00036 #include <map>
00037 #include <list>
00038
00039 class BasicMUXChannelBase;
00040
00041 class BasicMsgQueueMUX {
00042 typedef enum {MSGQ_OWNER = 1, MSGQ_CLIENT} message_types;
00043
00044 BasicIPCMsgQueue msgQueue;
00045
00046 bool writerFlushAndExit;
00047
00048 BasicFunctor<BasicMsgQueueMUX> readFunctor;
00049 BasicFunctor<BasicMsgQueueMUX> writeFunctor;
00050
00051 BasicThread readerThread;
00052 BasicThread writerThread;
00053
00054 typedef std::map<int, BasicMUXChannelBase *> channelMap_t;
00055 channelMap_t channelMap;
00056
00057 typedef std::list<BasicMUXChannelBase *> channelList_t;
00058 channelList_t channelList;
00059
00060 typedef std::list<int> msgClearedList_t;
00061 msgClearedList_t msgClearedList;
00062
00063 typedef struct {
00064 long type;
00065 long key;
00066 } clear_message_t;
00067
00068 public:
00069
00070 BasicMsgQueueMUX();
00071 virtual ~BasicMsgQueueMUX();
00072
00073 bool addChannel(BasicMUXChannelBase *channel);
00074 void removeChannel(BasicMUXChannelBase *channel);
00075
00076 bool isOpen();
00077 bool open(key_t key);
00078 key_t create(key_t key);
00079 void close(bool flushFirst = true);
00080
00081 protected:
00082 void msgCleared(int key);
00083 void wakeWriter();
00084
00085 void writeRun();
00086 void readRun();
00087
00088 private:
00089 bool writesAllClear();
00090 int sendClears(int max);
00091 int sendMessages(int max);
00092
00093 void readClear(clear_message_t *clearMsg);
00094 void readMessage(BasicMUXMessageBase::message_t *msg, int len);
00095
00096 friend class BasicMUXChannelBase;
00097 friend class BasicFunctor<BasicMsgQueueMUX>;
00098 };
00099 #endif