21#include <fmt/format.h>
23#include <wx/datetime.h>
25#include <wx/stdpaths.h>
38#include <api/common/envelope.pb.h>
44using kiapi::common::ApiRequest, kiapi::common::ApiResponse, kiapi::common::ApiStatusCode;
55 m_token(
KIID().AsStdString() ),
56 m_readyToReply( false )
58 if( !
Pgm().GetCommonSettings()->m_Api.enable_server )
60 wxLogTrace(
traceApi,
"Server: disabled by user preferences." );
81 socket.AssignDir( wxS(
"/tmp" ) );
83 socket.AssignDir( wxStandardPaths::Get().GetTempDir() );
85 socket.AppendDir( wxS(
"kicad" ) );
86 socket.SetFullName( wxS(
"api.sock" ) );
90 wxLogTrace(
traceApi, wxString::Format(
"Server: socket path %s could not be created",
100 wxFileName lockFilePath( socket.GetPath(), wxS(
"api.lock" ) );
102 int lockFile = open( lockFilePath.GetFullPath().c_str(), O_RDONLY | O_CREAT, 0600 );
104 if( lockFile >= 0 && flock( lockFile, LOCK_EX | LOCK_NB ) == 0 )
106 if( socket.Exists() )
108 wxLogTrace(
traceApi, wxString::Format(
"Server: cleaning up stale socket path %s",
109 socket.GetFullPath() ) );
110 wxRemoveFile( socket.GetFullPath() );
115 if( socket.Exists() )
117 socket.SetFullName( wxString::Format( wxS(
"api-%lu.sock" ), ::wxGetProcessId() ) );
119 if( socket.Exists() )
121 wxLogTrace(
traceApi, wxString::Format(
"Server: PID socket path %s already exists!",
122 socket.GetFullPath() ) );
127 m_server = std::make_unique<KINNG_REQUEST_SERVER>(
128 fmt::format(
"ipc://{}", socket.GetFullPath().ToStdString() ) );
137 log( fmt::format(
"--- KiCad API server started at {} ---\n",
SocketPath() ) );
151 wxLogTrace(
traceApi,
"Stopping server" );
167 wxCHECK( aHandler, );
188 ApiResponse notHandled;
189 notHandled.mutable_status()->set_status( ApiStatusCode::AS_NOT_READY );
190 notHandled.mutable_status()->set_error_message(
"KiCad is not ready to reply" );
191 m_server->Reply( notHandled.SerializeAsString() );
192 log(
"Got incoming request but was not yet ready to reply." );
196 wxCommandEvent* evt =
new wxCommandEvent( API_REQUEST_EVENT );
199 evt->SetClientData(
static_cast<void*
>( aRequest ) );
208 std::string& requestString = *
static_cast<std::string*
>( aEvent.GetClientData() );
211 if( !request.ParseFromString( requestString ) )
214 error.mutable_header()->set_kicad_token(
m_token );
215 error.mutable_status()->set_status( ApiStatusCode::AS_BAD_REQUEST );
216 error.mutable_status()->set_error_message(
"request could not be parsed" );
217 m_server->Reply( error.SerializeAsString() );
220 log(
"Response (ERROR): " + error.Utf8DebugString() );
224 log(
"Request: " + request.Utf8DebugString() );
226 if( !request.header().kicad_token().empty() &&
227 request.header().kicad_token().compare(
m_token ) != 0 )
230 error.mutable_header()->set_kicad_token(
m_token );
231 error.mutable_status()->set_status( ApiStatusCode::AS_TOKEN_MISMATCH );
232 error.mutable_status()->set_error_message(
233 "the provided kicad_token did not match this KiCad instance's token" );
234 m_server->Reply( error.SerializeAsString() );
237 log(
"Response (ERROR): " + error.Utf8DebugString() );
246 result = handler->Handle( request );
248 if( result.has_value() )
250 else if( result.error().status() != ApiStatusCode::AS_UNHANDLED )
256 if( result.has_value() )
258 result->mutable_header()->set_kicad_token(
m_token );
259 m_server->Reply( result->SerializeAsString() );
262 log(
"Response: " + result->Utf8DebugString() );
267 error.mutable_status()->CopyFrom( result.error() );
268 error.mutable_header()->set_kicad_token(
m_token );
270 if( result.error().status() == ApiStatusCode::AS_UNHANDLED )
272 std::string type =
"<unparseable Any>";
273 google::protobuf::Any::ParseAnyTypeUrl( request.message().type_url(), &type );
274 std::string msg = fmt::format(
"no handler available for request of type {}", type );
275 error.mutable_status()->set_error_message( msg );
278 m_server->Reply( error.SerializeAsString() );
281 log(
"Response (ERROR): " + error.Utf8DebugString() );
288 FILE* fp = wxFopen(
m_logFilePath.GetFullPath(), wxT(
"a" ) );
294 wxDateTime now = wxDateTime::Now();
296 fprintf( fp,
"%s",
TO_UTF8( out.Format( wxS(
"%s: %s" ),
297 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, bool aPathToFile=false)
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.