Allow turning the API server on/off at runtime

This commit is contained in:
Jon Evans 2024-04-03 22:03:58 -04:00
parent e0b913cc65
commit 9c113b25a4
3 changed files with 43 additions and 4 deletions

View File

@ -50,12 +50,29 @@ KICAD_API_SERVER::KICAD_API_SERVER() :
m_token( KIID().AsStdString() ),
m_readyToReply( false )
{
m_commonHandler = std::make_unique<API_HANDLER_COMMON>();
RegisterHandler( m_commonHandler.get() );
if( !Pgm().GetCommonSettings()->m_Api.enable_server )
{
wxLogTrace( traceApi, "Server: disabled by user preferences." );
return;
}
Start();
}
KICAD_API_SERVER::~KICAD_API_SERVER()
{
}
void KICAD_API_SERVER::Start()
{
if( Running() )
return;
wxFileName socket;
#ifdef __WXMAC__
socket.AssignDir( wxS( "/tmp" ) );
@ -88,9 +105,6 @@ KICAD_API_SERVER::KICAD_API_SERVER() :
fmt::format( "ipc://{}", socket.GetFullPath().ToStdString() ) );
m_server->SetCallback( [&]( std::string* aRequest ) { onApiRequest( aRequest ); } );
m_commonHandler = std::make_unique<API_HANDLER_COMMON>();
RegisterHandler( m_commonHandler.get() );
m_logFilePath.AssignDir( PATHS::GetLogsPath() );
m_logFilePath.SetName( s_logFileName );
@ -106,8 +120,16 @@ KICAD_API_SERVER::KICAD_API_SERVER() :
}
KICAD_API_SERVER::~KICAD_API_SERVER()
void KICAD_API_SERVER::Stop()
{
if( !Running() )
return;
wxLogTrace( traceApi, "Stopping server" );
Unbind( API_REQUEST_EVENT, &KICAD_API_SERVER::handleApiEvent, this );
m_server->Stop();
m_server.reset( nullptr );
}

View File

@ -73,6 +73,10 @@
#include <functional>
#include <kiface_ids.h>
#ifdef KICAD_IPC_API
#include <api/api_server.h>
#endif
// Minimum window size
static const wxSize minSizeLookup( FRAME_T aFrameType, wxWindow* aWindow )
@ -549,6 +553,15 @@ void EDA_BASE_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVars
COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
#ifdef KICAD_IPC_API
bool running = Pgm().GetApiServer().Running();
if( running && !settings->m_Api.enable_server )
Pgm().GetApiServer().Stop();
else if( !running && settings->m_Api.enable_server )
Pgm().GetApiServer().Start();
#endif
if( m_fileHistory )
{
int historySize = settings->m_System.file_history_size;

View File

@ -46,6 +46,10 @@ public:
~KICAD_API_SERVER();
void Start();
void Stop();
bool Running() const;
/**