i6engine  1.0
MessageSubscriber.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_CORE_MESSAGESUBSCRIBER_H__
26 #define __I6ENGINE_CORE_MESSAGESUBSCRIBER_H__
27 
28 #include <set>
29 #include <vector>
30 
32 
33 #include "boost/function.hpp"
34 #include "boost/thread/mutex.hpp"
35 
36 namespace i6e {
37 namespace core {
38 
39  class MessageTypeInfo;
40 
44  enum class IDStatus : uint16_t {
45  NONE,
46  CREATED,
47  DELETED
48  };
49 
53  struct ReceivedMessage {
55  std::set<int64_t> waitingFor;
56 
57  ReceivedMessage(const Message::Ptr & msg) : message(msg), waitingFor() {
58  }
59  };
60  typedef boost::shared_ptr<ReceivedMessage> ReceivedMessagePtr;
61 
62  typedef std::vector<ReceivedMessagePtr> MessageVector;
63 
77  public:
81  MessageSubscriber() : _objMessageVectorMutex(), _objMessageVectorA(), _objMessageVectorB(), _objActiveMessageVector(&_objMessageVectorA), _objInActiveMessageVector(&_objMessageVectorB), _ptrMessageMethod(), _newCreatedIDs(), _existingObjects(), _waitingMsgs(), _objMessageListMutex() {
82  }
83 
87  virtual ~MessageSubscriber() {
88  }
89 
94  void receiveMessage(const Message::Ptr & msg);
95 
101  virtual void processMessages();
102 
107  void notifyNewID(const int64_t id);
108 
113  void buffer(const ReceivedMessagePtr & msg);
114 
118  void reset();
119 
123  inline void addMethod(uint16_t msgType, const boost::function<void(const Message::Ptr &)> & ptrMessageMethod) {
124  _ptrMessageMethod[msgType] = ptrMessageMethod;
125  }
126 
130  inline void removeMethod(uint16_t msgType) {
131  _ptrMessageMethod.erase(msgType);
132  boost::mutex::scoped_lock objScopeLock(_objMessageVectorMutex);
133  MessageVector result;
134  for (auto & rm : *_objActiveMessageVector) {
135  if (rm->message->getMessageType() != msgType) {
136  result.push_back(rm);
137  }
138  }
139  _objActiveMessageVector->clear();
140  for (auto & rm : result) {
141  _objActiveMessageVector->push_back(rm);
142  }
143  }
144 
145  protected:
149  void swapMessageBuffer();
150 
154  bool updateBuffer();
155 
157  MessageVector _objMessageVectorA;
158  MessageVector _objMessageVectorB;
159  MessageVector * _objActiveMessageVector;
160  MessageVector * _objInActiveMessageVector;
161 
162  std::map<uint16_t, boost::function<void(const Message::Ptr &)>> _ptrMessageMethod;
163 
168  virtual void deliverMessageInternal(const ReceivedMessagePtr & msg);
169 
170  private:
171 
175  std::vector<int64_t> _newCreatedIDs; // list of all IDs that got created since the last tick
176 
180  std::map<int64_t, IDStatus> _existingObjects; // map for all GameObjects and their status TODO: remove ids deleted long ago?
181 
187  std::map<int64_t, std::vector<ReceivedMessagePtr>> _waitingMsgs;
188 
192  mutable boost::mutex _objMessageListMutex;
193 
197  mutable boost::mutex _bufferLock;
198 
202  MessageSubscriber(const MessageSubscriber &) = delete;
203 
207  MessageSubscriber & operator=(const MessageSubscriber &) = delete;
208  };
209 
210 } /* namespace core */
211 } /* namespace i6e */
212 
213 #endif /* __I6ENGINE_CORE_MESSAGESUBSCRIBER_H__ */
214 
MessageSubscriber()
Standard constructor.
MessageVector * _objActiveMessageVector
IDStatus
Status of an ID.
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
a received message within a MessageSubscriber
Interface for every class that wants to subscribe to the messaging system.
unknown (most likely not created yet)
MessageVector * _objInActiveMessageVector
void addMethod(uint16_t msgType, const boost::function< void(const Message::Ptr &)> &ptrMessageMethod)
adds a method for given message type where messages shall be delivered
std::set< int64_t > waitingFor
virtual ~MessageSubscriber()
Destructor.
boost::shared_ptr< ReceivedMessage > ReceivedMessagePtr
ReceivedMessage(const Message::Ptr &msg)
#define ISIXE_CORE_API
std::vector< ReceivedMessagePtr > MessageVector
void removeMethod(uint16_t msgType)
removes method for given message type
std::map< uint16_t, boost::function< void(const Message::Ptr &)> > _ptrMessageMethod