Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
Server.hh
Go to the documentation of this file.
1
2#pragma once
3
4#include <string>
5#include <set>
6#include <deque>
7#include <signal.h>
8#include <boost/uuid/uuid.hpp>
9#include <future>
10#include <pybind11/pybind11.h>
11#include <pybind11/embed.h>
12#include "nlohmann/json.hpp"
13#include "websocket_server.hh"
14
15#include "Stopwatch.hh"
16
32
33class Server {
34 public:
35 Server();
36 Server(const Server&)=delete;
37 Server(const std::string& socket);
38 virtual ~Server();
39
46
47 void run(int port=0, bool exit_on_disconnect=true);
48
49
54
56 public:
59
60 void write(const std::string& txt);
61 void clear();
62 std::string str() const;
63 private:
64 std::string collect;
65 };
66
68
71
93
94 virtual uint64_t send(const std::string& output, const std::string& msg_type,
95 uint64_t parent_id=0, uint64_t cell_id=0, bool last_in_sequence=false);
96
97 void send_progress_update(const std::string& msg, int n, int total);
98 void send_json(const std::string&);
99
100 bool handles(const std::string& otype) const;
101 std::string architecture() const;
102
106 void wait_for_job();
107
110 void wait_for_websocket();
111
112 protected:
113 void init();
114
115 // WebSocket++ dependent parts below.
116 void on_message(websocket_server::id_type id, const std::string& msg,
117 const websocket_server::request_type& req, const std::string& ip_address);
121
122 // Connection tracking. There can be multiple connections to
123 // the server, but they all have access to the same Python
124 // scope. With multiple connections, one can inspect the Python
125 // stack from a different client (e.g. for debugging purposes).
126 // All connections share the same authentication token.
127
129 public:
130 Connection();
131
133 boost::uuids::uuid uuid;
134 };
135 typedef std::map<websocket_server::id_type, Connection> ConnectionMap;
137
138 // Authentication token, needs to be sent along with any message.
139 // Gets set when the server announces its port.
141
142 // Mutex to be able to use the websocket layer from both the
143 // main loop and the python-running thread.
144 std::mutex ws_mutex;
145
146
147 // Basics for the working thread that processes blocks.
148 std::thread runner;
150 std::condition_variable block_available;
153 unsigned long main_thread_id;
154
155 // Data and connection info for a single block of code.
156 class Block {
157 public:
158 Block();
159 Block(websocket_server::id_type, const std::string&, uint64_t id, const std::string& msg_type);
160
161 websocket_server::id_type ws_id; // FIXME: decouple from websocket?
162 std::string msg_type;
163 std::string input;
164 std::string output;
165 std::string error;
166 uint64_t cell_id;
167 std::set<std::string> variables;
168 std::set<std::string> remove_variable_assignments;
169
170 // When a cell is re-run on variable change, we re-use the output cells of the
171 // previous run. The IDs of these cells are sent to us by the frontend. We
172 // store them here, and then pop them off the front for each call to `send`.
173
174 std::deque<uint64_t> reuse_output_cell_ids;
175
176 // Response message, partially filled in when the
177 // request comes in.
178
179 nlohmann::json response;
180 };
181 std::queue<Block> block_queue;
184 uint64_t current_id; // id of the block given to us by the client.
185
186 // Run a piece of Python code. This is called from a separate
187 // thread constructed by on_message().
188 std::string run_string(const std::string&,
189 bool handle_output=true,
190 bool extract_variables=false,
191 std::set<std::string> remove_variable_assignments=std::set<std::string>());
192
193 std::set<std::string> run_string_variables;
194
201
202 virtual void on_block_finished(Block);
203 virtual void on_block_error(Block);
204 virtual void on_kernel_fault(Block);
205
208
209 void stop_block();
211 std::future<std::string> job;
212
216
217 void dispatch_message(websocket_server::id_type, const std::string& json_string);
218
219 // Python global info.
220 pybind11::scoped_interpreter guard;
221 pybind11::module main_module;
222 pybind11::object main_namespace;
223 };
Definition Server.hh:156
std::set< std::string > remove_variable_assignments
Definition Server.hh:168
std::set< std::string > variables
Definition Server.hh:167
std::string input
Definition Server.hh:163
uint64_t cell_id
Definition Server.hh:166
Block()
Definition Server.cc:458
websocket_server::id_type ws_id
Definition Server.hh:161
std::string output
Definition Server.hh:164
std::deque< uint64_t > reuse_output_cell_ids
Definition Server.hh:174
std::string msg_type
Definition Server.hh:162
std::string error
Definition Server.hh:165
nlohmann::json response
Definition Server.hh:179
Python output catching.
Definition Server.hh:55
void clear()
Definition Server.cc:66
std::string str() const
Definition Server.cc:72
CatchOutput()
Definition Server.cc:52
std::string collect
Definition Server.hh:64
void write(const std::string &txt)
Definition Server.cc:60
Definition Server.hh:128
boost::uuids::uuid uuid
Definition Server.hh:133
Connection()
Definition Server.cc:303
websocket_server::id_type ws_id
Definition Server.hh:132
Object representing a Cadabra server, capable of receiving messages on a websocket,...
Definition Server.hh:33
void on_message(websocket_server::id_type id, const std::string &msg, const websocket_server::request_type &req, const std::string &ip_address)
Definition Server.cc:472
Block current_block
Definition Server.hh:182
void send_progress_update(const std::string &msg, int n, int total)
Definition Server.cc:661
void init()
Definition Server.cc:135
CatchOutput catchErr
Definition Server.hh:67
virtual void on_block_error(Block)
Definition Server.cc:691
virtual void on_block_finished(Block)
Called by the run_block() thread upon completion of the task.
Definition Server.cc:573
pybind11::object main_namespace
Definition Server.hh:222
std::thread runner
Definition Server.hh:148
std::string authentication_token
Definition Server.hh:140
pybind11::module main_module
Definition Server.hh:221
virtual void on_kernel_fault(Block)
Definition Server.cc:720
ConnectionMap connections
Definition Server.hh:136
std::future< std::string > job
Definition Server.hh:211
void on_open(websocket_server::id_type id)
Definition Server.cc:308
virtual ~Server()
Definition Server.cc:47
Stopwatch sympy_stopwatch
Definition Server.hh:70
std::mutex ws_mutex
Definition Server.hh:144
CatchOutput catchOut
Definition Server.hh:67
std::set< std::string > run_string_variables
Definition Server.hh:193
virtual uint64_t send(const std::string &output, const std::string &msg_type, uint64_t parent_id=0, uint64_t cell_id=0, bool last_in_sequence=false)
Raw code to send a string (which must be JSON formatted) as a message to the client.
Definition Server.cc:612
unsigned long main_thread_id
Definition Server.hh:153
websocket_server wserver
Definition Server.hh:120
std::map< websocket_server::id_type, Connection > ConnectionMap
Definition Server.hh:135
std::string run_string(const std::string &, bool handle_output=true, bool extract_variables=false, std::set< std::string > remove_variable_assignments=std::set< std::string >())
Definition Server.cc:212
void wait_for_job()
Thread entry point for the code that waits for blocks to appear on the block queue,...
Definition Server.cc:357
bool handles(const std::string &otype) const
Definition Server.cc:606
int run_on_port
Definition Server.hh:152
void dispatch_message(websocket_server::id_type, const std::string &json_string)
Takes a JSON encoded message and performs the required action to process it.
Definition Server.cc:490
void run(int port=0, bool exit_on_disconnect=true)
The only user-visible part: just instantiate a server object and start it with run().
Definition Server.cc:749
Server(const Server &)=delete
std::string architecture() const
Definition Server.cc:77
std::mutex block_available_mutex
Definition Server.hh:149
pybind11::scoped_interpreter guard
Definition Server.hh:220
std::queue< Block > block_queue
Definition Server.hh:181
void on_close(websocket_server::id_type id)
Definition Server.cc:317
Server()
Definition Server.cc:32
bool exit_on_disconnect
Definition Server.hh:151
std::condition_variable block_available
Definition Server.hh:150
void stop_block()
Halt the currently running block and prevent execution of any further blocks that may still be on the...
Definition Server.cc:441
void send_json(const std::string &)
Definition Server.cc:684
websocket_server::id_type current_ws_id
Definition Server.hh:183
Stopwatch server_stopwatch
Definition Server.hh:69
void wait_for_websocket()
Thread entry point for code that sets up and runs the websocket listener.
Definition Server.cc:336
uint64_t current_id
Definition Server.hh:184
bool started
Definition Server.hh:210
The Stopwach class provides a simple interace to allow timing function calls etc.....
Definition Stopwatch.hh:107
Definition websocket_server.hh:16
boost::beast::http::request< boost::beast::http::string_body > request_type
Definition websocket_server.hh:19
std::size_t id_type
Definition websocket_server.hh:18