i6engine  1.0
MessageStruct.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_MESSAGESTRUCT_H__
26 #define __I6ENGINE_CORE_MESSAGESTRUCT_H__
27 
29 
30 #include "boost/archive/text_iarchive.hpp"
31 #include "boost/archive/text_oarchive.hpp"
32 
33 #ifdef ISIXE_WITH_PROFILING
34  #include "boost/date_time/posix_time/posix_time.hpp"
35  #include "boost/date_time/posix_time/time_serialize.hpp"
36 #endif /* ISIXE_WITH_PROFILING */
37 
38 #include "boost/serialization/export.hpp"
39 #include "boost/serialization/list.hpp"
40 #include "boost/serialization/map.hpp"
41 #include "boost/serialization/vector.hpp"
42 
43 #ifdef ISIXE_WITH_PROFILING
44  #include "boost/thread/mutex.hpp"
45 #endif /* ISIXE_WITH_PROFILING */
46 
47 namespace i6e {
48 namespace core {
49 
50  typedef struct ISIXE_CORE_API MessageStruct {
51  int64_t _id;
53  int64_t _waitForId;
54 
55 #ifdef ISIXE_WITH_PROFILING
56  std::vector<std::pair<std::string, boost::posix_time::ptime>> _timestamps;
57  mutable boost::mutex _lock;
58 #endif /* ISIXE_WITH_PROFILING */
59 
60  template<class Archive>
61  void serialize(Archive & ar, const unsigned int /*version*/) {
62  ar & _id;
63  ar & _sender;
64  ar & _waitForId;
65 #ifdef ISIXE_WITH_PROFILING
66  boost::mutex::scoped_lock sl(_lock);
67  ar & _timestamps;
68 #endif /* ISIXE_WITH_PROFILING */
69  }
70 
71 #ifdef ISIXE_WITH_PROFILING
72 
75  MessageStruct() : _id(-1), _sender(), _waitForId(-1), _timestamps(), _lock() {
76  assert(_timestamps.size() == 0);
77  insertTimestamp("Create Default c'tor");
78  assert(_timestamps.size() == 1);
79  }
80 
84  MessageStruct(const MessageStruct & other): _id(other._id), _sender(other._sender), _waitForId(other._waitForId), _lock() {
85  other._lock.lock();
86  assert(_timestamps.size() == 0);
87  _timestamps = other._timestamps;
88  assert(_timestamps.size() == other._timestamps.size());
89  insertTimestamp("Copy");
90  assert(_timestamps.size() == other._timestamps.size() + 1);
91  other._lock.unlock();
92  }
93 
100  MessageStruct(const int64_t id, const IPKey & send = IPKey(), const int64_t waitID = -1) : _id(id), _sender(send), _waitForId(waitID), _timestamps(), _lock() {
101  assert(_timestamps.size() == 0);
102  insertTimestamp("Create");
103  assert(_timestamps.size() == 1);
104  }
105 
106  MessageStruct(int64_t id, int64_t waitID) : _id(id), _sender(), _waitForId(waitID), _timestamps(), _lock() {}
107 #else /* ISIXE_WITH_PROFILING */
108 
111  MessageStruct() : _id(-1), _sender(), _waitForId(-1) {}
112 
116  MessageStruct(const MessageStruct & other) : _id(other._id), _sender(other._sender), _waitForId(other._waitForId) {
117  }
118 
125  MessageStruct(const int64_t id, const IPKey & send, const int64_t waitID) : _id(id), _sender(send), _waitForId(waitID) {
126  }
127 
128  MessageStruct(int64_t id, int64_t waitID) : _id(id), _sender(), _waitForId(waitID) {
129  }
130 #endif /* ISIXE_WITH_PROFILING */
131 
132 
136  virtual ~MessageStruct() {}
137 
142  virtual MessageStruct * copy() { return new MessageStruct(*this); }
143 
144 #ifdef ISIXE_WITH_PROFILING
145 
149  void insertTimestamp(const std::string & text) {
150  _lock.lock();
151  size_t n = _timestamps.size();
152  boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time();
153  _timestamps.push_back(std::make_pair(text, time));
154 
155  assert(_timestamps.size() == n + 1);
156  _lock.unlock();
157  }
158 
163  std::string getTimestamps() {
164  std::stringstream ret;
165  ret << "\n\t\t" << _timestamps[0].first << "\n";
166  for (size_t i = 1; i < _timestamps.size(); ++i) {
167  boost::posix_time::time_duration diff = boost::posix_time::time_period(_timestamps[i - 1].second, _timestamps[i].second).length();
168  if (diff.total_milliseconds() < 15) {
169  ret << "\t[\x1b[32m" << boost::posix_time::to_simple_string(diff) << "\x1b[0m]\n";
170  } else {
171  ret << "\t[\x1b[31m" << boost::posix_time::to_simple_string(diff) << "\x1b[0m]\n";
172  }
173  ret << "\t\t" << _timestamps[i].first << "\n";
174  }
175  return ret.str();
176  }
177 #endif /* ISIXE_WITH_PROFILING */
178 
182  int64_t getID() const {
183  return _id;
184  }
185 
189  int64_t getWaitID() const {
190  return _waitForId;
191  }
192  } MessageStruct;
193 
194 } /* namespace core */
195 } /* namespace i6e */
196 
197 #endif /* __I6ENGINE_CORE_MESSAGESTRUCT_H__ */
198 
MessageStruct(int64_t id, int64_t waitID)
void serialize(Archive &ar, const unsigned int)
Definition: MessageStruct.h:61
MessageStruct(const int64_t id, const IPKey &send, const int64_t waitID)
Constructor for MessageStruct.
virtual ~MessageStruct()
Virtual destructor.
virtual MessageStruct * copy()
Copy method returning an exact copy of itself.
int64_t getID() const
returns the id
MessageStruct(const MessageStruct &other)
Copy constructor for MessageStruct.
int64_t getWaitID() const
returns the waitForId
MessageStruct()
Default constructor for MessageStruct.
#define ISIXE_CORE_API
struct ISIXE_CORE_API i6e::core::MessageStruct MessageStruct