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/Config.hpp"
6 #include "hmbdc/text/StringTrieSet.hpp"
7 #include "hmbdc/comm/inet/Misc.hpp"
8 #include "hmbdc/time/Timers.hpp"
9 
10 #include <memory>
11 
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/ip.h>
15 #include <arpa/inet.h>
16 
17 namespace hmbdc { namespace app { namespace mcast {
18 
20  EpollFd(Config const& cfg) {
21  using namespace std;
22  fd = socket(AF_INET, SOCK_DGRAM, 0);
23  if(fd < 0) {
24  HMBDC_THROW(runtime_error, "failed to create socket");
25  }
26  struct in_addr localInterface;
27  memset(&mcAddr, 0, sizeof(mcAddr));
28  mcAddr.sin_family = AF_INET;
29  mcAddr.sin_addr.s_addr = inet_addr(cfg.getExt<string>("mcastAddr").c_str());
30  mcAddr.sin_port = htons(cfg.getExt<short>("mcastPort"));
31  auto iface =
32  comm::inet::getLocalIpMatchMask(cfg.getExt<string>("ifaceAddr"));
33  localInterface.s_addr = inet_addr(iface.c_str());
34  if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF
35  , (char *)&localInterface, sizeof(localInterface)) < 0) {
36  HMBDC_THROW(runtime_error, "failed to set ifaceAddr " << cfg.getExt<string>("ifaceAddr"));
37  }
38  }
39 
40  struct sockaddr_in mcAddr;
41 };
42 
43 struct Transport : EpollFd {
44  using ptr = std::shared_ptr<Transport>;
45 
46  Transport(Config const& cfg)
47  : EpollFd(cfg)
48  , config_(cfg)
49  , mtu_(config_.getExt<size_t>("mtu")) {
50  mtu_ -= (8u + 20u); // 8bytes udp header and 20bytes ip header
51  cfg (hmbdcName_, "hmbdcName")
52  (schedPolicy_, "schedPolicy")
53  (schedPriority_, "schedPriority")
54  ;
55  }
56 
57  char const* hmbdcName() const {
58  return this->hmbdcName_.c_str();
59  }
60 
61  std::tuple<char const*, int> schedSpec() const {
62  return std::make_tuple(this->schedPolicy_.c_str(), this->schedPriority_);
63  }
64 
65  bool operator == (Transport const& other ) const {
66  return &config_ == &other.config_;
67  }
68 
69  bool operator < (Transport const& other ) const {
70  return &config_ < &other.config_;
71  }
72 
73 protected:
74  std::string hmbdcName_;
75  std::string schedPolicy_;
76  int schedPriority_;
77  Config const config_;
78  size_t mtu_;
79 };
80 
81 }}}
class to hold an hmbdc configuration
Definition: Config.hpp:44
T getExt(const path_type &param) const
get a value from the config
Definition: Config.hpp:154
Definition: TypedString.hpp:74
Definition: EpollTask.hpp:31
Definition: Transport.hpp:19
Definition: Transport.hpp:43
Definition: Base.hpp:12