KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_server.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 (C) 2023 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <fmt/format.h>
22#include <wx/app.h>
23#include <wx/datetime.h>
24#include <wx/event.h>
25#include <wx/stdpaths.h>
26
27#include <advanced_config.h>
28#include <api/api_handler.h>
29#include <api/api_utils.h> // traceApi
30#include <api/api_server.h>
31#include <kiid.h>
32#include <kinng.h>
33#include <paths.h>
34#include <pgm_base.h>
36#include <string_utils.h>
37
38#include <api/common/envelope.pb.h>
39
40#ifdef __UNIX__
41#include <sys/file.h>
42#endif
43
44using kiapi::common::ApiRequest, kiapi::common::ApiResponse, kiapi::common::ApiStatusCode;
45
46
47wxString KICAD_API_SERVER::s_logFileName = "api.log";
48
49
50wxDEFINE_EVENT( API_REQUEST_EVENT, wxCommandEvent );
51
52
54 wxEvtHandler(),
55 m_token( KIID().AsStdString() ),
56 m_readyToReply( false )
57{
58 if( !aAutoStart )
59 return;
60
61 if( !Pgm().GetCommonSettings()->m_Api.enable_server )
62 {
63 wxLogTrace( traceApi, "Server: disabled by user preferences." );
64 return;
65 }
66
67 Start();
68}
69
70
75
76
78{
79 wxFileName socket;
80
81#ifdef __WXMAC__
82 socket.AssignDir( wxS( "/tmp" ) );
83#else
84 socket.AssignDir( wxStandardPaths::Get().GetTempDir() );
85#endif
86
87 socket.AppendDir( wxS( "kicad" ) );
88 socket.SetFullName( wxS( "api.sock" ) );
89
90 return socket;
91}
92
93
95{
96 return fmt::format( "ipc://{}", StandardSocketPath().GetFullPath().ToUTF8().data() );
97}
98
99
101{
102 if( Running() )
103 return;
104
105 wxFileName socket;
106
107 if( m_socketPathOverride.IsEmpty() )
108 {
109 socket = StandardSocketPath();
110 }
111 else
112 {
113 socket.Assign( m_socketPathOverride );
114
115 if( !socket.IsAbsolute() )
116 socket.MakeAbsolute();
117 }
118
119 if( !PATHS::EnsurePathExists( socket.GetPath() ) )
120 {
121 wxLogTrace( traceApi, wxString::Format( "Server: socket path %s could not be created",
122 socket.GetPath() ) );
123 return;
124 }
125
126#ifndef __WINDOWS__
127 // We use non-abstract sockets because macOS and some other non-Linux platforms don't support
128 // abstract sockets, which means there might be an old socket to unlink. In order to try to
129 // recover this, we lock a file (which will be unlocked on process exit) and if we get the lock,
130 // we know the old socket is orphaned and can be removed.
131 wxFileName lockFilePath( socket.GetPath(), wxS( "api.lock" ) );
132
133 int lockFile = open( lockFilePath.GetFullPath().c_str(), O_RDONLY | O_CREAT, 0600 );
134
135 if( lockFile >= 0 && flock( lockFile, LOCK_EX | LOCK_NB ) == 0 )
136 {
137 if( socket.Exists() )
138 {
139 wxLogTrace( traceApi, wxString::Format( "Server: cleaning up stale socket path %s",
140 socket.GetFullPath() ) );
141 wxRemoveFile( socket.GetFullPath() );
142 }
143 }
144#endif
145
146 if( socket.Exists() )
147 {
148 socket.SetFullName( wxString::Format( wxS( "api-%lu.sock" ), ::wxGetProcessId() ) );
149
150 if( socket.Exists() )
151 {
152 wxLogTrace( traceApi, wxString::Format( "Server: PID socket path %s already exists!",
153 socket.GetFullPath() ) );
154 return;
155 }
156 }
157
158 m_server = std::make_unique<KINNG_REQUEST_SERVER>(
159 fmt::format( "ipc://{}", socket.GetFullPath().ToStdString() ) );
160 m_server->SetCallback( [&]( std::string* aRequest ) { onApiRequest( aRequest ); } );
161
162 if( !m_server->Start() )
163 {
164 wxLogTrace( traceApi, "Server: failed to start KINNG listener thread" );
165 m_server.reset( nullptr );
166 return;
167 }
168
169 m_logFilePath.AssignDir( PATHS::GetLogsPath() );
170 m_logFilePath.SetName( s_logFileName );
171
172 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
173 {
175 log( fmt::format( "--- KiCad API server started at {} ---\n", SocketPath() ) );
176 }
177
178 wxLogTrace( traceApi, wxString::Format( "Server: listening at %s", SocketPath() ) );
179 Bind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this );
180}
181
182
184{
185 if( !Running() )
186 return;
187
188 wxLogTrace( traceApi, "Stopping server" );
189 Unbind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this );
190
191 m_server->Stop();
192 m_server.reset( nullptr );
193}
194
195
197{
198 return m_server && m_server->Running();
199}
200
201
203{
204 wxCHECK( aHandler, /* void */ );
205 m_handlers.insert( aHandler );
206}
207
208
210{
211 m_handlers.erase( aHandler );
212}
213
214
216{
217 return m_server ? m_server->SocketPath() : "";
218}
219
220
221void KICAD_API_SERVER::onApiRequest( std::string* aRequest )
222{
223 if( !m_readyToReply.load( std::memory_order_acquire ) )
224 {
225 ApiResponse notHandled;
226 notHandled.mutable_status()->set_status( ApiStatusCode::AS_NOT_READY );
227 notHandled.mutable_status()->set_error_message( "KiCad is not ready to reply" );
228 m_server->Reply( notHandled.SerializeAsString() );
229 log( "Got incoming request but was not yet ready to reply." );
230 return;
231 }
232
233 wxCommandEvent* evt = new wxCommandEvent( API_REQUEST_EVENT );
234
235 // We don't actually need write access to this string, but client data is non-const
236 evt->SetClientData( static_cast<void*>( aRequest ) );
237
238 // Takes ownership and frees the wxCommandEvent
239 QueueEvent( evt );
240}
241
242
243void KICAD_API_SERVER::handleApiEvent( wxCommandEvent& aEvent )
244{
245 std::string& requestString = *static_cast<std::string*>( aEvent.GetClientData() );
246 handleApiRequestString( requestString );
247}
248
249
250void KICAD_API_SERVER::handleApiRequestString( std::string& aRequestString )
251{
252 ApiRequest request;
253
254 if( !request.ParseFromString( aRequestString ) )
255 {
256 ApiResponse error;
257 error.mutable_header()->set_kicad_token( m_token );
258 error.mutable_status()->set_status( ApiStatusCode::AS_BAD_REQUEST );
259 error.mutable_status()->set_error_message( "request could not be parsed" );
260 m_server->Reply( error.SerializeAsString() );
261
262 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
263 log( "Response (ERROR): " + error.Utf8DebugString() );
264
265 return;
266 }
267
268 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
269 log( "Request: " + request.Utf8DebugString() );
270
271 if( !request.header().kicad_token().empty() &&
272 request.header().kicad_token().compare( m_token ) != 0 )
273 {
274 ApiResponse error;
275 error.mutable_header()->set_kicad_token( m_token );
276 error.mutable_status()->set_status( ApiStatusCode::AS_TOKEN_MISMATCH );
277 error.mutable_status()->set_error_message(
278 "the provided kicad_token did not match this KiCad instance's token" );
279 m_server->Reply( error.SerializeAsString() );
280
281 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
282 log( "Response (ERROR): " + error.Utf8DebugString() );
283
284 return;
285 }
286
288
289 for( API_HANDLER* handler : m_handlers )
290 {
291 result = handler->Handle( request );
292
293 if( result.has_value() )
294 break;
295 else if( result.error().status() != ApiStatusCode::AS_UNHANDLED )
296 break;
297 }
298
299 // Note: at the point we call Reply(), we no longer own requestString.
300
301 if( result.has_value() )
302 {
303 result->mutable_header()->set_kicad_token( m_token );
304 m_server->Reply( result->SerializeAsString() );
305
306 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
307 log( "Response: " + result->Utf8DebugString() );
308 }
309 else
310 {
311 ApiResponse error;
312 error.mutable_status()->CopyFrom( result.error() );
313 error.mutable_header()->set_kicad_token( m_token );
314
315 if( result.error().status() == ApiStatusCode::AS_UNHANDLED )
316 {
317 std::string type = "<unparseable Any>";
318 google::protobuf::Any::ParseAnyTypeUrl( request.message().type_url(), &type );
319 std::string msg = fmt::format( "no handler available for request of type {}", type );
320 error.mutable_status()->set_error_message( msg );
321 }
322
323 m_server->Reply( error.SerializeAsString() );
324
325 if( ADVANCED_CFG::GetCfg().m_EnableAPILogging )
326 log( "Response (ERROR): " + error.Utf8DebugString() );
327 }
328}
329
330
331void KICAD_API_SERVER::log( const std::string& aOutput )
332{
333 FILE* fp = wxFopen( m_logFilePath.GetFullPath(), wxT( "a" ) );
334
335 if( !fp )
336 return;
337
338 wxString out;
339 wxDateTime now = wxDateTime::Now();
340
341 fprintf( fp, "%s", TO_UTF8( out.Format( wxS( "%s: %s" ),
342 now.FormatISOCombined(), aOutput ) ) );
343 fclose( fp );
344}
tl::expected< ApiResponse, ApiResponseStatus > API_RESULT
Definition api_handler.h:42
wxDEFINE_EVENT(API_REQUEST_EVENT, wxCommandEvent)
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
std::string m_token
Definition api_server.h:118
bool Running() const
void handleApiEvent(wxCommandEvent &aEvent)
Event handler that receives the event on the main thread sent by onApiRequest.
wxFileName m_logFilePath
Definition api_server.h:126
static wxString s_logFileName
Definition api_server.h:124
void RegisterHandler(API_HANDLER *aHandler)
Adds a new request handler to the server.
void onApiRequest(std::string *aRequest)
Callback that executes on the server thread and generates an event that will be handled by the wxWidg...
wxString m_socketPathOverride
Definition api_server.h:122
std::set< API_HANDLER * > m_handlers
Definition api_server.h:116
std::atomic< bool > m_readyToReply
Definition api_server.h:120
std::string SocketPath() const
void handleApiRequestString(std::string &aRequestString)
static std::string StandardSocketUrl()
Return the default API socket URL (including the ipc:// scheme).
void log(const std::string &aOutput)
KICAD_API_SERVER(bool aAutoStart=true)
void DeregisterHandler(API_HANDLER *aHandler)
static wxFileName StandardSocketPath()
Return the default API socket path (without the ipc:// scheme).
std::unique_ptr< KINNG_REQUEST_SERVER > m_server
Definition api_server.h:114
Definition kiid.h:46
static wxString GetLogsPath()
Gets a path to use for user-visible log files.
Definition paths.cpp:497
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
Definition paths.cpp:508
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition api_utils.cpp:33
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
wxString result
Test unit parsing edge cases and error handling.