22 #include <wx/filename.h>
24 #include "model/ipc_api.h"
25 #include "model/base_platform.h"
26 #include "model/logger.h"
27 #include "model/ocpn_utils.h"
29 IpcServer* IpcConnection::s_instance =
nullptr;
32 std::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";
39 IpcClient::IpcClient(
const std::string& path) {
40 connection = MakeConnection(
"localhost", path,
"OpenCPN");
46 LocalApiResult IpcClient::SendQuit() {
47 if (connection->Execute(wxString(
"quit"))) {
48 return LocalApiResult(
true,
"");
50 return LocalApiResult(
false,
"Server error running quit command");
55 LocalApiResult IpcClient::SendRaise() {
56 if (connection->Execute(wxString(
"raise"))) {
57 return LocalApiResult(
true,
"");
59 return LocalApiResult(
false,
"Server error running raise command");
64 LocalApiResult IpcClient::SendOpen(
const char* path) {
65 const void* reply = connection->Request(wxString(
"open " ) + path);
66 if (reply)
return LocalApiResult(
true,
static_cast<const char*
>(reply));
67 return LocalApiResult(
false,
"");
71 LocalApiResult IpcClient::GetRestEndpoint() {
72 const void* reply = connection->Request(
"get_rest_endpoint");
74 return LocalApiResult(
true,
static_cast<const char*
>(reply));
76 return LocalApiResult(
false,
"Server error running get_rest_endpoint");
80 if (!s_instance) s_instance =
new IpcServer(GetSocketPath());
84 void IpcConnection::ReleaseInstance() {
95 }
else if (data ==
"raise") {
104 const wxString& item,
size_t* size,
105 wxIPCFormat format) {
106 if (format != wxIPC_TEXT)
return 0;
108 std::string line = item.ToStdString();
109 if (ocpn::startswith(line,
"get_rest_endpoint")) {
110 buffer = server.get_rest_api_endpoint_cb();
111 if (size) *size = buffer.size();
112 return buffer.c_str();
113 }
else if( ocpn::startswith(line,
"open")) {
114 auto words = ocpn::split(line.c_str(),
" ");
115 if (words.size() != 2) {
116 wxLogWarning(
"Illegal open cmd line: %s", line.c_str());
120 const char* reply = ok ?
"ok" :
"fail";
121 if (size) *size = strlen(reply);
124 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.