Ardour  9.2-266-g5d535d4cb7
mcp_http_server.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 Frank Povazanj <frank.povazanj@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef _ardour_surface_mcp_http_server_h_
20 #define _ardour_surface_mcp_http_server_h_
21 
22 #include <atomic>
23 #include <stdint.h>
24 #include <string>
25 #include <thread>
26 #include <unordered_map>
27 
28 #include <libwebsockets.h>
29 
30 namespace PBD
31 {
32 class EventLoop;
33 }
34 
35 namespace ARDOUR
36 {
37 class Session;
38 }
39 
40 namespace ArdourSurface
41 {
42 
44 {
45 public:
46  MCPHttpServer (ARDOUR::Session&, uint16_t port, int debug_level, PBD::EventLoop* event_loop);
48 
49  int start ();
50  int stop ();
51  void set_debug_level (int);
52  int debug_level () const;
53 
54 private:
55  struct ClientContext {
56  bool mcp_post;
58  std::string request_body;
59  std::string response_body;
60  };
61 
62  typedef std::unordered_map<struct lws*, ClientContext> ClientMap;
63 
65  uint16_t _port;
66  std::atomic<int> _debug_level;
68  struct lws_context* _context;
69  struct lws_protocols _protocols[2];
70  struct lws_context_creation_info _info;
72  std::thread _service_thread;
73  bool _running;
74 
75  void run ();
76 
77  ClientContext& client (struct lws*);
78  void erase_client (struct lws*);
79 
80  int callback (struct lws*, enum lws_callback_reasons, void*, void*, size_t);
81  int handle_http (struct lws*, ClientContext&);
82  int handle_http_body (struct lws*, ClientContext&, void*, size_t);
84  int handle_http_writeable (struct lws*, ClientContext&);
85 
86  int send_json_headers (struct lws*);
87  int send_http_status (struct lws*, unsigned int);
88  int write_json_response (struct lws*, ClientContext&);
89 
90  std::string dispatch_jsonrpc (const std::string&) const;
91 
92  static int lws_callback (struct lws*, enum lws_callback_reasons, void*, void*, size_t);
93 };
94 
95 } // namespace ArdourSurface
96 
97 #endif // _ardour_surface_mcp_http_server_h_
int handle_http_writeable(struct lws *, ClientContext &)
int handle_http_body_completion(struct lws *, ClientContext &)
MCPHttpServer(ARDOUR::Session &, uint16_t port, int debug_level, PBD::EventLoop *event_loop)
int callback(struct lws *, enum lws_callback_reasons, void *, void *, size_t)
std::string dispatch_jsonrpc(const std::string &) const
int handle_http(struct lws *, ClientContext &)
std::atomic< int > _debug_level
static int lws_callback(struct lws *, enum lws_callback_reasons, void *, void *, size_t)
int send_json_headers(struct lws *)
struct lws_protocols _protocols[2]
ClientContext & client(struct lws *)
struct lws_context_creation_info _info
int write_json_response(struct lws *, ClientContext &)
struct lws_context * _context
int handle_http_body(struct lws *, ClientContext &, void *, size_t)
int send_http_status(struct lws *, unsigned int)
void erase_client(struct lws *)
std::unordered_map< struct lws *, ClientContext > ClientMap
DebugBits EventLoop
Definition: axis_view.h:42