KiCad PCB EDA Suite
Loading...
Searching...
No Matches
cross_probe_client.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <api/api_client.h>
22#include <api/api_server.h>
23#include <api/api_utils.h>
24#include <api/common/envelope.pb.h>
25
26#include <pgm_base.h>
27#include <wx/log.h>
28
29#include <fmt/format.h>
30
31
33std::map<FRAME_T, std::string> CROSS_PROBE_CLIENT::s_peers;
34
35
36static bool sendRequest( const std::string& aUrl, const google::protobuf::Message& aRequest )
37{
38 KICAD_API_CLIENT client( 100 );
39
40 if( !client.Connect( wxString::FromUTF8( aUrl ) ) )
41 {
42 wxLogTrace( traceApi,
43 wxString::Format( wxS( "crossprobe: failed to connect to %s" ), wxString::FromUTF8( aUrl ) ) );
44 return false;
45 }
46
47 kiapi::common::ApiResponse response;
48
49 if( !client.Send( aRequest, response, kiapi::common::StandaloneCrossProbeClientName ) )
50 {
51 wxLogTrace( traceApi,
52 wxString::Format( wxS( "crossprobe: failed to send to %s" ), wxString::FromUTF8( aUrl ) ) );
53 return false;
54 }
55
56 return response.status().status() == kiapi::common::AS_OK;
57}
58
59
60bool CROSS_PROBE_CLIENT::SendToFrame( FRAME_T aTarget, const google::protobuf::Message& aRequest )
61{
62 std::optional<std::string> targetUrl;
63
64 {
65 std::lock_guard<std::mutex> lock( s_mutex );
66
67 if( s_peers.contains( aTarget ) )
68 targetUrl = s_peers.at( aTarget );
69 }
70
71 KICAD_API_SERVER& server = Pgm().GetApiServer();
72
73 if( !targetUrl )
75
76 if( server.Running() && *targetUrl == server.SocketPath() )
77 return false;
78
79 return sendRequest( *targetUrl, aRequest );
80}
81
82
84{
85 KICAD_API_SERVER& server = Pgm().GetApiServer();
86
87 if( !server.Running() )
88 {
89 wxLogTrace( traceApi, wxS( "crossprobe: API server is not running, cannot announce" ) );
90 return;
91 }
92
93 std::string primaryUrl = KICAD_API_SERVER::StandardSocketUrl();
94 std::string ourUrl = server.SocketPath();
95
96 if( primaryUrl == ourUrl )
97 return;
98
99 kiapi::common::commands::CrossProbeAnnounce announce;
100 announce.set_frame_type( static_cast<kiapi::common::types::FrameType>( aFrameType ) );
101 announce.set_socket_path( ourUrl );
102 announce.set_api_token( server.Token() );
103
104 KICAD_API_CLIENT client;
105
106 if( !client.Connect( wxString::FromUTF8( primaryUrl ) ) )
107 {
108 wxLogTrace( traceApi, wxString::Format( wxS( "crossprobe: failed to connect to primary at %s" ),
109 wxString::FromUTF8( primaryUrl ) ) );
110 return;
111 }
112
113 if( sendRequest( primaryUrl, announce ) )
114 {
115 wxLogTrace( traceApi, wxString::Format( wxS( "crossprobe: announced frame %d to primary at %s" ), aFrameType,
116 wxString::FromUTF8( primaryUrl ) ) );
117 }
118}
119
120
121void CROSS_PROBE_CLIENT::RegisterPeer( FRAME_T aFrameType, const std::string& aSocketPath )
122{
123 std::lock_guard<std::mutex> lock( s_mutex );
124 s_peers[aFrameType] = aSocketPath;
125}
126
127
129{
130 KICAD_API_SERVER& server = Pgm().GetApiServer();
131
132 if( !server.Running() )
133 return false;
134
136}
static bool IsOnStandardSocketPath()
static void RegisterPeer(FRAME_T aFrameType, const std::string &aSocketPath)
static bool SendToFrame(FRAME_T aTarget, const google::protobuf::Message &aRequest)
static std::mutex s_mutex
static std::map< FRAME_T, std::string > s_peers
static void AnnounceToPrimary(FRAME_T aFrameType)
bool Connect(const wxString &aSocketUrl)
bool Send(const google::protobuf::Message &aRequest, kiapi::common::ApiResponse &aResponse, const std::string &aClientName="kicad")
Send a protobuf request to the KiCad API server and wait for a response.
bool Running() const
const std::string & Token() const
Definition api_server.h:81
std::string SocketPath() const
static std::string StandardSocketUrl()
Return the default API socket URL (including the ipc:// scheme).
KICAD_API_SERVER & GetApiServer()
Definition pgm_base.h:141
static bool sendRequest(const std::string &aUrl, const google::protobuf::Message &aRequest)
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition api_utils.cpp:33
const KICOMMON_API std::string StandaloneCrossProbeClientName
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE