OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_drv_internal.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2024 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// For compilers that support precompilation, includes "wx.h".
26#include <wx/wxprec.h>
27
28#ifndef WX_PRECOMP
29#include <wx/wx.h>
30#endif
31
32#include <wx/log.h>
33#include <wx/string.h>
34
35#include "observable.h"
36#include "wx/jsonreader.h"
37
38#include "config.h"
42#include "model/logger.h"
43#include "model/plugin_comm.h"
44
45CommDriverInternal::CommDriverInternal(DriverListener& listener)
46 : AbstractCommDriver(NavAddr::Bus::Plugin, "internal"),
47 m_listener(listener) {
48 this->attributes["commPort"] = "internal";
49 this->attributes["protocol"] = "internal";
50 this->attributes["ioDirection"] = "OUT";
51}
52
53bool CommDriverInternal::SendMessage(std::shared_ptr<const NavMsg> msg,
54 std::shared_ptr<const NavAddr> addr) {
55 auto plugin_msg = std::dynamic_pointer_cast<const PluginMsg>(msg);
56 if (!plugin_msg) {
57 WARNING_LOG << "SendMessage(): Illegal message type";
58 return false;
59 }
60 wxJSONValue root;
61 wxJSONReader json_reader;
62 int num_errors = json_reader.Parse(plugin_msg->message, &root);
63 if (num_errors > 0) {
64 WARNING_LOG << "SendMessage(): cannot parse json: "
65 << plugin_msg->message.substr(0, 30);
66 return false;
67 }
68 m_listener.Notify(plugin_msg);
69 return true;
70}
Common interface for all drivers.
Definition comm_driver.h:65
bool SendMessage(std::shared_ptr< const NavMsg > msg, std::shared_ptr< const NavAddr > addr) override
Send a message to all plugins and core on internal bus.
Interface for handling incoming messages.
Definition comm_driver.h:50
virtual void Notify(std::shared_ptr< const NavMsg > message)=0
Handle a received message.
Where messages are sent to or received from.
Plugin ABI encapsulation.
The JSON parser.
Definition jsonreader.h:50
int Parse(const wxString &doc, wxJSONValue *val)
Parse the JSON document.
The JSON value class implementation.
Definition jsonval.h:84
Internal send-only driver sending to plugins.
Driver registration container, a singleton.
Raw messages layer, supports sending and recieving navmsg messages.
Enhanced logging interface on top of wx/log.h.
General observable implementation with several specializations.
Tools to send data to plugins.