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 <signal.h>
6#include <boost/uuid/uuid.hpp>
7#include <future>
8#include <pybind11/pybind11.h>
9#include <pybind11/embed.h>
10#include "nlohmann/json.hpp"
11#include "websocket_server.hh"
12
13#include "Stopwatch.hh"
14
30
31class Server {
32 public:
33 Server();
34 Server(const Server&)=delete;
35 Server(const std::string& socket);
36 virtual ~Server();
37
44
45 void run(int port=0, bool exit_on_disconnect=true);
46
47
52
54 public:
57
58 void write(const std::string& txt);
59 void clear();
60 std::string str() const;
61 private:
62 std::string collect;
63 };
64
66
69
91
92 virtual uint64_t send(const std::string& output, const std::string& msg_type,
93 uint64_t parent_id=0, uint64_t cell_id=0, bool last_in_sequence=false);
94// virtual void send_completion(uint64_t id, int pos, int alternative, std::string original, std::string completed);
95
96 void send_progress_update(const std::string& msg, int n, int total);
97 void send_json(const std::string&);
98
99 bool handles(const std::string& otype) const;
100 std::string architecture() const;
101
105 void wait_for_job();
106
109 void wait_for_websocket();
110
111 protected:
112 void init();
113
114 // WebSocket++ dependent parts below.
115 void on_message(websocket_server::id_type id, const std::string& msg,
116 const websocket_server::request_type& req, const std::string& ip_address);
120
121 // Connection tracking. There can be multiple connections to
122 // the server, but they all have access to the same Python
123 // scope. With multiple connections, one can inspect the Python
124 // stack from a different client (e.g. for debugging purposes).
125 // All connections share the same authentication token.
126
128 public:
129 Connection();
130
132 boost::uuids::uuid uuid;
133 };
134 typedef std::map<websocket_server::id_type, Connection> ConnectionMap;
136
137 // Authentication token, needs to be sent along with any message.
138 // Gets set when the server announces its port.
140
141 // Mutex to be able to use the websocket layer from both the
142 // main loop and the python-running thread.
143 std::mutex ws_mutex;
144
145
146 // Basics for the working thread that processes blocks.
147 std::thread runner;
149 std::condition_variable block_available;
152 unsigned long main_thread_id;
153
154 // Data and connection info for a single block of code.
155 class Block {
156 public:
157 Block(websocket_server::id_type, const std::string&, uint64_t id, const std::string& msg_type);
158 websocket_server::id_type ws_id; // FIXME: decouple from websocket?
159 std::string msg_type;
160 std::string input;
161 std::string output;
162 std::string error;
163 uint64_t cell_id;
164
165 // Response message, partially filled in when the
166 // request comes in.
167 nlohmann::json response;
168 };
169 std::queue<Block> block_queue;
171 uint64_t current_id; // id of the block given to us by the client.
172
173 // Run a piece of Python code. This is called from a separate
174 // thread constructed by on_message().
175 std::string run_string(const std::string&, bool handle_output=true);
176
183
184 virtual void on_block_finished(Block);
185 virtual void on_block_error(Block);
186 virtual void on_kernel_fault(Block);
187
188// uint64_t return_cell_id; // serial number of cells generated by us.
189
192 void stop_block();
194 std::future<std::string> job;
195
199
200 void dispatch_message(websocket_server::id_type, const std::string& json_string);
201
202 // Python global info.
203 pybind11::scoped_interpreter guard;
204 pybind11::module main_module;
205 pybind11::object main_namespace;
206
207 // int cells_ran;
208 };
Definition Server.hh:155
std::string input
Definition Server.hh:160
uint64_t cell_id
Definition Server.hh:163
websocket_server::id_type ws_id
Definition Server.hh:158
std::string output
Definition Server.hh:161
std::string msg_type
Definition Server.hh:159
std::string error
Definition Server.hh:162
nlohmann::json response
Definition Server.hh:167
Python output catching.
Definition Server.hh:53
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:62
void write(const std::string &txt)
Definition Server.cc:60
Definition Server.hh:127
boost::uuids::uuid uuid
Definition Server.hh:132
Connection()
Definition Server.cc:285
websocket_server::id_type ws_id
Definition Server.hh:131
Object representing a Cadabra server, capable of receiving messages on a websocket,...
Definition Server.hh:31
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:448
void send_progress_update(const std::string &msg, int n, int total)
Definition Server.cc:620
void init()
Definition Server.cc:135
CatchOutput catchErr
Definition Server.hh:65
virtual void on_block_error(Block)
Definition Server.cc:650
virtual void on_block_finished(Block)
Called by the run_block() thread upon completion of the task.
Definition Server.cc:543
pybind11::object main_namespace
Definition Server.hh:205
std::thread runner
Definition Server.hh:147
std::string authentication_token
Definition Server.hh:139
pybind11::module main_module
Definition Server.hh:204
virtual void on_kernel_fault(Block)
Definition Server.cc:679
ConnectionMap connections
Definition Server.hh:135
std::future< std::string > job
Definition Server.hh:194
void on_open(websocket_server::id_type id)
Definition Server.cc:290
virtual ~Server()
Definition Server.cc:47
Stopwatch sympy_stopwatch
Definition Server.hh:68
std::mutex ws_mutex
Definition Server.hh:143
CatchOutput catchOut
Definition Server.hh:65
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:579
unsigned long main_thread_id
Definition Server.hh:152
websocket_server wserver
Definition Server.hh:119
std::map< websocket_server::id_type, Connection > ConnectionMap
Definition Server.hh:134
void wait_for_job()
Thread entry point for the code that waits for blocks to appear on the block queue,...
Definition Server.cc:339
bool handles(const std::string &otype) const
Definition Server.cc:573
int run_on_port
Definition Server.hh:151
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:466
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:708
Server(const Server &)=delete
std::string architecture() const
Definition Server.cc:77
std::mutex block_available_mutex
Definition Server.hh:148
pybind11::scoped_interpreter guard
Definition Server.hh:203
std::string run_string(const std::string &, bool handle_output=true)
Definition Server.cc:212
std::queue< Block > block_queue
Definition Server.hh:169
void on_close(websocket_server::id_type id)
Definition Server.cc:299
Server()
Definition Server.cc:32
bool exit_on_disconnect
Definition Server.hh:150
std::condition_variable block_available
Definition Server.hh:149
void stop_block()
Halt the currently running block and prevent execution of any further blocks that may still be on the...
Definition Server.cc:422
void send_json(const std::string &)
Definition Server.cc:643
websocket_server::id_type current_ws_id
Definition Server.hh:170
Stopwatch server_stopwatch
Definition Server.hh:67
void wait_for_websocket()
Thread entry point for code that sets up and runs the websocket listener.
Definition Server.cc:318
uint64_t current_id
Definition Server.hh:171
bool started
Definition Server.hh:193
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