m2etis  0.4
ChimeraWrapperImpl.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 
17 #ifndef __M2ETIS_WRAPPER_CHIMERA_CHIMERAWRAPPERIMPL_H__
18 #define __M2ETIS_WRAPPER_CHIMERA_CHIMERAWRAPPERIMPL_H__
19 
20 #include <stdexcept>
21 #include <memory>
22 
24 
27 #include "m2etis/net/NodeHandle.h"
28 
30 
31 #include "boost/date_time/posix_time/posix_time_types.hpp"
32 #include "boost/thread.hpp"
33 
34 extern "C" {
35  #include "chimera/chimera.h"
36 }
37 
38 namespace m2etis {
39 namespace wrapper {
40 namespace chimera {
41 
42  class ChimeraWrapper;
43 
50  friend class ChimeraWrapper;
51 
52  private:
53  const static int CHIMERA_MESSAGE_TYPE_BASE = 15;
54 
55  ChimeraWrapperImpl() : _cb(nullptr), _initialized(false), _me(new ChimeraHost), _state(new ChimeraState), periodic_(boost::posix_time::milliseconds(3000)), purge_distance_(boost::posix_time::milliseconds(6000)), neighbors_(), neighbors_mutex_(), purging_(true), purger_(boost::bind(&ChimeraWrapperImpl::manageHeartbeats, this)), map_mutex_(), map_() {
56  _impl = this;
57  }
58 
60 
61  const ChimeraWrapperImpl & operator =(const ChimeraWrapperImpl &);
62 
63  bool _initialized;
64  typedef std::shared_ptr<ChimeraHost> HostPtr;
65  HostPtr _me;
66  typedef std::shared_ptr<ChimeraState> StatePtr;
67  StatePtr _state;
68 
69  inline static int encodeChimeraMessageType(message::MessageType type) {
70  return CHIMERA_MESSAGE_TYPE_BASE + static_cast<int>(type);
71  }
72 
73  inline static message::MessageType decodeChimeraMessagetype(int ctype) {
74  return static_cast<message::MessageType>(ctype - CHIMERA_MESSAGE_TYPE_BASE);
75  }
76 
77  void setUpcalls();
78  void post_init();
79 
80  std::list<net::NodeHandle<net::NetworkType<net::CHIMERA> >::Ptr> createList(ChimeraHost **, int size) const;
81 
82  static ChimeraWrapperImpl * _impl;
83 
84  static void forward_upcall(Key **, Message **, ChimeraHost **);
85  static void update_upcall(Key * k, ChimeraHost * h, int joined);
86  static void deliver_upcall(Key * k, Message * m);
87 
88  const boost::posix_time::time_duration periodic_;
89  const boost::posix_time::time_duration purge_distance_;
90 
91  typedef std::pair<boost::posix_time::ptime, net::NetworkType<net::CHIMERA>::Key> TimePair;
92  typedef std::vector<TimePair> TimeList;
93  typedef boost::unique_lock<boost::mutex> Guard;
94  typedef std::map<net::NetworkType<net::CHIMERA>::Key, ChimeraHost *> KeyHostMap;
95 
96  TimeList neighbors_;
97  mutable boost::mutex neighbors_mutex_;
98  volatile bool purging_;
99  boost::thread purger_;
100  mutable boost::mutex map_mutex_;
101  KeyHostMap map_;
102 
103  void processHeartbeat(const net::NetworkType<net::CHIMERA>::Key & from);
104  void manageHeartbeats();
105  void sendHeartbeat(const net::NetworkType<net::CHIMERA>::Key & to, bool ack = false);
106 
107  public:
109  purging_ = false;
110  purger_.join();
111  }
112 
113  void init(const int port);
114  void init(const int port, const std::string & known_hostname, const int know_hostport);
115 
116  void send(const typename message::NetworkMessage<net::NetworkType<net::CHIMERA>>::Ptr msg, typename net::NodeHandle<net::NetworkType<net::CHIMERA>>::Ptr_const hint);
117 
118  void send(const typename message::NetworkMessage<net::NetworkType<net::CHIMERA>>::Ptr msg);
119 
121 
122  std::list<net::NodeHandle<net::NetworkType<net::CHIMERA> >::Ptr> local_lookup(const net::NetworkType<net::CHIMERA>::Key & key, const int num, const bool safe) const;
123 
125 
126  std::list<net::NodeHandle<net::NetworkType<net::CHIMERA> >::Ptr> neighborSet(const int num) const;
127 
128  std::list<net::NodeHandle<net::NetworkType<net::CHIMERA> >::Ptr> replicaSet(const net::NetworkType<net::CHIMERA>::Key & key, const int maxRank) const;
129 
130  bool range(const net::NodeHandle<net::NetworkType<net::CHIMERA> >::Ptr node, const int rank, const net::NetworkType<net::CHIMERA>::Key & lkey, const net::NetworkType<net::CHIMERA>::Key & rkey) const;
131 
132  void registerMessageType(const message::MessageType type, const bool ack) const;
133 
134  // MessagePtr_w createMessage(const int type, const Key& dest, const std::string& payload) const;
135  };
136 
137 } /* namespace chimera */
138 } /* namespace wrapper */
139 } /* namespace m2etis */
140 
141 #endif /* __M2ETIS_WRAPPER_CHIMERA_CHIMERAWRAPPERIMPL_H__ */
std::list< net::NodeHandle< net::NetworkType< net::CHIMERA > >::Ptr > replicaSet(const net::NetworkType< net::CHIMERA >::Key &key, const int maxRank) const
NodeHandle in different structured p2p-systems.
Definition: NodeHandle.h:44
void send(const typename message::NetworkMessage< net::NetworkType< net::CHIMERA >>::Ptr msg, typename net::NodeHandle< net::NetworkType< net::CHIMERA >>::Ptr_const hint)
bool isResponsibleFor(const net::NetworkType< net::CHIMERA >::Key &key, const net::NetworkType< net::CHIMERA >::Key &node) const
net::NodeHandle< net::NetworkType< net::CHIMERA > >::Ptr getSelfNodeHandle() const
The C++-wrapper around chimera.
std::list< net::NodeHandle< net::NetworkType< net::CHIMERA > >::Ptr > neighborSet(const int num) const
bool range(const net::NodeHandle< net::NetworkType< net::CHIMERA > >::Ptr node, const int rank, const net::NetworkType< net::CHIMERA >::Key &lkey, const net::NetworkType< net::CHIMERA >::Key &rkey) const
void registerMessageType(const message::MessageType type, const bool ack) const
std::list< net::NodeHandle< net::NetworkType< net::CHIMERA > >::Ptr > local_lookup(const net::NetworkType< net::CHIMERA >::Key &key, const int num, const bool safe) const
uint32_t MessageType
Definition: MessageType.h:35