OpenCPN Partial API docs
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 
46 using LocalApiResult = std::pair<bool, std::string>;
47 
48 enum class CmdlineAction { Raise, Quit, Open, GetRestEndpoint, Fail, Skip };
49 
50 
51 class LocalApiException : public std::exception {
52 public:
53  LocalApiException(const std::string why) : reason(why) {}
54 
55  const char* str() { return reason.c_str(); }
56 private:
57  std::string reason;
58 };
59 
60 
63 public:
64 
66  static LocalServerApi& GetInstance();
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 
87 protected:
89  : get_rest_api_endpoint_cb([](){ return "0.0.0.0/1024"; }) {}
90 
91 };
92 
95 public:
96  static std::unique_ptr<LocalClientApi> GetClient();
97 
98  LocalClientApi() = default;
99  virtual ~LocalClientApi() = default;
100 
101 
102  virtual LocalApiResult HandleCmdline(const wxCmdLineParser& parser);
103  virtual LocalApiResult HandleCmdline(CmdlineAction action, const std::string& arg);
104 
105  virtual LocalApiResult SendRaise() = 0;
106  virtual LocalApiResult SendOpen(const char* path) = 0;
107  virtual LocalApiResult SendQuit() = 0;
108  virtual LocalApiResult GetRestEndpoint() = 0;
109 
110 protected:
111  CmdlineAction ParseArgs(const wxCmdLineParser& parser, std::string& arg);
112 };
113 
114 #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:94
Base interface for local server command handling.
Definition: local_api.h:62
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()