KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_server.h
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) 2024 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#ifndef KICAD_API_SERVER_H
22#define KICAD_API_SERVER_H
23
24#include <atomic>
25#include <functional>
26#include <memory>
27#include <set>
28#include <string>
29
30#include <wx/event.h>
31#include <wx/filename.h>
32
33#include <kicommon.h>
34#include <api/common/types/base_types.pb.h>
35
36class API_HANDLER;
38class wxEvtHandler;
39
41class KICOMMON_API KICAD_API_REQUEST_EVENT : public wxCommandEvent
42{
43public:
44 KICAD_API_REQUEST_EVENT( wxEventType aEventType = wxEVT_NULL ) :
45 wxCommandEvent( aEventType )
46 {}
47
48 wxEvent* Clone() const override { return new KICAD_API_REQUEST_EVENT( *this ); }
49
50 wxEventCategory GetEventCategory() const override { return wxEVT_CATEGORY_THREAD; }
51};
52
54
55class KICOMMON_API KICAD_API_SERVER : public wxEvtHandler
56{
57public:
58 KICAD_API_SERVER( bool aAutoStart = true );
59
61
62 void Start();
63
64 void Stop();
65
66 bool Running() const;
67
78 void RegisterHandler( API_HANDLER* aHandler );
79
80 void DeregisterHandler( API_HANDLER* aHandler );
81
82 void SetReadyToReply( bool aReady = true )
83 {
84 m_readyToReply.store( aReady, std::memory_order_release );
85 }
86
87 void SetSocketPath( const wxString& aSocketPath )
88 {
89 m_socketPathOverride = aSocketPath;
90 }
91
92 std::string SocketPath() const;
93
94 const std::string& Token() const { return m_token; }
95
99 static wxFileName StandardSocketPath();
100
104 static std::string StandardSocketUrl();
105
111 void NotifyNetSettingsChanged();
112
117 void NotifyDocumentRenamed( kiapi::common::types::DocumentType aType, const wxString& aOldPath,
118 const wxString& aNewPath );
119
123 void SetRenameListener( std::function<void( kiapi::common::types::DocumentType,
124 const wxString&, const wxString& )> aListener )
125 {
126 m_renameListener = std::move( aListener );
127 }
128
129private:
130
138 void onApiRequest( std::string* aRequest );
139
144 void handleApiEvent( wxCommandEvent& aEvent );
145
146 void handleApiRequestString( std::string& aRequestString );
147
148 void log( const std::string& aOutput );
149
150 std::unique_ptr<KINNG_REQUEST_SERVER> m_server;
151
152 std::set<API_HANDLER*> m_handlers;
153
154 std::string m_token;
155
156 std::atomic<bool> m_readyToReply;
157
158 std::function<void( kiapi::common::types::DocumentType, const wxString&, const wxString& )> m_renameListener;
159
161
162 static wxString s_logFileName;
163
164 wxFileName m_logFilePath;
165};
166
167#endif //KICAD_API_SERVER_H
wxDECLARE_EVENT(API_REQUEST_EVENT, KICAD_API_REQUEST_EVENT)
Event carrying an incoming API request from the listener thread to the main thread.
Definition api_server.h:42
KICAD_API_REQUEST_EVENT(wxEventType aEventType=wxEVT_NULL)
Definition api_server.h:44
wxEvent * Clone() const override
Definition api_server.h:48
wxEventCategory GetEventCategory() const override
Definition api_server.h:50
std::string m_token
Definition api_server.h:154
bool Running() const
wxFileName m_logFilePath
Definition api_server.h:164
std::function< void(kiapi::common::types::DocumentType, const wxString &, const wxString &)> m_renameListener
Definition api_server.h:158
static wxString s_logFileName
Definition api_server.h:162
void RegisterHandler(API_HANDLER *aHandler)
Adds a new request handler to the server.
wxString m_socketPathOverride
Definition api_server.h:160
std::set< API_HANDLER * > m_handlers
Definition api_server.h:152
std::atomic< bool > m_readyToReply
Definition api_server.h:156
const std::string & Token() const
Definition api_server.h:94
void SetReadyToReply(bool aReady=true)
Definition api_server.h:82
void SetRenameListener(std::function< void(kiapi::common::types::DocumentType, const wxString &, const wxString &)> aListener)
Installs a callback to be invoked by NotifyDocumentRenamed.
Definition api_server.h:123
void SetSocketPath(const wxString &aSocketPath)
Definition api_server.h:87
KICAD_API_SERVER(bool aAutoStart=true)
void DeregisterHandler(API_HANDLER *aHandler)
std::unique_ptr< KINNG_REQUEST_SERVER > m_server
Definition api_server.h:150
#define KICOMMON_API
Definition kicommon.h:27