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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
39#ifndef LOCAL_SERVER_API__
40#define LOCAL_SERVER_API__
41
42#include <wx/cmdline.h>
43
44#include "observable_evtvar.h"
45
46using LocalApiResult = std::pair<bool, std::string>;
47
48enum class CmdlineAction { Raise, Quit, Open, GetRestEndpoint, Fail, Skip };
49
50class LocalApiException : public std::exception {
51public:
52 LocalApiException(const std::string why) : reason(why) {}
53
54 const char* str() { return reason.c_str(); }
55
56private:
57 std::string reason;
58};
59
66public:
69
71 static void ReleaseInstance();
72
75
78
80 std::function<bool(const std::string&)> open_file_cb;
81
83 virtual void SetGetRestApiEndpointCb(std::function<std::string()> cb) {
84 get_rest_api_endpoint_cb = cb;
85 }
86
87 std::function<std::string()> get_rest_api_endpoint_cb;
88
89protected:
91 : get_rest_api_endpoint_cb([]() { return "0.0.0.0/1024"; }) {}
92
96 ~LocalServerApi() = default;
97};
98
101public:
102 static std::unique_ptr<LocalClientApi> GetClient();
103
104 LocalClientApi() = default;
105 virtual ~LocalClientApi() = default;
106
107 virtual LocalApiResult HandleCmdline(const wxCmdLineParser& parser);
108 virtual LocalApiResult HandleCmdline(CmdlineAction action,
109 const std::string& arg);
110
111 virtual LocalApiResult SendRaise() = 0;
112 virtual LocalApiResult SendOpen(const char* path) = 0;
113 virtual LocalApiResult SendQuit() = 0;
114 virtual LocalApiResult GetRestEndpoint() = 0;
115
116protected:
117 CmdlineAction ParseArgs(const wxCmdLineParser& parser, std::string& arg);
118};
119
120#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:100
Base interface for local server command handling.
Definition local_api.h:65
std::function< bool(const std::string &)> open_file_cb
Callback invoked on open command with a file path argument.
Definition local_api.h:80
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:83
EventVar on_raise
Notified on the Raise command.
Definition local_api.h:74
EventVar on_quit
Notified on the Quit command.
Definition local_api.h:77
static LocalServerApi & GetInstance()
~LocalServerApi()=default
Destroy the Local Server Api object.
A common variable shared between producer and consumer which supports Listen() and Notify().