1 #include "otsdaq/NetworkUtilities/TCPClientBase.h"
2 #include "otsdaq/Macros/CoutMacros.h"
6 #include <netinet/in.h>
8 #include <boost/regex.hpp>
15 TCPClientBase::TCPClientBase(
const std::string& serverIP,
int serverPort)
16 : fServerIP(serverIP), fServerPort(serverPort), fConnected(false)
21 TCPClientBase::~TCPClientBase(
void)
30 bool TCPClientBase::connect(
int retry,
unsigned int sleepMilliSeconds)
32 __COUT__ << __PRETTY_FUNCTION__ <<
"Connecting Client socket to server name-"
33 << fServerIP <<
"-serverPort: " << fServerPort <<
" already connected? "
34 << fConnected << std::endl;
37 std::stringstream error;
38 error <<
"ERROR: This client is already connected. This must never happens. It "
39 "probably means that the connect method is called multiple times before "
40 "the TCPClient has been disconnected.";
41 throw std::runtime_error(error.str());
44 __COUT__ << __PRETTY_FUNCTION__ <<
"Connecting Client socket to server name-"
45 << fServerIP <<
"-serverPort: " << fServerPort << std::endl;
46 std::string serverName = fServerIP;
47 resolveServer(fServerIP);
48 __COUT__ <<
"Connecting Client socket to server ip -" << fServerIP
49 <<
"-serverPort: " << fServerPort << std::endl;
50 int status = invalidSocketId;
51 struct sockaddr_in serverSocketAddress;
52 serverSocketAddress.sin_family = AF_INET;
53 serverSocketAddress.sin_port = htons(fServerPort);
54 serverSocketAddress.sin_addr.s_addr = inet_addr(fServerIP.c_str());
56 int totalTries = retry;
57 while(!fConnected && (
unsigned int)retry-- > 0)
59 __COUT__ << __PRETTY_FUNCTION__ <<
"Trying to connect with socket ID "
60 << getSocketId() << std::endl;
62 status = ::connect(getSocketId(),
63 (
struct sockaddr*)&serverSocketAddress,
64 sizeof(serverSocketAddress));
65 __COUT__ << __PRETTY_FUNCTION__ <<
"Done Connect with status: " << status
69 if((
unsigned int)retry > 0)
71 __COUT__ << __PRETTY_FUNCTION__ <<
"WARNING: Can't connect to "
72 << serverName <<
". The server might still be down...Sleeping "
73 << sleepMilliSeconds <<
"ms and then retry "
74 << (
unsigned int)retry <<
" more times." << std::endl;
75 std::this_thread::sleep_for(std::chrono::milliseconds(sleepMilliSeconds));
80 std::stringstream error;
81 error <<
"ERROR: Can't connect to " << serverName
82 <<
". The server might still be down after "
83 << totalTries * sleepMilliSeconds / 1000. <<
" seconds and "
84 << totalTries <<
" connection attempts!";
86 throw std::runtime_error(error.str());
115 __COUT__ <<
"Succesfully connected to server " << fServerIP
116 <<
" port: " << fServerPort << std::endl;
123 bool TCPClientBase::disconnect(
void)
127 TCPSocket::sendClose();
136 void TCPClientBase::resolveServer(std::string& serverIP)
138 const std::string ipv4(
139 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
140 "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
141 "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
142 "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");
143 boost::regex ip_regex(ipv4.c_str());
146 if(boost::regex_match(serverIP, ip_regex))
148 else if(serverIP ==
"localhost" || serverIP ==
"localhost.localdomain")
150 serverIP =
"127.0.0.1";
154 struct hostent* resolvedHost = ::gethostbyname(serverIP.c_str());
155 if(resolvedHost == NULL)
157 throw std::runtime_error(serverIP +
" is unavailable and can't be resolved!");
160 in_addr* address = (in_addr*)resolvedHost->h_addr;
161 serverIP = inet_ntoa(*address);
162 __COUT__ <<
"IP: (" << serverIP <<
")\n";