hmbdc
simplify-high-performance-messaging-programming
Transport.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include "hmbdc/app/utils/EpollTask.hpp"
5 #include "hmbdc/app/Base.hpp"
6 #include "hmbdc/text/StringTrieSet.hpp"
7 #include "hmbdc/comm/Topic.hpp"
8 #include "hmbdc/comm/inet/Misc.hpp"
9 #include "hmbdc/time/Timers.hpp"
10 #include "hmbdc/pattern/MonoLockFreeBuffer.hpp"
11 
12 #include <boost/regex.hpp>
13 #include <stdexcept>
14 #include <memory>
15 #include <string>
16 
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 
22 namespace hmbdc { namespace app { namespace tcpcast {
23 
25  EpollFd(Config const& cfg)
26  : localAddr{0} {
27  fd = socket(AF_INET, SOCK_STREAM, 0);
28  if(fd < 0) {
29  HMBDC_THROW(std::runtime_error, "failed to create socket, errno=" << errno);
30  }
31 
32  int flags = fcntl(fd, F_GETFL, 0);
33  flags |= O_NONBLOCK;
34  if (fcntl(fd, F_SETFL, flags) < 0) {
35  HMBDC_THROW(std::runtime_error, "fcntl failed errno=" << errno);
36  }
37 
38  localAddr.sin_family = AF_INET;
39  localAddr.sin_addr.s_addr = inet_addr(hmbdc::comm::inet::getLocalIpMatchMask(
40  cfg.getExt<std::string>("ifaceAddr")).c_str());
41  localAddr.sin_port = htons(cfg.getExt<uint16_t>("tcpPort"));
42  if (bind(fd, (sockaddr*)&localAddr, sizeof(localAddr)) < 0) {
43  HMBDC_THROW(std::runtime_error, "failed to bind, errno=" << errno);
44  }
45 
46  auto addrLen = socklen_t(sizeof(localAddr));
47  if (getsockname(fd, (sockaddr*)&localAddr, &addrLen) < 0) {
48  HMBDC_THROW(std::runtime_error, "getsockname failure, errno=" << errno);
49  }
50 
51  char ipaddr[INET_ADDRSTRLEN];
52  if (!inet_ntop(AF_INET, &(localAddr.sin_addr), ipaddr, INET_ADDRSTRLEN)) {
53  HMBDC_THROW(std::runtime_error, "failed to inet_ntop, errno=" << errno);
54  }
55  localIp = ipaddr;
56  localPort = htons(localAddr.sin_port);
57  }
58 
59  sockaddr_in localAddr;
60  std::string localIp;
61  uint16_t localPort;
62 };
63 
64 struct Transport {
65  using ptr = std::shared_ptr<Transport>;
66 
67  Transport(Config const& cfg)
68  : config_(cfg)
69  , tcpcastAdTopic_("tcpcastad") {
70  cfg (hmbdcName_, "hmbdcName")
71  (schedPolicy_, "schedPolicy")
72  (schedPriority_, "schedPriority")
73  ;
74  }
75 
76  char const* hmbdcName() const {
77  return this->hmbdcName_.c_str();
78  }
79 
80  std::tuple<char const*, int> schedSpec() const {
81  return std::make_tuple(this->schedPolicy_.c_str(), this->schedPriority_);
82  }
83 
84  bool operator == (Transport const& other ) const {
85  return &config_ == &other.config_;
86  }
87 
88  bool operator < (Transport const& other ) const {
89  return &config_ < &other.config_;
90  }
91 
92  virtual ~Transport(){}
93 
94 protected:
95  Config const config_;
96  int schedPriority_;
97  Topic const tcpcastAdTopic_;
98 
99 private:
100  std::string hmbdcName_;
101  std::string schedPolicy_;
102 };
103 }}}
class to hold an hmbdc configuration
Definition: Config.hpp:44
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
T getExt(const path_type &param) const
get a value from the config
Definition: Config.hpp:154
Definition: EpollTask.hpp:31
Definition: Transport.hpp:64
Definition: Transport.hpp:24
Definition: Base.hpp:12