4S Device Communication Module Collection  Version 0.6-SNAPSHOT
Full documentation of the modules in the 4SDC collection (aimed at 4SDC module developers)
msgqueuebase.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2015 The 4S Foundation (www.4s-online.dk)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
34 #ifndef MSGQUEUEBASE_H
35 #define MSGQUEUEBASE_H
36 
37 #include "FSYS/handle.h"
38 #include "FSYS/msgcallback.h"
39 #include "FSYS/msgaddr.h"
40 
41 #include <condition_variable>
42 #include <forward_list>
43 #include <memory>
44 #include <mutex>
45 #include <queue>
46 #include <typeinfo>
47 
48 namespace FSYS
49 {
50  class MsgSenderBase;
51 
52  class MsgQueueBase: public Handle
53  {
54  friend class MsgSenderBase;
55  private:
56  explicit MsgQueueBase();
57  ~MsgQueueBase();
58 
60  {
61  public:
62  void *magicKey;
63  const std::type_info &typeOfMsg;
64  MsgCallBack * msgCallBack;
65  ListenerListItem( void *magicKey,
66  const std::type_info &typeOfMsg,
67  MsgCallBack * msgCallBack)
68  : magicKey(magicKey),
69  typeOfMsg(typeOfMsg),
70  msgCallBack(msgCallBack)
71  {}
72  };
73 
75  {
76  public:
77  std::shared_ptr<BaseMsg> msg; // Msg to queue
78  const std::type_info &typeOfMsg;
79  Handle object; // Msg object to target
80  void *magicKey; // MagicKey to target
81  MsgQueueItem(std::shared_ptr<BaseMsg> msg,
82  const std::type_info &typeOfMsg,
83  Handle object,
84  void *magicKey)
85  : msg(msg),
86  typeOfMsg(typeOfMsg),
87  object(object),
88  magicKey(magicKey)
89  {}
90 
91  MsgQueueItem(const MsgQueueItem &msgQueueItem)
92  : msg(msgQueueItem.msg),
93  typeOfMsg(msgQueueItem.typeOfMsg),
94  object(msgQueueItem.object),
95  magicKey(msgQueueItem.magicKey)
96  {}
97  };
98 
99  std::condition_variable_any localQueueConditionVariable;
100  std::queue<MsgQueueItem> localMsgQueue;
101 
102  inline static std::recursive_mutex &getMutex( void )
103  {
104  static std::recursive_mutex mutex;
105  return mutex;
106  }
107 
108  std::forward_list<ListenerListItem> localListenerList;
109 
110  // The list of queues in the system
111  static std::forward_list<MsgQueueBase*> listOfQueues;
112 
118  static void sendMsg(std::shared_ptr<BaseMsg> msg, // Msg to send
119  const std::type_info &typeOfMsg, // Type of msg to send
120  Handle queue, // Msg queue to target
121  Handle object, // Msg object to target
122  void *magicKey // MagicKey to target
123  );
124 
125 
126 
127 
128  inline static MsgQueueBase &getThreadQueue( void )
129  {
130  static thread_local MsgQueueBase localQueue;
131  return localQueue;
132  }
133 
134 
135  inline static bool &getTerminateLoopFlag( void )
136  {
137  // Set to true when loop should be terminated
138  static thread_local bool terminateFlag;
139 
140  return terminateFlag;
141  }
142 
143  public:
144 
145  inline static Handle getBroadCastHandle( void )
146  {
147  static Handle broadcastHandle;
148  return broadcastHandle;
149  }
150 
151 
152  static void addListenerToQueue(void *magicKey,
153  const std::type_info &typeOfMsg,
154  MsgCallBack *msgCallBack);
155 
156  static void removeListenerFromQueue(MsgCallBack *msgCallBack);
157 
158 
159 
160  static void waitUntilQueueHasData( void );
161 
186  static void emptyMsgQueue( int maxTimeMs=-1 );
187 
188 
189 
207  static void breakEmptyMsgQueue( void );
208 
217  static bool isEmpty( void );
218 
224  inline static Handle getHandle( void )
225  {
226  return getThreadQueue();
227  }
228 
229  };
230 
231 }
232 
233 #endif // MSGQUEUEBASE_H
static void sendMsg(std::shared_ptr< BaseMsg > msg, const std::type_info &typeOfMsg, Handle queue, Handle object, void *magicKey)
Adds the message to the right queues.
Definition: msgqueuebase.cpp:81
static Handle getHandle(void)
Function to get the handle for this threads message queue.
Definition: msgqueuebase.h:224
Contains interface for a message address.
Class that holds and assigns handles.
Definition: handle.h:55
Type neutral message sender class.
Definition: msgsenderbase.h:51
static void emptyMsgQueue(int maxTimeMs=-1)
The function will process messages on the message queue.
Definition: msgqueuebase.cpp:130
Contains interface declaration for the FSYS::Handle class.
Definition: basemsg.h:38
static bool isEmpty(void)
Function to determine if there is data on the msg queue.
Definition: msgqueuebase.cpp:209
static void breakEmptyMsgQueue(void)
Breaks out of emptyMsgQueue.
Definition: msgqueuebase.cpp:194
Contains interface declaration for the FSYS::MsgCallBack class.
Definition: msgqueuebase.h:74
Definition: msgqueuebase.h:52
The MsgCallBack class is an internal class to the message system.
Definition: msgcallback.h:54
Definition: msgqueuebase.h:59