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;
61 if( !
Pgm().GetCommonSettings()->m_Api.enable_server )
63 wxLogTrace( traceApi,
"Server: disabled by user preferences." );
87 socket.AssignDir( wxS(
"/tmp" ) );
89 socket.AssignDir( wxStandardPaths::Get().GetTempDir() );
91 socket.AppendDir( wxS(
"kicad" ) );
92 socket.SetFullName( wxS(
"api.sock" ) );
98 if( !socket.IsAbsolute() )
99 socket.MakeAbsolute();
104 wxLogTrace(
traceApi, wxString::Format(
"Server: socket path %s could not be created",
105 socket.GetPath() ) );
114 wxFileName lockFilePath( socket.GetPath(), wxS(
"api.lock" ) );
116 int lockFile = open( lockFilePath.GetFullPath().c_str(), O_RDONLY | O_CREAT, 0600 );
118 if( lockFile >= 0 && flock( lockFile, LOCK_EX | LOCK_NB ) == 0 )
120 if( socket.Exists() )
122 wxLogTrace(
traceApi, wxString::Format(
"Server: cleaning up stale socket path %s",
123 socket.GetFullPath() ) );
124 wxRemoveFile( socket.GetFullPath() );
129 if( socket.Exists() )
131 socket.SetFullName( wxString::Format( wxS(
"api-%lu.sock" ), ::wxGetProcessId() ) );
133 if( socket.Exists() )
135 wxLogTrace(
traceApi, wxString::Format(
"Server: PID socket path %s already exists!",
136 socket.GetFullPath() ) );
141 m_server = std::make_unique<KINNG_REQUEST_SERVER>(
142 fmt::format(
"ipc://{}", socket.GetFullPath().ToStdString() ) );
147 wxLogTrace(
traceApi,
"Server: failed to start KINNG listener thread" );
158 log( fmt::format(
"--- KiCad API server started at {} ---\n",
SocketPath() ) );
171 wxLogTrace(
traceApi,
"Stopping server" );
187 wxCHECK( aHandler, );
208 ApiResponse notHandled;
209 notHandled.mutable_status()->set_status( ApiStatusCode::AS_NOT_READY );
210 notHandled.mutable_status()->set_error_message(
"KiCad is not ready to reply" );
211 m_server->Reply( notHandled.SerializeAsString() );
212 log(
"Got incoming request but was not yet ready to reply." );
216 wxCommandEvent* evt =
new wxCommandEvent( API_REQUEST_EVENT );
219 evt->SetClientData(
static_cast<void*
>( aRequest ) );
228 std::string& requestString = *
static_cast<std::string*
>( aEvent.GetClientData() );
237 if( !request.ParseFromString( aRequestString ) )
240 error.mutable_header()->set_kicad_token(
m_token );
241 error.mutable_status()->set_status( ApiStatusCode::AS_BAD_REQUEST );
242 error.mutable_status()->set_error_message(
"request could not be parsed" );
243 m_server->Reply( error.SerializeAsString() );
246 log(
"Response (ERROR): " + error.Utf8DebugString() );
252 log(
"Request: " + request.Utf8DebugString() );
254 if( !request.header().kicad_token().empty() &&
255 request.header().kicad_token().compare(
m_token ) != 0 )
258 error.mutable_header()->set_kicad_token(
m_token );
259 error.mutable_status()->set_status( ApiStatusCode::AS_TOKEN_MISMATCH );
260 error.mutable_status()->set_error_message(
261 "the provided kicad_token did not match this KiCad instance's token" );
262 m_server->Reply( error.SerializeAsString() );
265 log(
"Response (ERROR): " + error.Utf8DebugString() );
274 result = handler->Handle( request );
278 else if(
result.error().status() != ApiStatusCode::AS_UNHANDLED )
290 log(
"Response: " +
result->Utf8DebugString() );
295 error.mutable_status()->CopyFrom(
result.error() );
296 error.mutable_header()->set_kicad_token(
m_token );
298 if(
result.error().status() == ApiStatusCode::AS_UNHANDLED )
300 std::string type =
"<unparseable Any>";
301 google::protobuf::Any::ParseAnyTypeUrl( request.message().type_url(), &type );
302 std::string msg = fmt::format(
"no handler available for request of type {}", type );
303 error.mutable_status()->set_error_message( msg );
306 m_server->Reply( error.SerializeAsString() );
309 log(
"Response (ERROR): " + error.Utf8DebugString() );
316 FILE* fp = wxFopen(
m_logFilePath.GetFullPath(), wxT(
"a" ) );
322 wxDateTime now = wxDateTime::Now();
324 fprintf( fp,
"%s",
TO_UTF8( out.Format( wxS(
"%s: %s" ),
325 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...
wxString m_socketPathOverride
std::set< API_HANDLER * > m_handlers
std::string SocketPath() const
void handleApiRequestString(std::string &aRequestString)
void log(const std::string &aOutput)
KICAD_API_SERVER(bool aAutoStart=true)
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.
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.
wxString result
Test unit parsing edge cases and error handling.