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