OpenCPN Partial API docs
Loading...
Searching...
No Matches
local_api.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2023 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
37#ifndef LOCAL_SERVER_API__
38#define LOCAL_SERVER_API__
39
40#include <wx/cmdline.h>
41
42#include "observable_evtvar.h"
43
44using LocalApiResult = std::pair<bool, std::string>;
45
46enum class CmdlineAction { Raise, Quit, Open, GetRestEndpoint, Fail, Skip };
47
48class LocalApiException : public std::exception {
49public:
50 LocalApiException(const std::string why) : reason(why) {}
51
52 const char* str() { return reason.c_str(); }
53
54private:
55 std::string reason;
56};
57
64public:
67
69 static void ReleaseInstance();
70
73
76
78 std::function<bool(const std::string&)> open_file_cb;
79
81 virtual void SetGetRestApiEndpointCb(std::function<std::string()> cb) {
82 get_rest_api_endpoint_cb = cb;
83 }
84
85 std::function<std::string()> get_rest_api_endpoint_cb;
86
87protected:
89 : get_rest_api_endpoint_cb([]() { return "0.0.0.0/1024"; }) {}
90
94 ~LocalServerApi() = default;
95};
96
99public:
100 static std::unique_ptr<LocalClientApi> GetClient();
101
102 LocalClientApi() = default;
103 virtual ~LocalClientApi() = default;
104
105 virtual LocalApiResult HandleCmdline(const wxCmdLineParser& parser);
106 virtual LocalApiResult HandleCmdline(CmdlineAction action,
107 const std::string& arg);
108
109 virtual LocalApiResult SendRaise() = 0;
110 virtual LocalApiResult SendOpen(const char* path) = 0;
111 virtual LocalApiResult SendQuit() = 0;
112 virtual LocalApiResult GetRestEndpoint() = 0;
113
114protected:
115 CmdlineAction ParseArgs(const wxCmdLineParser& parser, std::string& arg);
116};
117
118#endif // LOCAL_SERVER_API__
Generic event handling between MVC Model and Controller based on a shared EventVar variable.
Base interface for local clients.
Definition local_api.h:98
Base interface for local server command handling.
Definition local_api.h:63
std::function< bool(const std::string &)> open_file_cb
Callback invoked on open command with a file path argument.
Definition local_api.h:78
static void ReleaseInstance()
Release Instance.
virtual void SetGetRestApiEndpointCb(std::function< std::string()> cb)
Set callback returning the rest server root endpoint.
Definition local_api.h:81
EventVar on_raise
Notified on the Raise command.
Definition local_api.h:72
EventVar on_quit
Notified on the Quit command.
Definition local_api.h:75
static LocalServerApi & GetInstance()
~LocalServerApi()=default
Destroy the Local Server Api object.
A common variable shared between producer and consumer which supports Listen() and Notify().