OpenCPN Partial API docs
Loading...
Searching...
No Matches
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// MacOS 1.13:
36#if (defined(OCPN_GHC_FILESYSTEM) || \
37 (defined(__clang_major__) && (__clang_major__ < 15)))
38#include <ghc/filesystem.hpp>
39namespace fs = ghc::filesystem;
40
41#else
42#include <filesystem>
43#include <utility>
44namespace 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
62enum 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
74enum { ID_STG_CANCEL = 10000, ID_STG_OK, ID_STG_CHECK1, ID_STG_CHOICE_COMM };
75
77std::string RestResultText(RestServerResult result);
78
80struct 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
96public:
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
115class RouteCtx {
116public:
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
222public:
224 virtual bool StartServer(const fs::path& certificate_location) = 0;
225
227 virtual void StopServer() = 0;
228
230 virtual std::string GetEndpoint() = 0;
231
234
237};
238
240class RestServer : public AbstractRestServer, public wxEvtHandler {
241 friend class RestServerObjectApp;
242 friend class RestCheckWriteApp;
243 friend class RestServerPingApp;
244 friend class RestPluginMsgApp;
245
246public:
247 RestServer(RestServerDlgCtx ctx, RouteCtx route_ctx, bool& portable);
248
249 ~RestServer() override;
250
251 bool StartServer(const fs::path& certificate_location) override;
252
253 void StopServer() override;
254
255 std::string GetEndpoint() override { return m_endpoint; }
256
258 void UpdateReturnStatus(RestServerResult r);
259
261 RestServerResult GetReturnStatus() { return return_status; }
262
264 void UpdateRouteMgr() const { m_dlg_ctx.update_route_mgr(); }
265
267 std::string m_cert_file;
268
270 std::string m_key_file;
271
273 std::string m_reply_body;
274
276 std::mutex ret_mutex;
277
279 std::condition_variable return_status_cv;
280
285 wxSemaphore m_exit_sem;
286
287 const std::string m_endpoint;
288
289private:
290 class IoThread {
291 public:
306 IoThread(RestServer& parent, std::string ip);
307 virtual ~IoThread() = default;
308 void Run();
309
310 bool IsRunning() { return run_flag > 0; }
311
313 void Stop();
314
316 bool WaitUntilStopped();
317
318 private:
320 std::atomic_int run_flag;
321 RestServer& m_parent;
322 std::string m_server_ip;
323 };
324
329 class Apikeys : public std::unordered_map<std::string, std::string> {
330 public:
331 static Apikeys Parse(const std::string& s);
332 std::string ToString() const;
333 };
334
335 bool LoadConfig();
336 bool SaveConfig();
337
338 void HandleServerMessage(ObservedEvt& event);
339
340 void HandleWaypoint(pugi::xml_node object, const RestIoEvtData& evt_data);
341 void HandleTrack(pugi::xml_node object, const RestIoEvtData& evt_data);
342 void HandleRoute(pugi::xml_node object, const RestIoEvtData& evt_data);
343
344 bool CheckApiKey(const RestIoEvtData& evt_data);
345
346 RestServerDlgCtx m_dlg_ctx;
347 RouteCtx m_route_ctx;
348
349 RestServerResult return_status;
350
351 std::string m_certificate_directory;
352 Apikeys m_key_map;
353 wxDialog* m_pin_dialog;
354
355 bool m_overwrite;
356 std::string m_upload_path;
357 std::ofstream m_ul_stream;
358 std::thread m_std_thread;
359 IoThread m_io_thread;
360 Pincode m_pincode;
361};
362
363#endif // guard
Opencpn REST API.
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.
EventVar activate_route
Notified with a string GUID when user wants to activate a route.
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.
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.
std::function< AcceptObjectDlgResult(const wxString &msg, const wxString &check1msg)> run_accept_object_dlg
Run the "Accept Object" dialog, returns value from ShowModal().
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.
std::string m_cert_file
Semi-static storage used by IoThread C code.
wxSemaphore m_exit_sem
IoThread interface: Binary exit synchronization, released when io thread exits.
std::string m_reply_body
IoThread interface: body of return message, if any.
RestServerResult GetReturnStatus()
IoThread interface.
std::condition_variable return_status_cv
IoThread interface: Guards return_status.
std::mutex ret_mutex
IoThread interface: Guards return_status.
friend class RestServerPingApp
Unit test hook.
friend class RestCheckWriteApp
Unit test hook.
friend class RestPluginMsgApp
Unit test hook.
void StopServer() override
Stop server thread, blocks until completed.
std::string GetEndpoint() override
Return HTTPS url to local rest server.
void UpdateRouteMgr() const
IoThread interface.
void UpdateReturnStatus(RestServerResult r)
IoThread interface.
std::string m_key_file
Semi-static storage used by IoThread C code.
bool StartServer(const fs::path &certificate_location) override
Start the server thread.
friend class RestServerObjectApp
Unit test hook.
Callbacks for handling routes and tracks.
RouteCtx()
Dummy stubs constructor.
Definition route.h:75
Represents a track, which is a series of connected track points.
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