OpenCPN Partial API docs
ipc_api.h
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 
20 #ifndef _IPC_API_H__
21 #define _IPC_API_H__
22 
23 #include <wx/cmdline.h>
24 #include <wx/ipc.h>
25 #include <wx/log.h>
26 
27 #include "model/local_api.h"
28 
34 std::string GetSocketPath();
35 
39 class IpcClientConnection : public wxConnection {
40 friend class IpcClient;
41 
42 public:
43 
44 
45 private:
46  IpcClientConnection() : wxConnection() {}
47 };
48 
49 
50 class IpcClient : public wxClient, public LocalClientApi {
51 public:
52 
53  IpcClient(const std::string& path);
54 
55  IpcClient() : IpcClient(GetSocketPath()) {}
56 
57  LocalApiResult SendRaise();
58  LocalApiResult SendOpen(const char* path);
59  LocalApiResult SendQuit();
60  LocalApiResult GetRestEndpoint();
61  wxConnectionBase* OnMakeConnection() { return new IpcClientConnection; }
62 
63 private:
64  wxConnectionBase* connection;
65 };
66 
67 
68 class IpcServer; // forward
69 
73 class IpcConnection: public wxConnection {
74 friend class IpcServer;
75 
76 public:
77  static LocalServerApi& GetInstance();
78  static void ReleaseInstance();
79 
80  IpcConnection(IpcConnection&) = delete;
81  void operator= (const IpcConnection&) = delete;
82 
83 
84  IpcServer& server;
85 
87  bool OnExec(const wxString&, const wxString& data);
88 
94  const void* OnRequest(const wxString& topic, const wxString& item,
95  size_t* size, wxIPCFormat format) ;
96 
97 protected:
98  IpcConnection(IpcServer& s) : server(s) {}
99 
100 private:
101  std::string buffer;
102  static IpcServer* s_instance;
103 };
104 
108 class IpcServer : public wxServer, public LocalServerApi {
109 public:
110  const bool is_connected;
111 
112  IpcServer(const std::string& path)
113  : wxServer(), is_connected(Create(path)) {}
114 
115  IpcServer() : IpcServer(GetSocketPath()) {}
116 
117  wxConnectionBase* OnAcceptConnection(const wxString& topic) {
118  return new IpcConnection(*this);
119  }
120 
122  void Serve() {}
123 };
124 
129 public:
130  static DummyIpcServer& GetInstance() {
131  static DummyIpcServer server;
132  return server;
133  }
134 
135  DummyIpcServer() {}
136  DummyIpcServer(const std::string& path) {}
137 
138  wxConnectionBase* OnAcceptConnection(const wxString& topic) {
139  assert(false && "OnAcceptConnection called in DummyIpcServer");
140  return nullptr; // not reachable, for the compiler
141  }
142 
143  void Serve() {}
144 };
145 
147 public:
148 
149  DummyIpcClient(const std::string& path) {}
150 
151  DummyIpcClient() {}
152 
153  LocalApiResult SendRaise() {
154  return LocalApiResult(false, "raise command not implemented");
155  }
156 
157  LocalApiResult SendOpen(const char* path) {
158  return LocalApiResult(false, "open command not implemented");
159  }
160 
161  LocalApiResult SendQuit() {
162  return LocalApiResult(false, "quit command not implemented");
163  }
164 
165  LocalApiResult GetRestEndpoint() {
166  return LocalApiResult(false, "get_rest_endpoint command not implemented");
167  }
168 
169  wxConnectionBase* OnMakeConnection() {
170  assert(false && "OnMakeConnection called in DummyIpcServer");
171  return nullptr; // not reachable, for the compiler
172  }
173 };
174 
175 
176 
177 #endif // _IPC_API_H__
Useless place holder for LocalServerApi.
Definition: ipc_api.h:128
Implement LocalClientApi using a filesystem fifo/socket.
Definition: ipc_api.h:39
Started by IpcServer on filesystem fifo/socket connects.
Definition: ipc_api.h:73
bool OnExec(const wxString &, const wxString &data)
Handle commands without reply: quit and raise.
Definition: ipc_api.cpp:91
const void * OnRequest(const wxString &topic, const wxString &item, size_t *size, wxIPCFormat format)
Handle commands with a reply.
Definition: ipc_api.cpp:103
Implement LocalServerApi using a filesystem fifo/socket.
Definition: ipc_api.h:108
void Serve()
void, we are serving as long as there is a ServerFactory.
Definition: ipc_api.h:122
Base interface for local clients.
Definition: local_api.h:94
Base interface for local server command handling.
Definition: local_api.h:62
static void ReleaseInstance()
Release Instance.
static LocalServerApi & GetInstance()
The local API has a server side handling commands and a client part issuing commands.