MakerLabs ACM
request_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright Paul Reimer, 2018
3  *
4  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 Unported License.
5  * To view a copy of this license, visit
6  * https://creativecommons.org/licenses/by-nc-sa/4.0/
7  * or send a letter to
8  * Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
9  */
10 
11 #pragma once
12 
13 #include "request_handler.h"
14 
15 #include "uuid.h"
16 
17 #ifdef REQUESTS_USE_CURL
18 #include "curl/curl.h"
19 #include "curl/multi.h"
20 #endif // REQUESTS_USE_CURL
21 
22 #ifdef REQUESTS_USE_SH2LIB
23 extern "C" {
24 #include "sh2lib.h"
25 }
26 #endif // REQUESTS_USE_SH2LIB
27 
28 #include <experimental/string_view>
29 
30 #include <memory>
31 #include <string>
32 #include <unordered_map>
33 
34 #include "mbedtls/ssl.h"
35 #include "mbedtls/x509_crt.h"
36 
37 namespace Requests {
38 
40 {
41 #ifdef REQUESTS_USE_CURL
42 private:
43  // C++ will initialize members in the declared order (more importantly)
44  // Destruct the members in the reverse order
45  // Create the curl_multi_handle first, then the handle map
46  std::unique_ptr<CURLM, CURLMcode(*)(CURLM*)> multi_handle;
47 #endif // REQUESTS_USE_CURL
48 
49 public:
50 #ifdef REQUESTS_USE_CURL
51  using HandleImpl = CURL;
52 #endif // REQUESTS_USE_CURL
53 #ifdef REQUESTS_USE_SH2LIB
54  using HandleImpl = sh2lib_handle;
55 #endif // REQUESTS_USE_SH2LIB
56  using HandleImplPtr = std::unique_ptr<HandleImpl, void(*)(HandleImpl*)>;
57 
59 
62 
63  auto fetch(
64  const RequestIntentFlatbufferRef& _request_intent_buf_ref
65  ) -> bool;
66 
67  auto send(
68  HandleImpl* handle,
69  RequestHandler& handler
70  ) -> bool;
71 
72  auto wait_any()
73  -> size_t;
74 
75  auto wait_all()
76  -> size_t;
77 
78  auto add_cacert_pem(const string_view cacert_pem)
79  -> bool;
80  auto add_cacert_der(const string_view cacert_der)
81  -> bool;
82 
83 #ifdef REQUESTS_USE_CURL
84  auto sslctx_callback(CURL* curl, mbedtls_ssl_config* ssl_ctx)
85  -> CURLcode;
86 #endif // REQUESTS_USE_CURL
87 
88 protected:
89  using RequestMap = std::unordered_map<HandleImplPtr, RequestHandler>;
91 
92  auto get_existing_request_handler(const UUID::UUID* request_intent_id)
93  -> RequestMap::const_iterator;
94 };
95 
96 } // namespace Requests
std::unordered_map< HandleImplPtr, RequestHandler > RequestMap
Definition: request_manager.h:89
flatbuffers::BufferRef< RequestIntent > RequestIntentFlatbufferRef
Definition: requests.h:28
Definition: request_manager.h:39
~RequestManager()
Definition: request_manager.cpp:188
Handle for working with sh2lib APIs.
Definition: sh2lib.h:35
RequestManager()
Definition: request_manager.cpp:147
RequestMap requests
Definition: request_manager.h:90
auto wait_all() -> size_t
Definition: request_manager.cpp:694
auto add_cacert_der(const string_view cacert_der) -> bool
Definition: request_manager.cpp:748
Definition: request_handler.h:41
auto wait_any() -> size_t
Definition: request_manager.cpp:543
auto send(HandleImpl *handle, RequestHandler &handler) -> bool
Definition: request_manager.cpp:250
static spi_device_handle_t handle
Definition: u8g2_esp32_hal.cpp:14
std::experimental::string_view string_view
Definition: actor_model.cpp:16
auto fetch(const RequestIntentFlatbufferRef &_request_intent_buf_ref) -> bool
Definition: request_manager.cpp:202
auto add_cacert_pem(const string_view cacert_pem) -> bool
Definition: request_manager.cpp:703
std::experimental::string_view string_view
Definition: request_manager.h:58
std::unique_ptr< HandleImpl, void(*)(HandleImpl *)> HandleImplPtr
Definition: request_manager.h:56
Definition: curl_library_info.cpp:15
auto get_existing_request_handler(const UUID::UUID *request_intent_id) -> RequestMap::const_iterator
Definition: request_manager.cpp:813
UUID::UUID UUID
Definition: firmware_update_actor.cpp:41