OpenCPN Partial API docs
Loading...
Searching...
No Matches
rest_server.h
Go to the documentation of this file.
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, see <https://www.gnu.org/licenses/>. *
18 **************************************************************************/
19
26#ifndef RESTSERVER_H_
27#define RESTSERVER_H_
28
29#include <atomic>
30#include <condition_variable>
31#include <fstream>
32#include <functional>
33#include <string>
34#include <thread>
35#include <unordered_map>
36
37#include <wx/event.h>
38#include <wx/string.h>
39#include <wx/thread.h> // for wxSemaphore, std::semaphore is c++20
40
41#include "observable_evtvar.h"
42#include "std_filesystem.h"
43#include "pugixml.hpp"
44#include "pincode.h"
45#include "route.h"
46#include "track.h"
47
54enum class RestServerResult {
55 NoError = 0,
56 GenericError = 1,
57 ObjectRejected = 2,
58 DuplicateRejected = 3,
59 RouteInsertError = 4,
60 NewPinRequested = 5,
61 ObjectParseError = 6,
62 Void = 100
63};
64
66enum { ID_STG_CANCEL = 10000, ID_STG_OK, ID_STG_CHECK1, ID_STG_CHOICE_COMM };
67
69std::string RestResultText(RestServerResult result);
70
72struct RestIoEvtData;
73
76 const int status;
77 const bool check1_value;
78
81
83 AcceptObjectDlgResult(int s, bool b) : status(s), check1_value(b) {}
84};
85
88public:
90 std::function<wxDialog*(const std::string& msg, const std::string& text1)>
92
94 std::function<void(void)> update_route_mgr;
95
97 std::function<AcceptObjectDlgResult(const wxString& msg,
98 const wxString& check1msg)>
100 std::function<void()> top_level_refresh;
101
104};
105
107class RouteCtx {
108public:
109 std::function<Route*(wxString)> find_route_by_guid;
110 std::function<Track*(wxString)> find_track_by_guid;
111 std::function<RoutePoint*(wxString)> find_wpt_by_guid;
112 std::function<void(Route*)> delete_route;
113 std::function<void(Track*)> delete_track;
114 std::function<void(RoutePoint*)> delete_waypoint;
115
117 RouteCtx();
118};
119
214public:
216 virtual bool StartServer(const fs::path& certificate_location) = 0;
217
219 virtual void StopServer() = 0;
220
222 virtual std::string GetEndpoint() = 0;
223
226
229};
230
232class RestServer : public AbstractRestServer, public wxEvtHandler {
233 friend class RestServerObjectApp;
234 friend class RestCheckWriteApp;
235 friend class RestServerPingApp;
236 friend class RestPluginMsgApp;
237
238public:
239 RestServer(RestServerDlgCtx ctx, RouteCtx route_ctx, bool& portable);
240
241 ~RestServer() override;
242
243 bool StartServer(const fs::path& certificate_location) override;
244
245 void StopServer() override;
246
247 std::string GetEndpoint() override { return m_endpoint; }
248
251
253 RestServerResult GetReturnStatus() { return return_status; }
254
256 void UpdateRouteMgr() const { m_dlg_ctx.update_route_mgr(); }
257
259 std::string m_cert_file;
260
262 std::string m_key_file;
263
265 std::string m_reply_body;
266
268 std::mutex ret_mutex;
269
271 std::condition_variable return_status_cv;
272
277 wxSemaphore m_exit_sem;
278
279 const std::string m_endpoint;
280
281private:
282 class IoThread {
283 public:
298 IoThread(RestServer& parent, std::string ip);
299 virtual ~IoThread() = default;
300 void Run();
301
302 bool IsRunning() { return run_flag > 0; }
303
305 void Stop();
306
308 bool WaitUntilStopped();
309
310 private:
312 std::atomic_int run_flag;
313 RestServer& m_parent;
314 std::string m_server_ip;
315 };
316
321 class Apikeys : public std::unordered_map<std::string, std::string> {
322 public:
323 static Apikeys Parse(const std::string& s);
324 std::string ToString() const;
325 };
326
327 bool LoadConfig();
328 bool SaveConfig();
329
330 void HandleServerMessage(ObservedEvt& event);
331
332 void HandleWaypoint(pugi::xml_node object, const RestIoEvtData& evt_data);
333 void HandleTrack(pugi::xml_node object, const RestIoEvtData& evt_data);
334 void HandleRoute(pugi::xml_node object, const RestIoEvtData& evt_data);
335
336 bool CheckApiKey(const RestIoEvtData& evt_data);
337
338 RestServerDlgCtx m_dlg_ctx;
339 RouteCtx m_route_ctx;
340
341 RestServerResult return_status;
342
343 std::string m_certificate_directory;
344 Apikeys m_key_map;
345 wxDialog* m_pin_dialog;
346
347 bool m_overwrite;
348 std::string m_upload_path;
349 std::ofstream m_ul_stream;
350 std::thread m_std_thread;
351 IoThread m_io_thread;
352 Pincode m_pincode;
353};
354
355#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.
Custom event class for OpenCPN's notification system.
A random generated int value with accessors for string and hashcode.
Definition pincode.h:31
Callbacks for handling dialogs and RouteManager updates.
Definition rest_server.h:87
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:94
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:99
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:91
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.
Represents a waypoint or mark within the navigation system.
Definition route_point.h:71
Represents a navigational route in the navigation system.
Definition route.h:99
Represents a track, which is a series of connected track points.
Definition track.h:117
A common variable shared between producer and consumer which supports Listen() and Notify().
Peer communications pincode abstraction.
RestServerResult
Return codes from HandleServerMessage and eventually in the http response.
Definition rest_server.h:54
std::string RestResultText(RestServerResult result)
RestServerResult string representation.
Route abstraction.
Returned status from RunAcceptObjectDlg.
Definition rest_server.h:75
AcceptObjectDlgResult(int s, bool b)
Create a struct with given values for status and check1_value.
Definition rest_server.h:83
const bool check1_value
As of GetCheck1Value()
Definition rest_server.h:77
const int status
return value from ShowModal()
Definition rest_server.h:76
AcceptObjectDlgResult()
default constructor, returns empty struct.
Definition rest_server.h:80
Recorded track abstraction.