OpenCPN Partial API docs
Loading...
Searching...
No Matches
ipc_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
24#ifndef IPC_API_H_
25#define IPC_API_H_
26
27#include <wx/cmdline.h>
28#include <wx/ipc.h>
29#include <wx/log.h>
30
31#include "model/local_api.h"
32
33std::string GetSocketPath();
34
38class IpcClientConnection : public wxConnection {
39 friend class IpcClient;
40
41public:
42private:
43 IpcClientConnection() : wxConnection() {}
44};
45
46class IpcClient : public wxClient, public LocalClientApi {
47public:
48 IpcClient(const std::string& path);
49
50 IpcClient() : IpcClient(GetSocketPath()) {}
51
52 LocalApiResult SendRaise();
53 LocalApiResult SendOpen(const char* path);
54 LocalApiResult SendQuit();
55 LocalApiResult GetRestEndpoint();
56 wxConnectionBase* OnMakeConnection() { return new IpcClientConnection; }
57
58private:
59 wxConnectionBase* connection;
60};
61
62class IpcServer; // forward
63
67class IpcConnection : public wxConnection {
68 friend class IpcServer;
69
70public:
71 static LocalServerApi& GetInstance();
72 static void ReleaseInstance();
73
75 void operator=(const IpcConnection&) = delete;
76
77 IpcServer& server;
78
80 bool OnExec(const wxString&, const wxString& data);
81
87 const void* OnRequest(const wxString& topic, const wxString& item,
88 size_t* size, wxIPCFormat format);
89
90protected:
91 IpcConnection(IpcServer& s) : server(s) {}
92
93private:
94 std::string buffer;
95 static IpcServer* s_instance;
96};
97
101class IpcServer : public wxServer, public LocalServerApi {
102public:
103 const bool is_connected;
104
105 IpcServer(const std::string& path) : wxServer(), is_connected(Create(path)) {}
106
107 IpcServer() : IpcServer(GetSocketPath()) {}
108
109 wxConnectionBase* OnAcceptConnection(const wxString& topic) {
110 return new IpcConnection(*this);
111 }
112
114 void Serve() {}
115};
116
121public:
122 static DummyIpcServer& GetInstance() {
123 static DummyIpcServer server;
124 return server;
125 }
126
127 DummyIpcServer() {}
128 DummyIpcServer(const std::string& path) {}
129
130 wxConnectionBase* OnAcceptConnection(const wxString& topic) {
131 assert(false && "OnAcceptConnection called in DummyIpcServer");
132 return nullptr; // not reachable, for the compiler
133 }
134
135 void Serve() {}
136
137protected:
141 ~DummyIpcServer() = default;
142};
143
145public:
146 DummyIpcClient(const std::string& path) {}
147
148 DummyIpcClient() {}
149
150 LocalApiResult SendRaise() {
151 return LocalApiResult(false, "raise command not implemented");
152 }
153
154 LocalApiResult SendOpen(const char* path) {
155 return LocalApiResult(false, "open command not implemented");
156 }
157
158 LocalApiResult SendQuit() {
159 return LocalApiResult(false, "quit command not implemented");
160 }
161
162 LocalApiResult GetRestEndpoint() {
163 return LocalApiResult(false, "get_rest_endpoint command not implemented");
164 }
165
166 wxConnectionBase* OnMakeConnection() {
167 assert(false && "OnMakeConnection called in DummyIpcServer");
168 return nullptr; // not reachable, for the compiler
169 }
170};
171
172#endif // IPC_API_H_
Useless place holder for LocalServerApi.
Definition ipc_api.h:120
~DummyIpcServer()=default
Destroy the Dummy Ipc Server object.
Implement LocalClientApi using a filesystem fifo/socket.
Definition ipc_api.h:38
Started by IpcServer on filesystem fifo/socket connects.
Definition ipc_api.h:67
bool OnExec(const wxString &, const wxString &data)
Handle commands without reply: quit and raise.
Definition ipc_api.cpp:87
const void * OnRequest(const wxString &topic, const wxString &item, size_t *size, wxIPCFormat format)
Handle commands with a reply.
Definition ipc_api.cpp:99
Implement LocalServerApi using a filesystem fifo/socket.
Definition ipc_api.h:101
void Serve()
void, we are serving as long as there is a ServerFactory.
Definition ipc_api.h:114
Base interface for local clients.
Definition local_api.h:98
Base interface for local server command handling.
Definition local_api.h:63
The local API has a server side handling commands and a client part issuing commands.