voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
receiver.hpp
1 #ifndef NETWORK_SYNC_VOXEL_ACCESSOR_MULTIPLETILES_RECEIVER_HPP
2 #define NETWORK_SYNC_VOXEL_ACCESSOR_MULTIPLETILES_RECEIVER_HPP
3 
4 #include "blub/core/byteArray.hpp"
5 #include "blub/log/global.hpp"
6 #include "blub/core/sharedPointer.hpp"
7 #include "blub/async/dispatcher.hpp"
8 #include "blub/sync/voxel/accessor/multipleTiles/base.hpp"
9 #include "blub/procedural/voxel/simple/accessor.hpp"
10 #include "blub/procedural/voxel/simple/base.hpp"
11 #include "blub/procedural/voxel/tile/accessor.hpp"
12 
13 #include <boost/iostreams/copy.hpp>
14 #include <boost/iostreams/filter/bzip2.hpp>
15 #include <boost/iostreams/filtering_stream.hpp>
16 #include <boost/iostreams/stream.hpp>
17 
18 
19 
20 namespace blub
21 {
22 namespace sync
23 {
24 namespace voxel
25 {
26 namespace accessor
27 {
28 namespace multipleTiles
29 {
30 
31 
32 template <class voxelType>
33 class receiver : public procedural::voxel::simple::base<sharedPointer<procedural::voxel::tile::accessor<voxelType> > >
34 {
35 public:
40  typedef typename t_base::t_tileId t_tileId;
41 
42 
43  receiver(blub::async::dispatcher * todoListenerMaster, const int32& lod = 0)
44  : t_base(*todoListenerMaster)
45  , m_lod(lod)
46  {
48  }
49 
50  // "receive sync"
51  void receivedTileData(const byteArray& data)
52  {
53  t_base::m_master.post(boost::bind(&receiver::receivedTileDataMaster, this, data));
54  }
55  void receivedTilePtrData(t_tileDataPtr data)
56  {
57  BASSERT(!data.isNull());
58 
59  t_base::m_master.post(boost::bind(&receiver::receivedTileDataPtrMaster, this, data));
60  }
61 
62 protected:
63  void receivedTileDataMaster(const byteArray& data)
64  {
65  sendType type;
66 
67  std::istringstream input(std::string(data.data(), data.size()));
69  format >> type;
70 
71  if (type == sendType::lockForEdit)
72  {
73 #ifdef BLUB_LOG_VOXEL_ACCESSOR_SYNC
74  blub::BOUT("type == sendType::lockForEdit id:" + blub::string::number(id));
75 #endif
77  return;
78  }
79  if (type == sendType::unlockForEdit)
80  {
81 #ifdef BLUB_LOG_VOXEL_ACCESSOR_SYNC
82  blub::BOUT("type == sendType::unlockForEdit id:" + blub::string::number(id));
83 #endif
85  return;
86  }
87 
88  t_tileId id;
89  format >> id;
90 
91  if (type == sendType::removeTile)
92  {
93 #ifdef BLUB_LOG_VOXEL_ACCESSOR_SYNC
94  blub::BOUT("type == sendType::removeTile id:" + blub::string::number(id));
95 #endif
96  t_base::addToChangeList(id, nullptr);
97  return;
98  }
99 
100  BASSERT((uint32)data.size() > sizeof(sendType) + sizeof(t_tileId));
101  BASSERT(type == sendType::setTile);
102 #ifdef BLUB_LOG_VOXEL_ACCESSOR_SYNC
103  blub::BOUT("type == sendType::setTile id:" + blub::string::number(id));
104 #endif
105 
106  byteArray toDecompress;
107  format >> toDecompress;
108 
109  t_tilePtr workTile(t_base::createTile());
110  {
111  std::stringstream toReadFromBuffer;
112  // decompress
113  {
114  boost::iostreams::stream<boost::iostreams::array_source> src(toDecompress.data(), toDecompress.size());
115  boost::iostreams::filtering_istream filterIn;
116  filterIn.push(boost::iostreams::bzip2_decompressor());
117  filterIn.push(src);
118  boost::iostreams::copy(filterIn, toReadFromBuffer);
119  }
120 
121  blub::serialization::format::binary::input toReadFrom(toReadFromBuffer);
122 
123  toReadFrom >> *workTile.get();
124  }
125  t_base::addToChangeList(id, workTile);
126  }
127  void receivedTileDataPtrMaster(t_tileDataPtr data)
128  {
129  receivedTileDataMaster(*data.data());
130  }
131 
132  const int32 m_lod;
133 
134 };
135 
136 
137 
138 
139 }
140 }
141 }
142 }
143 }
144 
145 
146 
147 
148 
149 #endif // NETWORK_SYNC_VOXEL_ACCESSOR_MULTIPLETILES_RECEIVER_HPP
The base class gets derived by every class in the namespace simple::*. It represends one level of det...
Definition: predecl.hpp:30
virtual void lockForEditMaster()
lockForEditMaster locks for write, or waits until possible. Call by master dispatcher.
Definition: base.hpp:228
virtual t_tilePtr createTile() const
createTile creates a new Tile. Uses callback set by setCreateTileCallback()
Definition: base.hpp:249
virtual void unlockForEditMaster()
unlockForEditMaster unlocks write. Call by master dispatcher.
Definition: base.hpp:236
Definition: sharedPointer.hpp:12
static t_base::pointer create()
create creates an instance.
Definition: accessor.hpp:63
void addToChangeList(const t_tileId &id, t_tilePtr toAdd)
addToChangeList adds a tile to the change-list.
Definition: base.hpp:210
Definition: dispatcher.hpp:29
The accessor class caches all voxel needed by tile::surface for an extremly optimized and fast calcul...
Definition: predecl.hpp:21
Definition: byteArray.hpp:17
Definition: portable_binary_iarchive.hpp:68
Definition: deadlineTimer.hpp:10
blub::async::strand m_master
m_master The master synchronises jobs for the worker-thread and writes to class member. The master calls all methods which end with *Master.
Definition: base.hpp:143
void setCreateTileCallback(const t_createTileCallback &callback)
setCreateTileCallback sets a callback for creating tiles. Use this method if you want to create custo...
Definition: base.hpp:204
Definition: customVertexInformation.cpp:177