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 std::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 std::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 std::cout << __PRETTY_FUNCTION__ <<
"Trying to connect" << std::endl;
61 status = ::connect(getSocketId(),
62 (
struct sockaddr*)&serverSocketAddress,
63 sizeof(serverSocketAddress));
64 std::cout << __PRETTY_FUNCTION__ <<
"Done Connect with status: " << status
68 if((
unsigned int)retry > 0)
70 std::cout << __PRETTY_FUNCTION__ <<
"WARNING: Can't connect to "
71 << serverName <<
". The server might still be down...Sleeping "
72 << sleepMilliSeconds <<
"ms and then retry "
73 << (
unsigned int)retry <<
" more times." << std::endl;
74 std::this_thread::sleep_for(std::chrono::milliseconds(sleepMilliSeconds));
79 std::stringstream error;
80 error <<
"ERROR: Can't connect to " << serverName
81 <<
". The server might still be down after "
82 << totalTries * sleepMilliSeconds / 1000. <<
" seconds and "
83 << totalTries <<
" connection attempts!";
85 throw std::runtime_error(error.str());
114 __COUT__ <<
"Succesfully connected to server " << fServerIP
115 <<
" port: " << fServerPort << std::endl;
122 bool TCPClientBase::disconnect(
void)
126 TCPSocket::sendClose();
135 void TCPClientBase::resolveServer(std::string& serverIP)
137 const std::string ipv4(
138 "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
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 boost::regex ip_regex(ipv4.c_str());
145 if(boost::regex_match(serverIP, ip_regex))
147 else if(serverIP ==
"localhost" || serverIP ==
"localhost.localdomain")
149 serverIP =
"127.0.0.1";
153 struct hostent* resolvedHost = ::gethostbyname(serverIP.c_str());
154 if(resolvedHost == NULL)
156 throw std::runtime_error(serverIP +
" is unavailable and can't be resolved!");
159 in_addr* address = (in_addr*)resolvedHost->h_addr;
160 serverIP = inet_ntoa(*address);
161 std::cout <<
"IP: (" << serverIP <<
")\n";