m2etis  0.4
DetMergeOrderInfo.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
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 
22 #ifndef __M2ETIS_MESSAGE_DETMERGEORDERINFO_H__
23 #define __M2ETIS_MESSAGE_DETMERGEORDERINFO_H__
24 
25 #include <cstdlib>
26 
28 #include "m2etis/util/Logger.h"
29 
30 #include "boost/serialization/base_object.hpp"
31 #include "boost/serialization/shared_ptr.hpp"
32 
33 namespace m2etis {
34 namespace message {
35 
36  template<class Config>
37  class DetMergeOrderInfo : public OrderInfo, public Config {
38  public:
39  typedef boost::shared_ptr<DetMergeOrderInfo> Ptr;
40 
44  static bool doSerialize(ActionType t) {
45  if (t == SUBSCRIBE || t == UNSUBSCRIBE || t == JOIN || t == STATE || t == LEAVE) {
46  return false;
47  }
48  return true;
49  }
50 
54  typedef struct Timestamp {
55  Timestamp() : kn(2 * Config::eps), unique(std::rand()), r(0), c(0) {}
56 
57  template <typename Archive>
58  void serialize(Archive & ar, const unsigned int /*version*/) {
59  ar & r;
60  ar & c;
61  ar & kn;
62  ar & unique;
63  }
64 
65  std::vector<int> kn; // kn.e_j
66  int unique; // unique id (needed to get a pre-defined order with 2 equal Timestamps)
67  uint64_t r; // physical clock on creator
68  int64_t c; // difference to max clock
69 
73  bool operator<(const struct Timestamp & other) {
74  if (r + c == other.r + other.c) {
75  uint64_t e = Config::eps;
76  size_t c1 = size_t(c), c2 = size_t(other.c);
77 
78  while (e != 0) {
79  if (!(c1 < 2 * Config::eps && c2 < 2 * Config::eps)) {
80  return false;
81  }
82  if (kn[c1] != other.kn[c2]) {
83  return kn[c1] < other.kn[c2];
84  }
85  c1--;
86  c2--;
87  e--;
88  }
89 
90  return unique < other.unique;
91  } else {
92  return r + c < other.r + other.c;
93  }
94  }
95 
99  std::string toString() const {
100  std::stringstream ss;
101  ss << "TS: {r: " << r << ", c: " << c << ", kn: ";
102  for (unsigned int t = 0; t < Config::eps * 2; ++t) {
103  ss << kn[t] << ", ";
104  }
105  ss << "u: " << unique << "}";
106  return ss.str();
107  }
108  } Timestamp;
109 
110  typedef boost::shared_ptr<Timestamp> timestamp_p;
111 
112  DetMergeOrderInfo() : ts(boost::make_shared<Timestamp>()) {}
113 
114  DetMergeOrderInfo(const DetMergeOrderInfo & other) : ts(boost::make_shared<Timestamp>(*other.ts)) {}
115 
119  bool operator<(const DetMergeOrderInfo & other) const {
120  return *(ts.get()) < *(other.ts.get());
121  }
122 
126  timestamp_p ts;
127 
128  private:
130  template <typename Archive>
131  void serialize(Archive & ar, const unsigned int /*version*/) {
132  ar & boost::serialization::base_object<OrderInfo>(*this);
133  ar & ts;
134  }
135  };
136 
137 } /* namespace message */
138 } /* namespace m2etis */
139 
140 #endif /* __M2ETIS_MESSAGE_DETMERGEORDERINFO_H__ */
141 
bool operator<(const struct Timestamp &other)
compares two structs
boost::shared_ptr< DetMergeOrderInfo > Ptr
struct m2etis::message::DetMergeOrderInfo::Timestamp Timestamp
represents a timestamp
DetMergeOrderInfo(const DetMergeOrderInfo &other)
STL namespace.
timestamp_p ts
timestamp for this OrderInfo
friend class boost::serialization::access
std::string toString() const
creates a readable string out o f a timestamp
bool operator<(const DetMergeOrderInfo &other) const
compares two orderinfos by their timestamp
void serialize(Archive &ar, const unsigned int)
boost::shared_ptr< Timestamp > timestamp_p
static bool doSerialize(ActionType t)
don't serialise this for SUB and UNSUB msgs