OpenCPN Partial API docs
rest_server.h
1 
2 /***************************************************************************
3  * Copyright (C) 2022 David Register *
4  * Copyright (C) 2022-2023 Alec Leamas *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20  **************************************************************************/
21 
22 #ifndef RESTSERVER_H_
23 #define RESTSERVER_H_
24 
25 #include <atomic>
26 #include <condition_variable>
27 #include <fstream>
28 #include <functional>
29 #include <string>
30 #include <thread>
31 #include <unordered_map>
32 
33 #include "observable_evtvar.h"
34 
35 
36 // MacOS 1.13:
37 #if (defined(OCPN_GHC_FILESYSTEM) || (defined(__clang_major__) && (__clang_major__ < 15)))
38 #include <ghc/filesystem.hpp>
39 namespace fs = ghc::filesystem;
40 
41 #else
42 #include <filesystem>
43 #include <utility>
44 namespace fs = std::filesystem;
45 #endif
46 
47 #include <wx/event.h>
48 #include <wx/string.h>
49 #include <wx/thread.h> // for wxSemaphore, std::semaphore is c++20
50 
51 #include "pugixml.hpp"
52 #include "pincode.h"
53 #include "route.h"
54 #include "track.h"
55 
62 enum class RestServerResult {
63  NoError = 0,
64  GenericError = 1,
65  ObjectRejected = 2,
66  DuplicateRejected = 3,
67  RouteInsertError = 4,
68  NewPinRequested = 5,
69  ObjectParseError = 6,
70  Void = 100
71 };
72 
74 enum { ID_STG_CANCEL = 10000, ID_STG_OK, ID_STG_CHECK1, ID_STG_CHOICE_COMM };
75 
77 std::string RestResultText(RestServerResult result);
78 
80 struct RestIoEvtData;
81 
84  const int status;
85  const bool check1_value;
86 
89 
91  AcceptObjectDlgResult(int s, bool b) : status(s), check1_value(b) {}
92 };
93 
96 public:
98  std::function<wxDialog*(const std::string& msg, const std::string& text1)>
100 
102  std::function<void(void)> update_route_mgr;
103 
105  std::function<AcceptObjectDlgResult(const wxString& msg,
106  const wxString& check1msg)>
108  std::function<void()> top_level_refresh;
109 
112 };
113 
115 class RouteCtx {
116 public:
117  std::function<Route*(wxString)> find_route_by_guid;
118  std::function<Track*(wxString)> find_track_by_guid;
119  std::function<RoutePoint*(wxString)> find_wpt_by_guid;
120  std::function<void(Route*)> delete_route;
121  std::function<void(Track*)> delete_track;
122  std::function<void(RoutePoint*)> delete_waypoint;
123 
125  RouteCtx();
126 };
127 
222 
223 public:
225  virtual bool StartServer(const fs::path& certificate_location) = 0;
226 
228  virtual void StopServer() = 0;
229 
231  virtual std::string GetEndpoint() = 0;
232 
235 
238 };
239 
241 class RestServer : public AbstractRestServer, public wxEvtHandler {
242  friend class RestServerObjectApp;
243  friend class RestCheckWriteApp;
244  friend class RestServerPingApp;
245  friend class RestPluginMsgApp;
246 
247 public:
248  RestServer(RestServerDlgCtx ctx, RouteCtx route_ctx, bool& portable);
249 
250  ~RestServer() override;
251 
252  bool StartServer(const fs::path& certificate_location) override;
253 
254  void StopServer() override;
255 
256  std::string GetEndpoint() override { return m_endpoint; }
257 
259  void UpdateReturnStatus(RestServerResult r);
260 
262  RestServerResult GetReturnStatus() { return return_status; }
263 
265  void UpdateRouteMgr() const { m_dlg_ctx.update_route_mgr(); }
266 
268  std::string m_cert_file;
269 
271  std::string m_key_file;
272 
274  std::string m_reply_body;
275 
277  std::mutex ret_mutex;
278 
280  std::condition_variable return_status_cv;
281 
286  wxSemaphore m_exit_sem;
287 
288  const std::string m_endpoint;
289 
290 private:
291  class IoThread {
292  public:
307  IoThread(RestServer& parent, std::string ip);
308  virtual ~IoThread() = default;
309  void Run();
310 
311  bool IsRunning() { return run_flag > 0; }
312 
314  void Stop();
315 
317  bool WaitUntilStopped();
318 
319  private:
321  std::atomic_int run_flag;
322  RestServer& m_parent;
323  std::string m_server_ip;
324  };
325 
330  class Apikeys : public std::unordered_map<std::string, std::string> {
331  public:
332  static Apikeys Parse(const std::string& s);
333  std::string ToString() const;
334  };
335 
336  bool LoadConfig();
337  bool SaveConfig();
338 
339  void HandleServerMessage(ObservedEvt& event);
340 
341  void HandleWaypoint(pugi::xml_node object, const RestIoEvtData& evt_data);
342  void HandleTrack(pugi::xml_node object, const RestIoEvtData& evt_data);
343  void HandleRoute(pugi::xml_node object, const RestIoEvtData& evt_data);
344 
345  bool CheckApiKey(const RestIoEvtData& evt_data);
346 
347  RestServerDlgCtx m_dlg_ctx;
348  RouteCtx m_route_ctx;
349 
350  RestServerResult return_status;
351 
352  std::string m_certificate_directory;
353  Apikeys m_key_map;
354  wxDialog* m_pin_dialog;
355 
356  bool m_overwrite;
357  std::string m_upload_path;
358  std::ofstream m_ul_stream;
359  std::thread m_std_thread;
360  IoThread m_io_thread;
361  Pincode m_pincode;
362 };
363 
364 #endif // guard
Opencpn REST API.
Definition: rest_server.h:221
virtual void StopServer()=0
Stop server thread, blocks until completed.
virtual std::string GetEndpoint()=0
Return HTTPS url to local rest server.
EventVar reverse_route
Notified with a string GUID when user wants to reverse a route.
Definition: rest_server.h:237
EventVar activate_route
Notified with a string GUID when user wants to activate a route.
Definition: rest_server.h:234
virtual bool StartServer(const fs::path &certificate_location)=0
Start the server thread.
Generic event handling between MVC Model and Controller based on a shared EventVar variable.
Adds a std::shared<void> element to wxCommandEvent.
Definition: ocpn_plugin.h:1652
A random generated int value with accessors for string and hashcode.
Definition: pincode.h:27
Callbacks for handling dialogs and RouteManager updates.
Definition: rest_server.h:95
RestServerDlgCtx()
All dummy stubs constructor.
std::function< void(void)> update_route_mgr
Update Route manager after updates to underlying nav_object_database.
Definition: rest_server.h:102
std::function< AcceptObjectDlgResult(const wxString &msg, const wxString &check1msg)> run_accept_object_dlg
Run the "Accept Object" dialog, returns value from ShowModal().
Definition: rest_server.h:107
std::function< wxDialog *(const std::string &msg, const std::string &text1)> run_pincode_dlg
Run the "Server wants a pincode" dialog.
Definition: rest_server.h:99
AbstractRestServer implementation and interface to underlying IO thread.
Definition: rest_server.h:241
std::string m_cert_file
Semi-static storage used by IoThread C code.
Definition: rest_server.h:268
wxSemaphore m_exit_sem
IoThread interface: Binary exit synchronization, released when io thread exits.
Definition: rest_server.h:286
std::string m_reply_body
IoThread interface: body of return message, if any.
Definition: rest_server.h:274
RestServerResult GetReturnStatus()
IoThread interface.
Definition: rest_server.h:262
std::condition_variable return_status_cv
IoThread interface: Guards return_status.
Definition: rest_server.h:280
std::mutex ret_mutex
IoThread interface: Guards return_status.
Definition: rest_server.h:277
friend class RestServerPingApp
Unit test hook.
Definition: rest_server.h:244
friend class RestCheckWriteApp
Unit test hook.
Definition: rest_server.h:243
friend class RestPluginMsgApp
Unit test hook.
Definition: rest_server.h:245
void StopServer() override
Stop server thread, blocks until completed.
std::string GetEndpoint() override
Return HTTPS url to local rest server.
Definition: rest_server.h:256
void UpdateRouteMgr() const
IoThread interface.
Definition: rest_server.h:265
void UpdateReturnStatus(RestServerResult r)
IoThread interface.
std::string m_key_file
Semi-static storage used by IoThread C code.
Definition: rest_server.h:271
bool StartServer(const fs::path &certificate_location) override
Start the server thread.
friend class RestServerObjectApp
Unit test hook.
Definition: rest_server.h:242
Callbacks for handling routes and tracks.
Definition: rest_server.h:115
RouteCtx()
Dummy stubs constructor.
Definition: route.h:75
Definition: track.h:78
std::string RestResultText(RestServerResult result)
RestServerResult string representation.
Returned status from RunAcceptObjectDlg.
Definition: rest_server.h:83
AcceptObjectDlgResult(int s, bool b)
Create a struct with given values for status and check1_value.
Definition: rest_server.h:91
const bool check1_value
As of GetCheck1Value()
Definition: rest_server.h:85
const int status
return value from ShowModal()
Definition: rest_server.h:84
AcceptObjectDlgResult()
default constructor, returns empty struct.
Definition: rest_server.h:88