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 along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef KICAD_API_SERVER_H
22#define KICAD_API_SERVER_H
23
24#include <atomic>
25#include <memory>
26#include <set>
27#include <string>
28
29#include <wx/event.h>
30#include <wx/filename.h>
31
32#include <kicommon.h>
33
34class API_HANDLER;
36class wxEvtHandler;
37
38
39wxDECLARE_EVENT( API_REQUEST_EVENT, wxCommandEvent );
40
41
42class KICOMMON_API KICAD_API_SERVER : public wxEvtHandler
43{
44public:
45 KICAD_API_SERVER( bool aAutoStart = true );
46
48
49 void Start();
50
51 void Stop();
52
53 bool Running() const;
54
65 void RegisterHandler( API_HANDLER* aHandler );
66
67 void DeregisterHandler( API_HANDLER* aHandler );
68
69 void SetReadyToReply( bool aReady = true )
70 {
71 m_readyToReply.store( aReady, std::memory_order_release );
72 }
73
74 void SetSocketPath( const wxString& aSocketPath )
75 {
76 m_socketPathOverride = aSocketPath;
77 }
78
79 std::string SocketPath() const;
80
81 const std::string& Token() const { return m_token; }
82
83private:
84
92 void onApiRequest( std::string* aRequest );
93
98 void handleApiEvent( wxCommandEvent& aEvent );
99
100 void handleApiRequestString( std::string& aRequestString );
101
102 void log( const std::string& aOutput );
103
104 std::unique_ptr<KINNG_REQUEST_SERVER> m_server;
105
106 std::set<API_HANDLER*> m_handlers;
107
108 std::string m_token;
109
110 std::atomic<bool> m_readyToReply;
111
113
114 static wxString s_logFileName;
115
116 wxFileName m_logFilePath;
117};
118
119#endif //KICAD_API_SERVER_H
wxDECLARE_EVENT(API_REQUEST_EVENT, wxCommandEvent)
std::string m_token
Definition api_server.h:108
bool Running() const
wxFileName m_logFilePath
Definition api_server.h:116
static wxString s_logFileName
Definition api_server.h:114
void RegisterHandler(API_HANDLER *aHandler)
Adds a new request handler to the server.
wxString m_socketPathOverride
Definition api_server.h:112
std::set< API_HANDLER * > m_handlers
Definition api_server.h:106
std::atomic< bool > m_readyToReply
Definition api_server.h:110
const std::string & Token() const
Definition api_server.h:81
void SetReadyToReply(bool aReady=true)
Definition api_server.h:69
void SetSocketPath(const wxString &aSocketPath)
Definition api_server.h:74
KICAD_API_SERVER(bool aAutoStart=true)
void DeregisterHandler(API_HANDLER *aHandler)
std::unique_ptr< KINNG_REQUEST_SERVER > m_server
Definition api_server.h:104
#define KICOMMON_API
Definition kicommon.h:27