21#include <fmt/format.h>
23#include <wx/datetime.h>
25#include <wx/stdpaths.h>
38#include <api/common/envelope.pb.h>
40using kiapi::common::ApiRequest, kiapi::common::ApiResponse, kiapi::common::ApiStatusCode;
51 m_token(
KIID().AsStdString() ),
52 m_readyToReply( false )
54 if( !
Pgm().GetCommonSettings()->m_Api.enable_server )
56 wxLogTrace(
traceApi,
"Server: disabled by user preferences." );
76 socket.AssignDir( wxS(
"/tmp" ) );
78 socket.AssignDir( wxStandardPaths::Get().GetTempDir() );
80 socket.AppendDir( wxS(
"kicad" ) );
81 socket.SetFullName( wxS(
"api.sock" ) );
85 wxLogTrace(
traceApi, wxString::Format(
"Server: socket path %s could not be created",
90 if( socket.FileExists() )
92 socket.SetFullName( wxString::Format( wxS(
"api-%ul.sock" ), ::wxGetProcessId() ) );
94 if( socket.FileExists() )
96 wxLogTrace(
traceApi, wxString::Format(
"Server: PID socket path %s already exists!",
97 socket.GetFullPath() ) );
102 m_server = std::make_unique<KINNG_REQUEST_SERVER>(
103 fmt::format(
"ipc://{}", socket.GetFullPath().ToStdString() ) );
112 log( fmt::format(
"--- KiCad API server started at {} ---\n",
SocketPath() ) );
126 wxLogTrace(
traceApi,
"Stopping server" );
142 wxCHECK( aHandler, );
163 ApiResponse notHandled;
164 notHandled.mutable_status()->set_status( ApiStatusCode::AS_NOT_READY );
165 notHandled.mutable_status()->set_error_message(
"KiCad is not ready to reply" );
166 m_server->Reply( notHandled.SerializeAsString() );
167 log(
"Got incoming request but was not yet ready to reply." );
171 wxCommandEvent* evt =
new wxCommandEvent( API_REQUEST_EVENT );
174 evt->SetClientData(
static_cast<void*
>( aRequest ) );
183 std::string& requestString = *
static_cast<std::string*
>( aEvent.GetClientData() );
186 if( !request.ParseFromString( requestString ) )
189 error.mutable_header()->set_kicad_token(
m_token );
190 error.mutable_status()->set_status( ApiStatusCode::AS_BAD_REQUEST );
191 error.mutable_status()->set_error_message(
"request could not be parsed" );
192 m_server->Reply( error.SerializeAsString() );
195 log(
"Response (ERROR): " + error.Utf8DebugString() );
199 log(
"Request: " + request.Utf8DebugString() );
201 if( !request.header().kicad_token().empty() &&
202 request.header().kicad_token().compare(
m_token ) != 0 )
205 error.mutable_header()->set_kicad_token(
m_token );
206 error.mutable_status()->set_status( ApiStatusCode::AS_TOKEN_MISMATCH );
207 error.mutable_status()->set_error_message(
208 "the provided kicad_token did not match this KiCad instance's token" );
209 m_server->Reply( error.SerializeAsString() );
212 log(
"Response (ERROR): " + error.Utf8DebugString() );
219 result = handler->Handle( request );
221 if( result.has_value() )
223 else if( result.error().status() != ApiStatusCode::AS_UNHANDLED )
229 if( result.has_value() )
231 result->mutable_header()->set_kicad_token(
m_token );
232 m_server->Reply( result->SerializeAsString() );
235 log(
"Response: " + result->Utf8DebugString() );
240 error.mutable_status()->CopyFrom( result.error() );
241 error.mutable_header()->set_kicad_token(
m_token );
243 if( result.error().status() == ApiStatusCode::AS_UNHANDLED )
245 std::string type =
"<unparseable Any>";
246 google::protobuf::Any::ParseAnyTypeUrl( request.message().type_url(), &type );
247 std::string msg = fmt::format(
"no handler available for request of type {}", type );
248 error.mutable_status()->set_error_message( msg );
251 m_server->Reply( error.SerializeAsString() );
254 log(
"Response (ERROR): " + error.Utf8DebugString() );
261 FILE* fp = wxFopen(
m_logFilePath.GetFullPath(), wxT(
"a" ) );
267 wxDateTime now = wxDateTime::Now();
269 fprintf( fp,
"%s",
TO_UTF8( out.Format( wxS(
"%s: %s" ),
270 now.FormatISOCombined(), aOutput ) ) );
tl::expected< ApiResponse, ApiResponseStatus > API_RESULT
wxDEFINE_EVENT(API_REQUEST_EVENT, wxCommandEvent)
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
void handleApiEvent(wxCommandEvent &aEvent)
Event handler that receives the event on the main thread sent by onApiRequest.
static wxString s_logFileName
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...
std::set< API_HANDLER * > m_handlers
std::string SocketPath() const
void log(const std::string &aOutput)
void DeregisterHandler(API_HANDLER *aHandler)
std::unique_ptr< KINNG_REQUEST_SERVER > m_server
static wxString GetLogsPath()
Gets a path to use for user-visible log files.
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
bool m_EnableAPILogging
Log IPC API requests and responses.
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
PGM_BASE & Pgm()
The global Program "get" accessor.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.