22#include <wx/filename.h>
25#include "model/base_platform.h"
27#include "model/ocpn_utils.h"
29IpcServer* IpcConnection::s_instance =
nullptr;
32std::string GetSocketPath() {
33 auto const static sep =
static_cast<char>(wxFileName::GetPathSeparator());
35 if (!wxFileName::DirExists(dirpath)) wxFileName::Mkdir(dirpath);
36 return dirpath.ToStdString() + sep +
"opencpn-ipc";
39IpcClient::IpcClient(
const std::string& path) {
40 connection = MakeConnection(
"localhost", path,
"OpenCPN");
45LocalApiResult IpcClient::SendQuit() {
46 if (connection->Execute(wxString(
"quit"))) {
47 return LocalApiResult(
true,
"");
49 return LocalApiResult(
false,
"Server error running quit command");
53LocalApiResult IpcClient::SendRaise() {
54 if (connection->Execute(wxString(
"raise"))) {
55 return LocalApiResult(
true,
"");
57 return LocalApiResult(
false,
"Server error running raise command");
61LocalApiResult IpcClient::SendOpen(
const char* path) {
62 const void* reply = connection->Request(wxString(
"open ") + path);
63 if (reply)
return LocalApiResult(
true,
static_cast<const char*
>(reply));
64 return LocalApiResult(
false,
"");
67LocalApiResult IpcClient::GetRestEndpoint() {
68 const void* reply = connection->Request(
"get_rest_endpoint");
70 return LocalApiResult(
true,
static_cast<const char*
>(reply));
72 return LocalApiResult(
false,
"Server error running get_rest_endpoint");
76 if (!s_instance) s_instance =
new IpcServer(GetSocketPath());
80void IpcConnection::ReleaseInstance() {
91 }
else if (data ==
"raise") {
100 const wxString& item,
size_t* size,
101 wxIPCFormat format) {
102 if (format != wxIPC_TEXT)
return 0;
104 std::string line = item.ToStdString();
105 if (ocpn::startswith(line,
"get_rest_endpoint")) {
106 buffer = server.get_rest_api_endpoint_cb();
107 if (size) *size = buffer.size();
108 return buffer.c_str();
109 }
else if (ocpn::startswith(line,
"open")) {
110 auto words = ocpn::split(line.c_str(),
" ");
111 if (words.size() != 2) {
112 wxLogWarning(
"Illegal open cmd line: %s", line.c_str());
116 const char* reply = ok ?
"ok" :
"fail";
117 if (size) *size = strlen(reply);
120 wxLogWarning(
"Illegal cmd line: %s", line.c_str());
const void Notify()
Notify all listeners, no data supplied.
bool OnExec(const wxString &, const wxString &data)
Handle commands without reply: quit and raise.
const void * OnRequest(const wxString &topic, const wxString &item, size_t *size, wxIPCFormat format)
Handle commands with a reply.
Implement LocalServerApi using a filesystem fifo/socket.
Base interface for local server command handling.
std::function< bool(const std::string &)> open_file_cb
Callback invoked on open command with a file path argument.
EventVar on_raise
Notified on the Raise command.
EventVar on_quit
Notified on the Quit command.
Server and client CLI api implementations.
Enhanced logging interface on top of wx/log.h.