#ifndef FAIRMQTOOLS_H_ #define FAIRMQTOOLS_H_ #ifndef _GNU_SOURCE #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST #endif #include #include #include #include #include #include #include #include #include using namespace std; namespace FairMQ { namespace tools { template unique_ptr make_unique(Args&& ...args) { return unique_ptr(new T(forward(args)...)); } int getHostIPs(map& addressMap) { struct ifaddrs *ifaddr, *ifa; int s; char host[NI_MAXHOST]; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); return -1; } for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL) { continue; } if (ifa->ifa_addr->sa_family == AF_INET) { s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (s != 0) { cout << "getnameinfo() failed: " << gai_strerror(s) << endl; return -1; } addressMap.insert(pair(ifa->ifa_name, host)); } } freeifaddrs(ifaddr); return 0; } string getInterfaceIP(string interface) { map IPs; FairMQ::tools::getHostIPs(IPs); if (IPs.count(interface)) { return IPs[interface]; } else { LOG(ERROR) << "Could not find provided network interface: \"" << interface << "\"!, exiting."; exit(EXIT_FAILURE); } } #if defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #endif // below are SFINAE template functions to check for function member signatures of class namespace details { // test, at compile time, whether T has BindSendHeader member function with returned type R and argument ...Args type template struct has_BindSendHeader : false_type {}; template struct has_BindSendHeader ().BindSendHeader(declval()...)), R>::value || is_same::value>::type >:true_type {}; // test, at compile time, whether T has BindGetSocketNumber member function with returned type R and argument ...Args type template struct has_BindGetSocketNumber : false_type {}; template struct has_BindGetSocketNumber ().BindGetSocketNumber(declval()...)), R>::value || is_same::value>::type >:true_type {}; // test, at compile time, whether T has GetHeader member function with returned type R and argument ...Args type template struct has_GetHeader : false_type {}; template struct has_GetHeader ().GetHeader(declval()...)), R>::value || is_same::value>::type >:true_type {}; // test, at compile time, whether T has BindGetCurrentIndex member function with returned type R and argument ...Args type template struct has_BindGetCurrentIndex : false_type {}; template struct has_BindGetCurrentIndex ().BindGetCurrentIndex(declval()...)), R>::value || is_same::value>::type >:true_type {}; } // end namespace details #if defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic pop #endif // Alias template of the above structs template using has_BindSendHeader = integral_constant::value>; template using has_BindGetSocketNumber = integral_constant::value>; template using has_GetHeader = integral_constant::value>; template using has_BindGetCurrentIndex = integral_constant::value>; // enable_if Alias template template using enable_if_has_BindSendHeader = typename enable_if::value, int>::type; template using enable_if_hasNot_BindSendHeader = typename enable_if::value, int>::type; template using enable_if_has_BindGetSocketNumber = typename enable_if::value, int>::type; template using enable_if_hasNot_BindGetSocketNumber = typename enable_if::value, int>::type; template using enable_if_has_BindGetCurrentIndex = typename enable_if::value, int>::type; template using enable_if_hasNot_BindGetCurrentIndex = typename enable_if::value, int>::type; template using enable_if_has_GetHeader = typename enable_if::value, int>::type; template using enable_if_hasNot_GetHeader = typename enable_if::value, int>::type; } // namespace tools } // namespace FairMQ #endif