hmbdc
simplify-high-performance-messaging-programming
 All Classes Namespaces Functions Variables Friends Pages
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 <regex>
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  uint32_t yes = 1;
39  if (setsockopt(fd, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
40  HMBDC_LOG_C("failed to set reuse address errno=", errno);
41  }
42 
43  localAddr.sin_family = AF_INET;
44  localAddr.sin_addr.s_addr = inet_addr(hmbdc::comm::inet::getLocalIpMatchMask(
45  cfg.getExt<std::string>("ifaceAddr")).c_str());
46  localAddr.sin_port = htons(cfg.getExt<uint16_t>("tcpPort"));
47  if (bind(fd, (sockaddr*)&localAddr, sizeof(localAddr)) < 0) {
48  HMBDC_THROW(std::runtime_error, "failed to bind, errno=" << errno);
49  }
50 
51  auto addrLen = socklen_t(sizeof(localAddr));
52  if (getsockname(fd, (sockaddr*)&localAddr, &addrLen) < 0) {
53  HMBDC_THROW(std::runtime_error, "getsockname failure, errno=" << errno);
54  }
55 
56  char ipaddr[INET_ADDRSTRLEN];
57  if (!inet_ntop(AF_INET, &(localAddr.sin_addr), ipaddr, INET_ADDRSTRLEN)) {
58  HMBDC_THROW(std::runtime_error, "failed to inet_ntop, errno=" << errno);
59  }
60  localIp = ipaddr;
61  localPort = htons(localAddr.sin_port);
62  }
63 
64  sockaddr_in localAddr;
65  std::string localIp;
66  uint16_t localPort;
67 };
68 
69 struct Transport {
70  using ptr = std::shared_ptr<Transport>;
71 
72  Transport(Config const& cfg)
73  : config_(cfg)
74  , tcpcastAdTopic_("__tcpcastad") {
75  cfg (hmbdcName_, "hmbdcName")
76  (schedPolicy_, "schedPolicy")
77  (schedPriority_, "schedPriority")
78  ;
79  }
80 
81  char const* hmbdcName() const {
82  return this->hmbdcName_.c_str();
83  }
84 
85  std::tuple<char const*, int> schedSpec() const {
86  return std::make_tuple(this->schedPolicy_.c_str(), this->schedPriority_);
87  }
88 
89  bool operator == (Transport const& other ) const {
90  return &config_ == &other.config_;
91  }
92 
93  bool operator < (Transport const& other ) const {
94  return &config_ < &other.config_;
95  }
96 
97  virtual ~Transport(){}
98 
99 protected:
100  Config const config_;
101  int schedPriority_;
102  Topic const tcpcastAdTopic_;
103 
104 private:
105  std::string hmbdcName_;
106  std::string schedPolicy_;
107 };
108 }}}
class to hold an hmbdc configuration
Definition: Config.hpp:46
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
Definition: EpollTask.hpp:70
Definition: Transport.hpp:69
T getExt(const path_type &param, bool throwIfMissing=true) const
get a value from the config
Definition: Config.hpp:225
Definition: Transport.hpp:24