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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
25#ifndef _IPC_API_H__
26#define _IPC_API_H__
27
28#include <wx/cmdline.h>
29#include <wx/ipc.h>
30#include <wx/log.h>
31
32#include "model/local_api.h"
33
34std::string GetSocketPath();
35
39class IpcClientConnection : public wxConnection {
40 friend class IpcClient;
41
42public:
43private:
44 IpcClientConnection() : wxConnection() {}
45};
46
47class IpcClient : public wxClient, public LocalClientApi {
48public:
49 IpcClient(const std::string& path);
50
51 IpcClient() : IpcClient(GetSocketPath()) {}
52
53 LocalApiResult SendRaise();
54 LocalApiResult SendOpen(const char* path);
55 LocalApiResult SendQuit();
56 LocalApiResult GetRestEndpoint();
57 wxConnectionBase* OnMakeConnection() { return new IpcClientConnection; }
58
59private:
60 wxConnectionBase* connection;
61};
62
63class IpcServer; // forward
64
68class IpcConnection : public wxConnection {
69 friend class IpcServer;
70
71public:
72 static LocalServerApi& GetInstance();
73 static void ReleaseInstance();
74
76 void operator=(const IpcConnection&) = delete;
77
78 IpcServer& server;
79
81 bool OnExec(const wxString&, const wxString& data);
82
88 const void* OnRequest(const wxString& topic, const wxString& item,
89 size_t* size, wxIPCFormat format);
90
91protected:
92 IpcConnection(IpcServer& s) : server(s) {}
93
94private:
95 std::string buffer;
96 static IpcServer* s_instance;
97};
98
102class IpcServer : public wxServer, public LocalServerApi {
103public:
104 const bool is_connected;
105
106 IpcServer(const std::string& path) : wxServer(), is_connected(Create(path)) {}
107
108 IpcServer() : IpcServer(GetSocketPath()) {}
109
110 wxConnectionBase* OnAcceptConnection(const wxString& topic) {
111 return new IpcConnection(*this);
112 }
113
115 void Serve() {}
116};
117
122public:
123 static DummyIpcServer& GetInstance() {
124 static DummyIpcServer server;
125 return server;
126 }
127
128 DummyIpcServer() {}
129 DummyIpcServer(const std::string& path) {}
130
131 wxConnectionBase* OnAcceptConnection(const wxString& topic) {
132 assert(false && "OnAcceptConnection called in DummyIpcServer");
133 return nullptr; // not reachable, for the compiler
134 }
135
136 void Serve() {}
137};
138
140public:
141 DummyIpcClient(const std::string& path) {}
142
143 DummyIpcClient() {}
144
145 LocalApiResult SendRaise() {
146 return LocalApiResult(false, "raise command not implemented");
147 }
148
149 LocalApiResult SendOpen(const char* path) {
150 return LocalApiResult(false, "open command not implemented");
151 }
152
153 LocalApiResult SendQuit() {
154 return LocalApiResult(false, "quit command not implemented");
155 }
156
157 LocalApiResult GetRestEndpoint() {
158 return LocalApiResult(false, "get_rest_endpoint command not implemented");
159 }
160
161 wxConnectionBase* OnMakeConnection() {
162 assert(false && "OnMakeConnection called in DummyIpcServer");
163 return nullptr; // not reachable, for the compiler
164 }
165};
166
167#endif // _IPC_API_H__
Useless place holder for LocalServerApi.
Definition ipc_api.h:121
Implement LocalClientApi using a filesystem fifo/socket.
Definition ipc_api.h:39
Started by IpcServer on filesystem fifo/socket connects.
Definition ipc_api.h:68
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:102
void Serve()
void, we are serving as long as there is a ServerFactory.
Definition ipc_api.h:115
Base interface for local clients.
Definition local_api.h:91
Base interface for local server command handling.
Definition local_api.h:61
The local API has a server side handling commands and a client part issuing commands.