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
44#include "pugixml.hpp"
45#include "pincode.h"
46#include "route.h"
47#include "track.h"
48
55enum class RestServerResult {
56 NoError = 0,
57 GenericError = 1,
58 ObjectRejected = 2,
59 DuplicateRejected = 3,
60 RouteInsertError = 4,
61 NewPinRequested = 5,
62 ObjectParseError = 6,
63 Void = 100
64};
65
67enum { ID_STG_CANCEL = 10000, ID_STG_OK, ID_STG_CHECK1, ID_STG_CHOICE_COMM };
68
70std::string RestResultText(RestServerResult result);
71
73struct RestIoEvtData;
74
77 const int status;
78 const bool check1_value;
79
82
84 AcceptObjectDlgResult(int s, bool b) : status(s), check1_value(b) {}
85};
86
89public:
91 std::function<wxDialog*(const std::string& msg, const std::string& text1)>
93
95 std::function<void(void)> update_route_mgr;
96
98 std::function<AcceptObjectDlgResult(const wxString& msg,
99 const wxString& check1msg)>
101 std::function<void()> top_level_refresh;
102
105};
106
108class RouteCtx {
109public:
110 std::function<Route*(wxString)> find_route_by_guid;
111 std::function<Track*(wxString)> find_track_by_guid;
112 std::function<RoutePoint*(wxString)> find_wpt_by_guid;
113 std::function<void(Route*)> delete_route;
114 std::function<void(Track*)> delete_track;
115 std::function<void(RoutePoint*)> delete_waypoint;
116
118 RouteCtx();
119};
120
215public:
217 virtual bool StartServer(const fs::path& certificate_location) = 0;
218
220 virtual void StopServer() = 0;
221
223 virtual std::string GetEndpoint() = 0;
224
226 obs::EventVar activate_route;
227
229 obs::EventVar reverse_route;
230};
231
233class RestServer : public AbstractRestServer, public wxEvtHandler {
234 friend class RestServerObjectApp;
235 friend class RestCheckWriteApp;
236 friend class RestServerPingApp;
237 friend class RestPluginMsgApp;
238
239public:
240 RestServer(RestServerDlgCtx ctx, RouteCtx route_ctx, bool& portable);
241
242 ~RestServer() override;
243
244 bool StartServer(const fs::path& certificate_location) override;
245
246 void StopServer() override;
247
248 std::string GetEndpoint() override { return m_endpoint; }
249
252
254 RestServerResult GetReturnStatus() { return return_status; }
255
257 void UpdateRouteMgr() const { m_dlg_ctx.update_route_mgr(); }
258
260 std::string m_cert_file;
261
263 std::string m_key_file;
264
266 std::string m_reply_body;
267
269 std::mutex ret_mutex;
270
272 std::condition_variable return_status_cv;
273
278 wxSemaphore m_exit_sem;
279
280 const std::string m_endpoint;
281
282private:
283 class IoThread {
284 public:
299 IoThread(RestServer& parent, std::string ip);
300 virtual ~IoThread() = default;
301 void Run();
302
303 bool IsRunning() { return run_flag > 0; }
304
306 void Stop();
307
309 bool WaitUntilStopped();
310
311 private:
313 std::atomic_int run_flag;
314 RestServer& m_parent;
315 std::string m_server_ip;
316 };
317
322 class Apikeys : public std::unordered_map<std::string, std::string> {
323 public:
324 static Apikeys Parse(const std::string& s);
325 std::string ToString() const;
326 };
327
328 bool LoadConfig();
329 bool SaveConfig();
330
331 void HandleServerMessage(ObservedEvt& event);
332
333 void HandleWaypoint(pugi::xml_node object, const RestIoEvtData& evt_data);
334 void HandleTrack(pugi::xml_node object, const RestIoEvtData& evt_data);
335 void HandleRoute(pugi::xml_node object, const RestIoEvtData& evt_data);
336
337 bool CheckApiKey(const RestIoEvtData& evt_data);
338
339 RestServerDlgCtx m_dlg_ctx;
340 RouteCtx m_route_ctx;
341
342 RestServerResult return_status;
343
344 std::string m_certificate_directory;
345 Apikeys m_key_map;
346 wxDialog* m_pin_dialog;
347
348 bool m_overwrite;
349 std::string m_upload_path;
350 std::ofstream m_ul_stream;
351 std::thread m_std_thread;
352 IoThread m_io_thread;
353 Pincode m_pincode;
354};
355
356#endif // guard
Opencpn REST API.
virtual void StopServer()=0
Stop server thread, blocks until completed.
obs::EventVar reverse_route
Notified with a string GUID when user wants to reverse a route.
virtual std::string GetEndpoint()=0
Return HTTPS url to local rest server.
obs::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.
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:88
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:95
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:92
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
Peer communications pincode abstraction.
RestServerResult
Return codes from HandleServerMessage and eventually in the http response.
Definition rest_server.h:55
std::string RestResultText(RestServerResult result)
RestServerResult string representation.
Route abstraction.
Returned status from RunAcceptObjectDlg.
Definition rest_server.h:76
AcceptObjectDlgResult(int s, bool b)
Create a struct with given values for status and check1_value.
Definition rest_server.h:84
const bool check1_value
As of GetCheck1Value()
Definition rest_server.h:78
const int status
return value from ShowModal()
Definition rest_server.h:77
AcceptObjectDlgResult()
default constructor, returns empty struct.
Definition rest_server.h:81
Recorded track abstraction.