m2etis  0.4
UdpWrapper.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_UDP_UDPWRAPPER_H__
23 #define __M2ETIS_WRAPPER_UDP_UDPWRAPPER_H__
24 
25 #include "boost/asio.hpp"
26 
27 #include <deque>
28 
31 #include "m2etis/net/NetworkType.h"
32 #include "m2etis/net/NodeHandle.h"
33 
34 #include "boost/make_shared.hpp"
35 #include "boost/thread.hpp"
36 
37 namespace m2etis {
38 namespace wrapper {
39 namespace udp {
40 
44  class UdpWrapper : public net::NetworkInterface<net::NetworkType<net::UDP>> {
45  public:
46  UdpWrapper(const std::string & ownIP, uint16_t listenPort, const std::string & hostIP, uint16_t hostPort);
47 
48  ~UdpWrapper();
49 
54 
59 
64  if (!_initialized) {
65  throw std::runtime_error("UDPWrapper: not initialized. call init first!");
66  }
67 
68  net::NodeHandle<net::NetworkType<net::UDP>>::Ptr node = boost::make_shared<net::NodeHandle<net::NetworkType<net::UDP>>>();
69  std::stringstream os;
70  // M2ETIS_LOG_DEBUG("TCPWrapper::getSelfNodeHandle", "Address: " + ownIP)
71  os << _name << ":" << _listenPort;
72  net::NetworkType<net::UDP>::Key key(os.str());
73  node->key_ = key;
74  node->hostname_ = _name;
75  node->port_ = _listenPort;
76 
77  return node;
78  }
79 
83  void registerMessageType(const message::MessageType type, const bool ack) const;
84 
89  return _root;
90  }
91 
92  private:
93  typedef std::pair<std::vector<unsigned char>, message::Key<message::IPv4KeyProvider>> msgPair;
94 
95  bool _initialized;
96  std::string _name;
97  std::string _hostName;
98  uint16_t _listenPort;
99  uint16_t _hostPort;
100  boost::asio::io_service _io_service;
101  boost::asio::ip::udp::socket * _socket;
103 
104  std::vector<boost::thread *> threads_;
105  boost::array<uint8_t, 1048576> recv_buf;
106 
107  boost::asio::io_service::strand _strand__;
108  // buffers messages till they got sent
109  std::deque<msgPair> _outbox;
110  boost::asio::io_service::work _work; // keeps the io_service running
111 
112  boost::asio::ip::udp::endpoint * _endpoint;
113  boost::asio::ip::udp::endpoint * _remote_endpoint;
114 
115  void write(const std::vector<uint8_t> & message, message::Key<message::IPv4KeyProvider> key);
116 
117  void writeImpl(const std::vector<uint8_t> & message, message::Key<message::IPv4KeyProvider> key);
118 
119  void write();
120 
121  void workerFunc();
122 
123  void handleReceive(boost::asio::ip::udp::socket * socket, std::string message, boost::asio::ip::udp::endpoint * endpoint, size_t len);
124  void handleReceive(const boost::system::error_code & error, size_t len, boost::asio::ip::udp::endpoint * re);
125 
126  UdpWrapper(const UdpWrapper &) = delete;
127  UdpWrapper & operator=(const UdpWrapper & rhs) = delete;
128  };
129 
130 } /* namespace udp */
131 } /* namespace wrapper */
132 } /* namespace m2etis */
133 
134 #endif /* __M2ETIS_WRAPPER_UDP_UDPWRAPPER_H__ */
135 
void send(const message::NetworkMessage< net::NetworkType< net::UDP >>::Ptr msg, net::NodeHandle< net::NetworkType< net::UDP >>::Ptr_const hint)
calls the other send method
std::string hostname_
Definition: NodeHandle.h:52
NodeHandle in different structured p2p-systems.
Definition: NodeHandle.h:44
net::NetworkType< net::UDP >::Key getRoot() const
returns root node
Definition: UdpWrapper.h:88
net::NodeHandle< net::NetworkType< net::UDP > >::Ptr getSelfNodeHandle() const
returns pointer with own data
Definition: UdpWrapper.h:63
void registerMessageType(const message::MessageType type, const bool ack) const
Not used right now.
NetworkType::Key key_
Definition: NodeHandle.h:51
creates connections using boosts UdpSocket
Definition: UdpWrapper.h:44
UdpWrapper(const std::string &ownIP, uint16_t listenPort, const std::string &hostIP, uint16_t hostPort)
uint32_t MessageType
Definition: MessageType.h:35