libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
libcurl.ipp
Go to the documentation of this file.
3#include <cstdlib>
4#include <iostream>
5#include <fstream>
6#include <filesystem>
7#include <unistd.h>
8
9namespace adc {
10
11using std::cout;
12
13typedef std::string string;
14typedef std::string_view string_view;
15
16#define ADC_LIBCURL_PLUGIN_PORT_DEFAULT "443"
17#define ADC_LIBCURL_PLUGIN_URL_DEFAULT "https://localhost"
18
19const std::map< string, string > plugin_libcurl_config_defaults =
22};
23const string plugin_libcurl_prefix("ADC_LIBCURL_PLUGIN_");
24
25/* this behaves as a singleton under the plugin dynamic loader */
26
27/*! \brief NOT yet implemented publisher plugin that will eventually use libcurl.
28 */
30 enum state {
31 ok,
32 err
33 };
34 enum mode {
35 /* the next mode needed for correct operation */
36 pi_config,
37 pi_init,
38 pi_pub_or_final
39 };
40
41private:
42 const string vers;
43 const std::vector<string> tags;
44 string port;
45 string url;
46 enum state state;
47 bool paused;
48 enum mode mode;
49
50 int config(string_view surl, string_view sport) {
51 if (mode != pi_config)
52 return 2;
53 port = sport;
54 url = surl;
55 mode = pi_init;
56 return 0;
57 }
58
59 // find field in m, then with prefix in env, then the default.
60 const string get(const std::map< string, string >& m,
61 string field, string_view env_prefix) {
62 // fields not defined in config_defaults raise an exception.
63 auto it = m.find(field);
64 if (it != m.end()) {
65 return it->second;
66 }
67 string en = string(env_prefix) += field;
68 char *ec = getenv(en.c_str());
69 if (!ec) {
71 } else {
72 return string(ec);
73 }
74 }
75
76 string get_temp_file_name() {
77 string tplt = fdir + "/json-msg-XXXXXX";
78 char * cstr = new char [tplt.length()+1];
79 std::strcpy (cstr, tplt.c_str());
80 int fd = mkstemp(cstr);
81 if (fd >= 0) {
82 string f(cstr);
83 delete[] cstr;
84 close(fd);
85 // current user now owns tempfile.
86 return f;
87 }
88 std::cout<< "mkstemp failed" <<std::endl;
89 return "";
90 }
91
92 // invoke https network POST with json
93 // wrap this in a thread to make it non-blocking eventually?
94 int libcurl_send(std::string_view json)
95 {
96 // fixme: implement POST with boost beast/asio or libcurl in libcurl_send; see curl --libcurl ; libcurl_plugin::libcurl_send
97 // probably prefer libcurl:
98 // https://curl.se/libcurl/c/postinmemory.html
99 // https://curl.se/libcurl/c/simplessl.html
100 return 1;
101 }
102
103public:
104 libcurl_plugin() : vers("0.0.0") , tags({"unimplemented"}), state(ok), paused(false), mode(pi_config) {
105 std::cout << "Constructing libcurl_plugin" << std::endl;
106 }
107
108 /*! \brief NOT IMPLEMENTED .libcurl_plugin::libcurl_send pending. */
109 int publish(std::shared_ptr< builder_api > b) {
110 if (paused)
111 return 0;
112 if (state != ok)
113 return 1;
114 if (mode != pi_pub_or_final)
115 return 2;
116 string json = b->serialize();
117 libcurl_send(json);
118 return 0;
119 }
120
121 int config(const std::map< std::string, std::string >& m) {
122 return config(m, plugin_libcurl_prefix);
123 }
124
125 int config(const std::map< std::string, std::string >& m, std::string_view env_prefix) {
126 string url = get(m, "URL", env_prefix);
127 string port = get(m, "PORT", env_prefix);
128 return config(url, port);
129 }
130
131 const std::map< const std::string, const std::string> & get_option_defaults() {
133 }
134
136 std::map <string, string >m;
137 // config if never config'd
138 if (!fdir.size())
139 config(m);
140 if (mode != pi_init) {
141 return 2;
142 }
143 if ( state == err ) {
144 std::cout << "libcurl plugin initialize found pre-existing error" << std::endl;
145 return 3;
146 }
147 std::error_code ec;
148 std::filesystem::create_directories(fdir, ec);
149 if (ec.value() != 0 && ec.value() != EEXIST ) {
150 state = err;
151 std::cout << "unable to create scratch directory for plugin 'libcurl'; "
152 << fdir << " : " << ec.message() << std::endl;
153 return ec.value();
154 } else {
155 std::cout << "created " << fdir <<std::endl;
156 mode = pi_pub_or_final;
157 }
158 return 0;
159 }
160
161 void finalize() {
162 if (mode == pi_pub_or_final) {
163 state = ok;
164 paused = false;
165 mode = pi_config;
166 } else {
167 std::cout << "libcurl plugin finalize on non-running plugin" << std::endl;
168 }
169 }
170
171 void pause() {
172 paused = true;
173 }
174
175 void resume() {
176 paused = false;
177 }
178
180 return "libcurl";
181 }
182
184 return vers;
185 }
186
188 std::cout << "Destructing libcurl_plugin" << std::endl;
189 }
190};
191
192} // adc
NOT yet implemented publisher plugin that will eventually use libcurl.
Definition libcurl.ipp:29
int config(const std::map< std::string, std::string > &m)
Configure the plugin with the options given.
Definition libcurl.ipp:121
const std::map< const std::string, const std::string > & get_option_defaults()
Look up the settable options and their defaults.
Definition libcurl.ipp:131
int publish(std::shared_ptr< builder_api > b)
NOT IMPLEMENTED .libcurl_plugin::libcurl_send pending.
Definition libcurl.ipp:109
void pause()
Pause publishing until a call to resume. Duplicate calls are allowed.
Definition libcurl.ipp:171
int initialize()
Ready the plugin to publish following the configuration options set or defaulted.
Definition libcurl.ipp:135
void finalize()
Stop publishing and release any resources held for managing publication.
Definition libcurl.ipp:161
void resume()
Resume publishing Duplicate calls are allowed.
Definition libcurl.ipp:175
string_view name() const
Definition libcurl.ipp:179
string_view version() const
Definition libcurl.ipp:183
int config(const std::map< std::string, std::string > &m, std::string_view env_prefix)
Configure the plugin with the options given and the corresponding environment variables.
Definition libcurl.ipp:125
Publisher plugin interface.
Definition publisher.hpp:44
#define ADC_LIBCURL_PLUGIN_PORT_DEFAULT
Definition libcurl.ipp:16
#define ADC_LIBCURL_PLUGIN_URL_DEFAULT
Definition libcurl.ipp:17
Definition adc.hpp:75
std::string_view string_view
Definition curl.ipp:14
const string plugin_libcurl_prefix("ADC_LIBCURL_PLUGIN_")
std::string string
Definition curl.ipp:13
const std::map< string, string > plugin_libcurl_config_defaults
Definition libcurl.ipp:19