1 #ifndef SYNC_SENDER_HPP
2 #define SYNC_SENDER_HPP
4 #include "blub/core/globals.hpp"
5 #include "blub/core/signal.hpp"
6 #include "blub/math/octree/container.hpp"
7 #include "blub/math/octree/search.hpp"
8 #include "blub/async/strand.hpp"
10 #include "boost/function/function2.hpp"
11 #include "boost/function/function3.hpp"
20 template <
typename syncType,
typename receiverType>
24 typedef syncType t_sync;
25 typedef receiverType t_receiver;
27 typedef octree::container<t_sync> t_syncTree;
28 typedef octree::container<t_receiver> t_receiverTree;
30 typedef hashMap<t_receiver, vector3> t_receiverPosMap;
31 typedef hashMap<t_sync, vector3> t_syncPosMap;
33 typedef hashList<t_sync> t_syncList;
34 typedef hashList<t_receiver> t_receiverList;
36 typedef hashMap<t_receiver, t_syncList> t_receiverToSyncsMap;
37 typedef hashMap<t_sync, t_receiverList> t_syncToReceiversMap;
40 typedef boost::function<bool (t_receiver, vector3, typename t_syncTree::t_nodePtr)> t_callbackInSyncRangeReceiver;
41 typedef boost::function<bool (t_sync, vector3, typename t_receiverTree::t_nodePtr)> t_callbackInSyncRangeSync;
46 , m_syncTree(treeSize)
47 , m_receiverTree(treeSize)
57 void addSync(
const t_sync toSync,
const vector3& pos)
59 m_master.dispatch(boost::bind(&sender::addSyncMaster,
this, toSync, pos));
61 void updateSync(
const t_sync toSync,
const vector3& pos)
63 m_master.dispatch(boost::bind(&sender::updateSyncMaster,
this, toSync, pos));
65 void removeSync(
const t_sync toSync)
67 m_master.dispatch(boost::bind(&sender::removeSyncMaster,
this, toSync));
71 void addReceiver(t_receiver receiver,
const vector3& pos)
73 m_master.dispatch(boost::bind(&sender::addReceiverMaster,
this, receiver, pos));
75 void updateReceiver(t_receiver receiver,
const vector3& pos)
77 m_master.dispatch(boost::bind(&sender::updateReceiverMaster,
this, receiver, pos));
79 void removeReceiver(t_receiver receiver)
81 m_master.dispatch(boost::bind(&sender::removeReceiverMaster,
this, receiver));
84 void addSyncMaster(
const t_sync toSync,
const vector3& pos)
86 BASSERT(m_syncPosMap.find(toSync) == m_syncPosMap.cend());
87 BASSERT(m_syncReceivers.find(toSync) == m_syncReceivers.cend());
88 BASSERT(m_syncTree.getNodes(toSync).empty());
90 m_syncPosMap.insert(toSync, pos);
92 m_syncReceivers.insert(toSync, t_receiverList());
97 m_syncTree.insert(toSync, pos);
100 updateLinkSyncReceiverMaster(toSync);
102 void updateSyncMaster(
const t_sync toSync,
const vector3& pos)
104 BASSERT(m_syncPosMap.find(toSync) != m_syncPosMap.cend());
105 BASSERT(m_syncReceivers.find(toSync) != m_syncReceivers.cend());
106 BASSERT(!m_syncTree.getNodes(toSync).empty());
108 m_syncPosMap.insert(toSync, pos);
110 if (!m_syncTree.update(toSync, pos))
116 updateLinkSyncReceiverMaster(toSync);
118 void removeSyncMaster(
const t_sync toSync)
120 BASSERT(m_syncPosMap.find(toSync) != m_syncPosMap.cend());
121 BASSERT(m_syncReceivers.find(toSync) != m_syncReceivers.cend());
122 BASSERT(!m_syncTree.getNodes(toSync).empty());
125 m_syncPosMap.erase(m_syncPosMap.find(toSync));
132 m_syncTree.remove(toSync);
137 typename t_syncToReceiversMap::iterator it = m_syncReceivers.find(toSync);
138 BASSERT(it != m_syncReceivers.cend());
139 const t_receiverList& accordingReceivers(it->second);
140 while (!accordingReceivers.empty())
142 removeLinkSyncReceiverMaster(*accordingReceivers.cbegin(), toSync);
144 m_syncReceivers.erase(it);
148 void addReceiverMaster(t_receiver receiver,
const vector3& pos)
150 BASSERT(m_receiverPosMap.find(receiver) == m_receiverPosMap.cend());
151 BASSERT(m_receiverSyncs.find(receiver) == m_receiverSyncs.cend());
152 BASSERT(m_receiverTree.getNodes(receiver).empty());
154 m_receiverPosMap.insert(receiver, pos);
156 m_receiverSyncs.insert(receiver, t_syncList());
161 m_receiverTree.insert(receiver, pos);
164 updateLinkReceiverSyncMaster(receiver);
166 void updateReceiverMaster(t_receiver receiver,
const vector3& pos)
168 BASSERT(m_receiverPosMap.find(receiver) != m_receiverPosMap.cend());
169 BASSERT(m_receiverSyncs.find(receiver) != m_receiverSyncs.cend());
170 BASSERT(!m_receiverTree.getNodes(receiver).empty());
172 m_receiverPosMap.insert(receiver, pos);
174 if (!m_receiverTree.update(receiver, pos))
181 updateLinkReceiverSyncMaster(receiver);
183 void removeReceiverMaster(t_receiver receiver)
185 BASSERT(m_receiverPosMap.find(receiver) != m_receiverPosMap.cend());
186 BASSERT(m_receiverSyncs.find(receiver) != m_receiverSyncs.cend());
187 BASSERT(!m_receiverTree.getNodes(receiver).empty());
190 m_receiverPosMap.erase(m_receiverPosMap.find(receiver));
197 m_receiverTree.remove(receiver);
202 typename t_receiverToSyncsMap::iterator it = m_receiverSyncs.find(receiver);
203 BASSERT(it != m_receiverSyncs.cend());
204 const t_syncList& accordingSyncs(it->second);
205 while (!accordingSyncs.empty())
207 removeLinkSyncReceiverMaster(receiver, *accordingSyncs.cbegin());
209 m_receiverSyncs.erase(it);
218 void setCallbackInSyncRangeReceiver(t_callbackInSyncRangeReceiver toSet)
220 m_callbackInSyncRangeReceiver = toSet;
222 void setCallbackInSyncRangeSync(t_callbackInSyncRangeSync toSet)
224 m_callbackInSyncRangeSync = toSet;
228 t_sigAdd* signalAdd()
233 t_sigRemove* signalRemove()
239 void updateLinkReceiverSyncMaster(t_receiver receiver)
241 auto leafs(m_receiverTree.getNodes(receiver));
242 BASSERT(leafs.size() == 1);
243 auto leaf(*leafs.begin());
244 vector3int32 centerLeaf(leaf->getPosition()+m_receiverTree.getMinNodeSize()/2);
246 auto callbackForOctree(boost::bind(&sender<t_sync, t_receiver>::isInSyncRangeReceiver,
this, receiver, vector3(centerLeaf.x, centerLeaf.y, centerLeaf.z), _1));
247 const typename octree::search<t_sync>::t_dataList result(octree::search<t_sync>::getDataByUserDefinedFunction(m_syncTree, callbackForOctree));
249 typename t_receiverToSyncsMap::const_iterator it = m_receiverSyncs.find(receiver);
250 BASSERT(it != m_receiverSyncs.cend());
253 const t_syncList syncs(it->second);
255 for (t_sync sync : syncs)
257 if (result.find(sync) == result.cend())
259 removeLinkSyncReceiverMaster(receiver, sync);
265 for (t_sync sync : result)
267 if (syncs.find(sync) == syncs.cend())
269 addLinkSyncReceiverMaster(receiver, sync);
275 void updateLinkSyncReceiverMaster(t_sync sync)
277 if (m_receiverPosMap.empty())
282 auto leafs(m_syncTree.getNodes(sync));
283 BASSERT(leafs.size() == 1);
284 auto leaf(*leafs.begin());
285 vector3int32 centerLeaf(leaf->getPosition()+m_receiverTree.getMinNodeSize()/2);
287 auto callbackForOctree(boost::bind(&sender<t_sync, t_receiver>::isInSyncRangeSync,
this, sync, vector3(centerLeaf.x, centerLeaf.y, centerLeaf.z), _1));
288 const typename octree::search<t_receiver>::t_dataList result(octree::search<t_receiver>::getDataByUserDefinedFunction(m_receiverTree, callbackForOctree));
290 typename t_syncToReceiversMap::const_iterator it = m_syncReceivers.find(sync);
291 BASSERT(it != m_syncReceivers.cend());
294 const t_receiverList receivers(it->second);
296 for (t_receiver receiver : receivers)
298 if (result.find(receiver) == result.cend())
300 removeLinkSyncReceiverMaster(receiver, sync);
306 for (t_receiver receiver : result)
308 if (receivers.find(receiver) == receivers.cend())
310 addLinkSyncReceiverMaster(receiver, sync);
316 void addLinkSyncReceiverMaster(
const t_receiver receiver,
const t_sync sync)
319 typename t_syncToReceiversMap::iterator itSync = m_syncReceivers.find(sync);
320 BASSERT(itSync != m_syncReceivers.cend());
321 BASSERT(itSync->second.find(receiver) == itSync->second.cend());
322 itSync->second.insert(receiver);
325 typename t_receiverToSyncsMap::iterator itReceiver = m_receiverSyncs.find(receiver);
326 BASSERT(itReceiver != m_receiverSyncs.cend());
327 BASSERT(itReceiver->second.find(sync) == itReceiver->second.cend());
328 itReceiver->second.insert(sync);
330 addSyncReceiver(receiver, sync);
332 void removeLinkSyncReceiverMaster(
const t_receiver receiver,
const t_sync sync)
335 typename t_syncToReceiversMap::iterator itSync = m_syncReceivers.find(sync);
336 BASSERT(itSync != m_syncReceivers.cend());
337 typename t_receiverList::const_iterator itReceiver = itSync->second.find(receiver);
338 BASSERT(itReceiver != itSync->second.cend());
339 itSync->second.erase(itReceiver);
342 typename t_receiverToSyncsMap::iterator itReceiver = m_receiverSyncs.find(receiver);
343 BASSERT(itReceiver != m_receiverSyncs.cend());
344 typename t_syncList::const_iterator itSync = itReceiver->second.find(sync);
345 BASSERT(itSync != itReceiver->second.cend());
346 itReceiver->second.erase(itSync);
348 removeSyncReceiver(receiver, sync);
351 virtual bool isInSyncRangeReceiver(
const t_receiver receiver,
const vector3& posOfReceiverLeafCenter,
const typename t_syncTree::t_nodePtr& octreeNode)
353 return m_callbackInSyncRangeReceiver(receiver, posOfReceiverLeafCenter, octreeNode);
356 virtual bool isInSyncRangeSync(
const t_sync sync,
const vector3& posOfSyncLeafCenter,
const typename t_receiverTree::t_nodePtr& octreeNode)
358 return m_callbackInSyncRangeSync(sync, posOfSyncLeafCenter, octreeNode);
361 virtual void addSyncReceiver(
const t_receiver receiver,
const t_sync sync)
363 m_sigAdd(receiver, sync);
366 virtual void removeSyncReceiver(
const t_receiver receiver,
const t_sync sync)
368 m_sigRemove(receiver, sync);
372 async::strand m_master;
374 t_syncTree m_syncTree;
375 t_syncToReceiversMap m_syncReceivers;
376 t_syncPosMap m_syncPosMap;
378 t_receiverTree m_receiverTree;
379 t_receiverToSyncsMap m_receiverSyncs;
380 t_receiverPosMap m_receiverPosMap;
382 t_callbackInSyncRangeReceiver m_callbackInSyncRangeReceiver;
383 t_callbackInSyncRangeSync m_callbackInSyncRangeSync;
386 t_sigRemove m_sigRemove;
396 #endif // SYNC_SENDER_HPP
Definition: strand.hpp:16
Definition: dispatcher.hpp:29
Definition: deadlineTimer.hpp:10