11#include < chimbuko/pserver/PSstatSender.hpp>
2- #include < curl/curl.h >
2+ #include < chimbuko/util/curlJsonSender.hpp >
33#include < chimbuko/verbose.hpp>
44#include < sstream>
55#include < fstream>
6+
67using namespace chimbuko ;
78
89
@@ -17,29 +18,6 @@ PSstatSender::~PSstatSender()
1718 for (auto &payload : m_payloads) delete payload;
1819}
1920
20-
21- // holder for curl fetch
22- struct curl_fetch_str {
23- std::string m_payload;
24- bool m_do_fetch;
25- curl_fetch_str (bool do_fetch) : m_do_fetch(do_fetch)
26- {
27-
28- }
29- };
30-
31- static size_t _curl_writefunc (char *ptr, size_t size, size_t nmemb, void * userp)
32- {
33- // std::cout << "curl_writefunc: " << std::string(ptr) << std::endl;
34- struct curl_fetch_str *p = (struct curl_fetch_str *) userp;
35-
36- if (ptr && p->m_do_fetch ) {
37- p->m_payload = std::string (ptr);
38- }
39-
40- return size * nmemb;
41- }
42-
4321static void send_stat (std::string url, std::string stat_save_dir,
4422 std::atomic_bool& bStop,
4523 const std::vector<PSstatSenderPayloadBase*> &payloads,
@@ -49,29 +27,7 @@ static void send_stat(std::string url, std::string stat_save_dir,
4927 bool write_curl = url.size () > 0 ;
5028 bool write_disk = stat_save_dir.size () > 0 ;
5129
52- CURL* curl = nullptr ;
53- struct curl_slist * headers = nullptr ;
54- CURLcode res;
55- long httpCode (0 );
56-
57- // Initialize
58- if (write_curl){
59- curl_global_init (CURL_GLOBAL_ALL);
60-
61- curl = curl_easy_init ();
62- if (curl == nullptr ) throw std::runtime_error (" Failed to initialize curl easy handler" );
63-
64- curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
65- // Dont bother trying IPv6, which would increase DNS resolution time
66- curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
67- // Don't wait forever, time out after 10 seconds
68- // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
69- // Follow HTTP redirects if necessary
70- curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
71- // header
72- headers = curl_slist_append (headers, " Content-Type: application/json" );
73- curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headers);
74- }
30+ curlJsonSender *curl_sender = write_curl ? new curlJsonSender (url) : nullptr ;
7531
7632 size_t iter = 0 ;
7733
@@ -100,22 +56,11 @@ static void send_stat(std::string url, std::string stat_save_dir,
10056 of << packet;
10157 }
10258 if (write_curl){ // Send data via curl
103- curl_fetch_str fetch (do_fetch); // request callback if
104- curl_easy_setopt (curl, CURLOPT_POSTFIELDS, packet.c_str ());
105- curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, packet.size ());
106- curl_easy_setopt (curl, CURLOPT_VERBOSE, 0L );
107- curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &_curl_writefunc);
108- curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *)&fetch);
109-
110- res = curl_easy_perform (curl);
111- if (res != CURLE_OK){
112- std::cerr << " curl_easy_perform() failed: " << curl_easy_strerror (res) << std::endl;
113- }
114- curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpCode);
115-
59+ std::string response;
60+ curl_sender->send (packet, do_fetch ? &response : nullptr );
11661 if (do_fetch){
11762 for (auto payload : payloads){
118- if (payload->do_fetch ()) payload->process_callback (packet, fetch. m_payload );
63+ if (payload->do_fetch ()) payload->process_callback (packet, response );
11964 }
12065 }
12166 }
@@ -124,12 +69,7 @@ static void send_stat(std::string url, std::string stat_save_dir,
12469 ++iter;
12570 }
12671
127-
128- if (curl){
129- if (headers) curl_slist_free_all (headers);
130- curl_easy_cleanup (curl);
131- curl_global_cleanup ();
132- }
72+ if (curl_sender) delete curl_sender;
13373 }catch (const std::exception &exc){
13474 std::cerr << " PSstatSender caught exception:" << exc.what () << std::endl;
13575 bad = true ;
0 commit comments