Skip to content

Commit 8d12774

Browse files
committed
remove precision, add hostname conversion
1 parent 800e091 commit 8d12774

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

influxdb.hpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sstream>
1414
#include <cstring>
1515
#include <cstdio>
16+
#include <cstdlib>
1617

1718
#ifdef _WIN32
1819
#define NOMINMAX
@@ -30,6 +31,7 @@
3031
#include <sys/socket.h>
3132
#include <sys/uio.h>
3233
#include <netinet/in.h>
34+
#include <netdb.h>
3335
#include <arpa/inet.h>
3436
#define closesocket close
3537
#endif
@@ -41,9 +43,26 @@ namespace influxdb_cpp {
4143
std::string db_;
4244
std::string usr_;
4345
std::string pwd_;
44-
std::string precision_;
45-
server_info(const std::string& host, int port, const std::string& db = "", const std::string& usr = "", const std::string& pwd = "", const std::string& precision="ms")
46-
: host_(host), port_(port), db_(db), usr_(usr), pwd_(pwd), precision_(precision) {}
46+
server_info(const std::string& host, int port, const std::string& db = "", const std::string& usr = "", const std::string& pwd = "") {
47+
port_ = port;
48+
db_ = db;
49+
usr_ = usr;
50+
pwd_ = pwd;
51+
52+
//convert hostname to ip-address
53+
hostent * record = gethostbyname(host.c_str());
54+
if(record == NULL)
55+
{
56+
printf("Cannot resolve IP address from hostname: %s is unavailable. Try to ping the host.\n", host.c_str());
57+
std::exit(-1);
58+
}
59+
in_addr * address = (in_addr * )record->h_addr;
60+
std::string ip_address = inet_ntoa(* address);
61+
62+
printf("Resolved IP address from hostname: %s.\n", ip_address.c_str());
63+
64+
host_ = ip_address;
65+
}
4766
};
4867
namespace detail {
4968
struct meas_caller;
@@ -105,7 +124,6 @@ namespace influxdb_cpp {
105124
lines_ << delim;
106125
_escape(k, ",= ");
107126
lines_.precision(prec);
108-
lines_.setf(std::ios::fixed);
109127
lines_ << '=' << v;
110128
return (detail::field_caller&)*this;
111129
}
@@ -206,6 +224,7 @@ namespace influxdb_cpp {
206224

207225
addr.sin_family = AF_INET;
208226
addr.sin_port = htons(si.port_);
227+
209228
if((addr.sin_addr.s_addr = inet_addr(si.host_.c_str())) == INADDR_NONE) return -1;
210229

211230
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -2;
@@ -219,8 +238,8 @@ namespace influxdb_cpp {
219238

220239
for(;;) {
221240
iv[0].iov_len = snprintf(&header[0], len,
222-
"%s /%s?db=%s&u=%s&p=%s&epoch=%s%s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n",
223-
method, uri, si.db_.c_str(), si.usr_.c_str(), si.pwd_.c_str(), si.precision_.c_str(),
241+
"%s /%s?db=%s&u=%s&p=%s%s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n",
242+
method, uri, si.db_.c_str(), si.usr_.c_str(), si.pwd_.c_str(),
224243
querystring.c_str(), si.host_.c_str(), (int)body.length());
225244
if((int)iv[0].iov_len >= len)
226245
header.resize(len *= 2);

0 commit comments

Comments
 (0)