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
40
41wxDECLARE_EVENT( API_REQUEST_EVENT, wxCommandEvent );
42
43
44class KICOMMON_API KICAD_API_SERVER : public wxEvtHandler
45{
46public:
47 KICAD_API_SERVER( bool aAutoStart = true );
48
50
51 void Start();
52
53 void Stop();
54
55 bool Running() const;
56
67 void RegisterHandler( API_HANDLER* aHandler );
68
69 void DeregisterHandler( API_HANDLER* aHandler );
70
71 void SetReadyToReply( bool aReady = true )
72 {
73 m_readyToReply.store( aReady, std::memory_order_release );
74 }
75
76 void SetSocketPath( const wxString& aSocketPath )
77 {
78 m_socketPathOverride = aSocketPath;
79 }
80
81 std::string SocketPath() const;
82
83 const std::string& Token() const { return m_token; }
84
88 static wxFileName StandardSocketPath();
89
93 static std::string StandardSocketUrl();
94
100 void NotifyNetSettingsChanged();
101
106 void NotifyDocumentRenamed( kiapi::common::types::DocumentType aType, const wxString& aOldPath,
107 const wxString& aNewPath );
108
112 void SetRenameListener( std::function<void( kiapi::common::types::DocumentType,
113 const wxString&, const wxString& )> aListener )
114 {
115 m_renameListener = std::move( aListener );
116 }
117
118private:
119
127 void onApiRequest( std::string* aRequest );
128
133 void handleApiEvent( wxCommandEvent& aEvent );
134
135 void handleApiRequestString( std::string& aRequestString );
136
137 void log( const std::string& aOutput );
138
139 std::unique_ptr<KINNG_REQUEST_SERVER> m_server;
140
141 std::set<API_HANDLER*> m_handlers;
142
143 std::string m_token;
144
145 std::atomic<bool> m_readyToReply;
146
147 std::function<void( kiapi::common::types::DocumentType, const wxString&, const wxString& )> m_renameListener;
148
150
151 static wxString s_logFileName;
152
153 wxFileName m_logFilePath;
154};
155
156#endif //KICAD_API_SERVER_H
wxDECLARE_EVENT(API_REQUEST_EVENT, wxCommandEvent)
std::string m_token
Definition api_server.h:143
bool Running() const
wxFileName m_logFilePath
Definition api_server.h:153
std::function< void(kiapi::common::types::DocumentType, const wxString &, const wxString &)> m_renameListener
Definition api_server.h:147
static wxString s_logFileName
Definition api_server.h:151
void RegisterHandler(API_HANDLER *aHandler)
Adds a new request handler to the server.
wxString m_socketPathOverride
Definition api_server.h:149
std::set< API_HANDLER * > m_handlers
Definition api_server.h:141
std::atomic< bool > m_readyToReply
Definition api_server.h:145
const std::string & Token() const
Definition api_server.h:83
void SetReadyToReply(bool aReady=true)
Definition api_server.h:71
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:112
void SetSocketPath(const wxString &aSocketPath)
Definition api_server.h:76
KICAD_API_SERVER(bool aAutoStart=true)
void DeregisterHandler(API_HANDLER *aHandler)
std::unique_ptr< KINNG_REQUEST_SERVER > m_server
Definition api_server.h:139
#define KICOMMON_API
Definition kicommon.h:27