m2etis  0.4
TcpWrapper.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_TCPWRAPPER_H__
23 #define __M2ETIS_WRAPPER_TCPWRAPPER_H__
24 
25 #include "boost/asio.hpp"
26 
27 #include <deque>
28 #include <mutex>
29 #include <set>
30 
33 #include "m2etis/net/NetworkType.h"
34 #include "m2etis/net/NodeHandle.h"
35 
36 #include "boost/make_shared.hpp"
37 #include "boost/thread/mutex.hpp"
38 
39 namespace boost {
40  class thread;
41 } /* namespace boost */
42 
43 namespace m2etis {
44 namespace wrapper {
45 namespace tcp {
46 
50  class TcpWrapper : public net::NetworkInterface<net::NetworkType<net::TCP>> {
51  public:
52  TcpWrapper(const std::string & listenIP, const uint16_t listenPort, const std::string & connectIP, const uint16_t connectPort);
53 
54  ~TcpWrapper();
55 
60 
65 
70  if (!_initialized) {
71  throw std::runtime_error("TCPWrapper: not initialized. call init first!");
72  }
73 
74  net::NodeHandle<net::NetworkType<net::TCP>>::Ptr node = boost::make_shared<net::NodeHandle<net::NetworkType<net::TCP>>>();
75  node->key_ = _local;
76  node->hostname_ = _local.ipStr();
77  node->port_ = _local.getPort();
78 
79  return node;
80  }
81 
85  void registerMessageType(const message::MessageType type, const bool ack) const;
86 
91  return _rendezvouz;
92  }
93 
94  private:
95  typedef std::pair<std::vector<uint8_t>, boost::asio::ip::tcp::socket *> msgPair;
96 
97  bool _initialized;
98 
101 
102  boost::asio::io_service _io_service;
103  boost::asio::ip::tcp::acceptor * _acceptor;
104 
105  boost::mutex lock_; // used to lock _sockets
106  std::map<net::NetworkType<net::TCP>::Key, boost::asio::ip::tcp::socket *> _sockets;
107 
108  boost::asio::io_service::strand _strand__;
109 
110  // buffers messages till they got sent
111  std::deque<msgPair> _outbox;
112  boost::asio::io_service::work * _work; // keeps the io_service running
113 
114  std::map<net::NetworkType<net::TCP>::Key, net::NetworkType<net::TCP>::Key> _mapping_metis_real;
115  std::map<net::NetworkType<net::TCP>::Key, net::NetworkType<net::TCP>::Key> _mapping_real_metis;
116 
117  boost::mutex _mapLock;
118 
119  std::mutex _threadLock;
120  std::multimap<uint16_t, boost::thread *> threads_;
121 
122  std::mutex _deleteSocketsLock;
123  std::set<boost::asio::ip::tcp::socket *> _deleteSockets;
124 
125  void eraseSocket(net::NetworkType<net::TCP>::Key realKey);
126  void workerFunc();
127 
131  void readFromSocket(boost::asio::ip::tcp::socket * oldSocket);
132 
133  void write(const std::vector<uint8_t> & message, boost::asio::ip::tcp::socket * sock);
134 
135  void writeImpl(const std::vector<uint8_t> & message, boost::asio::ip::tcp::socket * sock);
136 
137  void write();
138 
139  void writeHandler(const boost::system::error_code & error, const size_t bytesTransferred);
140 
141  void handleAccept(const boost::system::error_code & error, boost::asio::ip::tcp::socket * socket);
142 
144  auto it = _mapping_real_metis.find(key);
145  if (it == _mapping_real_metis.end()) {
146  return key;
147  }
148  return it->second;
149  }
150 
152  auto it = _mapping_metis_real.find(key);
153  if (it == _mapping_metis_real.end()) {
154  return key;
155  }
156  return it->second;
157  }
158 
159  void removeSocket(boost::asio::ip::tcp::socket * sock);
160 
161  TcpWrapper(const TcpWrapper &) = delete;
162  TcpWrapper & operator=(const TcpWrapper & rhs) = delete;
163  };
164 
165 } /* namespace tcp */
166 } /* namespace wrapper */
167 } /* namespace m2etis */
168 
169 #endif /* __M2ETIS_WRAPPER_TCPWRAPPER_H__ */
170 
creates connections using boosts TcpSocket
Definition: TcpWrapper.h:50
std::string hostname_
Definition: NodeHandle.h:52
net::NetworkType< net::TCP >::Key getRoot() const
returns root node
Definition: TcpWrapper.h:90
NodeHandle in different structured p2p-systems.
Definition: NodeHandle.h:44
net::NodeHandle< net::NetworkType< net::TCP > >::Ptr getSelfNodeHandle() const
returns pointer with own data
Definition: TcpWrapper.h:69
NetworkType::Key key_
Definition: NodeHandle.h:51
void registerMessageType(const message::MessageType type, const bool ack) const
Not used right now.
TcpWrapper(const std::string &listenIP, const uint16_t listenPort, const std::string &connectIP, const uint16_t connectPort)
void send(const message::NetworkMessage< net::NetworkType< net::TCP >>::Ptr msg, net::NodeHandle< net::NetworkType< net::TCP >>::Ptr_const hint)
calls the other send method
uint32_t MessageType
Definition: MessageType.h:35