m2etis  0.4
clockTcpWrapper.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_WRAPPER_CLOCKTCPWRAPPER_H__
23 #define __M2ETIS_WRAPPER_CLOCKTCPWRAPPER_H__
24 
27 #include "m2etis/net/NetworkType.h"
28 #include "m2etis/net/NodeHandle.h"
29 
30 #include "boost/make_shared.hpp"
31 #include "boost/thread/mutex.hpp"
32 
33 namespace boost {
34  class thread;
35 } /* namespace boost */
36 namespace clockUtils {
37 namespace sockets {
38  class TcpSocket;
39 } /* namespace sockets */
40 } /* namespace clockUtils */
41 
42 namespace m2etis {
43 namespace wrapper {
44 namespace clocktcp {
45 
49  class clockTcpWrapper : public net::NetworkInterface<net::NetworkType<net::clockTCP>> {
50  public:
54  clockTcpWrapper(const std::string & listenIP, const uint16_t listenPort, const std::string & connectIP, const uint16_t connectPort);
55 
60 
67 
73  if (!_initialized) {
74  throw std::runtime_error("clockTCPWrapper: not initialized. call init() first!");
75  }
76 
77  net::NodeHandle<net::NetworkType<net::clockTCP>>::Ptr node = boost::make_shared<net::NodeHandle<net::NetworkType<net::clockTCP>>>();
78  node->key_ = _local;
79  node->hostname_ = _local.ipStr();
80  node->port_ = _local.getPort();
81 
82  return node;
83  }
84 
88  void registerMessageType(const message::MessageType type, const bool ack) const;
89 
90  private:
91  clockTcpWrapper(const clockTcpWrapper &) = delete;
92  clockTcpWrapper & operator=(const clockTcpWrapper & rhs) = delete;
93 
95 
97 
98  void eraseSocket(net::NetworkType<net::clockTCP>::Key realKey);
99 
100  bool _initialized;
101 
103 
104  boost::mutex _lock; // used to lock _sockets
105  std::map<net::NetworkType<net::clockTCP>::Key, clockUtils::sockets::TcpSocket *> _sockets;
106 
110  void readFromSocket(clockUtils::sockets::TcpSocket * oldSocket);
111 
112 
113  std::map<net::NetworkType<net::clockTCP>::Key, net::NetworkType<net::clockTCP>::Key> _mapping_metis_real;
114  std::map<net::NetworkType<net::clockTCP>::Key, net::NetworkType<net::clockTCP>::Key> _mapping_real_metis;
115  boost::mutex _mapLock;
116 
120  std::multimap<uint32_t, boost::thread *> _threads;
121  };
122 
123 } /* namespace clocktcp */
124 } /* namespace wrapper */
125 } /* namespace m2etis */
126 
127 #endif /* __M2ETIS_WRAPPER_CLOCKTCPWRAPPER_H__ */
128 
std::string hostname_
Definition: NodeHandle.h:52
NodeHandle in different structured p2p-systems.
Definition: NodeHandle.h:44
void registerMessageType(const message::MessageType type, const bool ack) const
Doesn't do anything right now.
~clockTcpWrapper()
closes socket and all its connections
clockTcpWrapper(const std::string &listenIP, const uint16_t listenPort, const std::string &connectIP, const uint16_t connectPort)
starts a TcpSocket connection listening
NetworkType::Key key_
Definition: NodeHandle.h:51
void send(const message::NetworkMessage< net::NetworkType< net::clockTCP >>::Ptr msg, net::NodeHandle< net::NetworkType< net::clockTCP >>::Ptr_const hint)
sends a message
net::NodeHandle< net::NetworkType< net::clockTCP > >::Ptr getSelfNodeHandle() const
returns pointer with data
uint32_t MessageType
Definition: MessageType.h:35
creates connections using clockUtils' TcpSocket