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
62public:
65
67 static void ReleaseInstance();
68
71
74
76 std::function<bool(const std::string&)> open_file_cb;
77
79 virtual void SetGetRestApiEndpointCb(std::function<std::string()> cb) {
80 get_rest_api_endpoint_cb = cb;
81 }
82
83 std::function<std::string()> get_rest_api_endpoint_cb;
84
85protected:
87 : get_rest_api_endpoint_cb([]() { return "0.0.0.0/1024"; }) {}
88};
89
92public:
93 static std::unique_ptr<LocalClientApi> GetClient();
94
95 LocalClientApi() = default;
96 virtual ~LocalClientApi() = default;
97
98 virtual LocalApiResult HandleCmdline(const wxCmdLineParser& parser);
99 virtual LocalApiResult HandleCmdline(CmdlineAction action,
100 const std::string& arg);
101
102 virtual LocalApiResult SendRaise() = 0;
103 virtual LocalApiResult SendOpen(const char* path) = 0;
104 virtual LocalApiResult SendQuit() = 0;
105 virtual LocalApiResult GetRestEndpoint() = 0;
106
107protected:
108 CmdlineAction ParseArgs(const wxCmdLineParser& parser, std::string& arg);
109};
110
111#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:91
Base interface for local server command handling.
Definition local_api.h:61
std::function< bool(const std::string &)> open_file_cb
Callback invoked on open command with a file path argument.
Definition local_api.h:76
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:79
EventVar on_raise
Notified on the Raise command.
Definition local_api.h:70
EventVar on_quit
Notified on the Quit command.
Definition local_api.h:73
static LocalServerApi & GetInstance()