Cadabra
Computer algebra system for field theory problems
Server.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <websocketpp/server.hpp>
5 #include <websocketpp/config/asio_no_tls.hpp>
6 #include <websocketpp/common/functional.hpp>
7 #include <string>
8 #include <signal.h>
9 #include <boost/uuid/uuid.hpp>
10 #include <future>
11 #include <pybind11/pybind11.h>
12 #include <pybind11/embed.h>
13 
14 #include "Stopwatch.hh"
15 
31 
32 class Server {
33  public:
34  Server();
35  Server(const Server&)=delete;
36  Server(const std::string& socket);
37  ~Server();
38 
42  void run();
43 
44 
49 
50  class CatchOutput {
51  public:
52  CatchOutput();
53  CatchOutput(const CatchOutput&);
54 
55  void write(const std::string& txt);
56  void clear();
57  std::string str() const;
58  private:
59  std::string collect;
60  };
61 
63 
66 
84 
85  uint64_t send(const std::string& output, const std::string& msg_type, uint64_t parent_id=0, bool last_in_sequence=false);
86  void send_json(const std::string&);
87 
88  bool handles(const std::string& otype) const;
89  std::string architecture() const;
90 
91  private:
92  void init();
93 
94  // WebSocket++ dependent parts below.
95  typedef websocketpp::server<websocketpp::config::asio> WebsocketServer;
96  void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket & s);
97  void on_message(websocketpp::connection_hdl hdl, WebsocketServer::message_ptr msg);
98  void on_open(websocketpp::connection_hdl hdl);
99  void on_close(websocketpp::connection_hdl hdl);
101  std::string socket_name;
102 
103  // Connection tracking. There can be multiple connections to
104  // the server, but they all have access to the same Python
105  // scope. With multiple connections, one can inspect the Python
106  // stack from a different client (e.g. for debugging purposes).
107 
108  class Connection {
109  public:
110  Connection();
111 
112  websocketpp::connection_hdl hdl;
113  boost::uuids::uuid uuid;
114  };
115  typedef std::map<websocketpp::connection_hdl, Connection,
116  std::owner_less<websocketpp::connection_hdl>> ConnectionMap;
118 
119  // Mutex to be able to use the websocket layer from both the
120  // main loop and the python-running thread.
121  std::mutex ws_mutex;
122 
123 
124  // Basics for the working thread that processes blocks.
125  std::thread runner;
127  std::condition_variable block_available;
128  void wait_for_job();
129 
130  // Data and connection info for a single block of code.
131  class Block {
132  public:
133  Block(websocketpp::connection_hdl, const std::string&, uint64_t id);
134  websocketpp::connection_hdl hdl; // FIXME: decouple from websocket?
135  std::string input;
136  std::string output;
137  std::string error;
138  uint64_t cell_id;
139  };
140  std::queue<Block> block_queue;
141  websocketpp::connection_hdl current_hdl;
142  uint64_t current_id; // id of the block given to us by the client.
143 
144  // Run a piece of Python code. This is called from a separate
145  // thread constructed by on_message().
146  std::string run_string(const std::string&, bool handle_output=true);
147 
154 
155  void on_block_finished(Block);
156  void on_block_error(Block);
157  void on_kernel_fault(Block);
158 
159 
160 
161 
162  uint64_t return_cell_id; // serial number of cells generated by us.
163 
166  void stop_block();
167  bool started;
168  std::future<std::string> job;
169 
173 
174  void dispatch_message(websocketpp::connection_hdl, const std::string& json_string);
175 
176  // Python global info.
177  pybind11::scoped_interpreter guard;
178  pybind11::module main_module;
179  pybind11::object main_namespace;
180 
182 };
183 
void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket &s)
Definition: Server.cc:186
void on_kernel_fault(Block)
Definition: Server.cc:435
Definition: Server.hh:131
Object representing a Cadabra server, capable of receiving messages on a websocket, running Python code, and sending output back to the client.
Definition: Server.hh:32
std::string error
Definition: Server.hh:137
pybind11::module main_module
Definition: Server.hh:178
int cells_ran
Definition: Server.hh:181
std::string run_string(const std::string &, bool handle_output=true)
Definition: Server.cc:143
pybind11::object main_namespace
Definition: Server.hh:179
std::mutex ws_mutex
Definition: Server.hh:121
~Server()
Definition: Server.cc:43
void write(const std::string &txt)
Definition: Server.cc:56
websocketpp::config::asio_client::message_type::ptr message_ptr
Definition: ComputeThread.hh:13
void on_block_error(Block)
Definition: Server.cc:409
void on_open(websocketpp::connection_hdl hdl)
Definition: Server.cc:197
void on_block_finished(Block)
Called by the run_block() thread upon completion of the task.
Definition: Server.cc:358
std::thread runner
Definition: Server.hh:125
The Stopwach class provides a simple interace to allow timing function calls etc...
Definition: Stopwatch.hh:107
void send_json(const std::string &)
Definition: Server.cc:402
std::string socket_name
Definition: Server.hh:101
void dispatch_message(websocketpp::connection_hdl, const std::string &json_string)
Takes a JSON encoded message and performs the required action to process it.
Definition: Server.cc:312
CatchOutput catchOut
Definition: Server.hh:62
std::string input
Definition: Server.hh:135
uint64_t send(const std::string &output, const std::string &msg_type, uint64_t parent_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:369
std::condition_variable block_available
Definition: Server.hh:127
Definition: Server.hh:108
bool started
Definition: Server.hh:167
std::string collect
Definition: Server.hh:59
void wait_for_job()
Definition: Server.cc:221
uint64_t cell_id
Definition: Server.hh:138
Python output catching.
Definition: Server.hh:50
CatchOutput()
Definition: Server.cc:48
std::queue< Block > block_queue
Definition: Server.hh:140
uint64_t return_cell_id
Definition: Server.hh:162
void on_close(websocketpp::connection_hdl hdl)
Definition: Server.cc:206
WebsocketServer wserver
Definition: Server.hh:100
void on_message(websocketpp::connection_hdl hdl, WebsocketServer::message_ptr msg)
Definition: Server.cc:297
void stop_block()
Halt the currently running block and prevent execution of any further blocks that may still be on the...
Definition: Server.cc:284
void clear()
Definition: Server.cc:62
uint64_t current_id
Definition: Server.hh:142
CatchOutput catchErr
Definition: Server.hh:62
websocketpp::connection_hdl current_hdl
Definition: Server.hh:141
std::string str() const
Definition: Server.cc:68
std::map< websocketpp::connection_hdl, Connection, std::owner_less< websocketpp::connection_hdl > > ConnectionMap
Definition: Server.hh:116
void run()
The only user-visible part: just instantiate a server object and start it with run().
Definition: Server.cc:461
std::mutex block_available_mutex
Definition: Server.hh:126
Server()
Definition: Server.cc:28
void init()
Definition: Server.cc:93
websocketpp::connection_hdl hdl
Definition: Server.hh:134
Stopwatch server_stopwatch
Definition: Server.hh:64
websocketpp::connection_hdl hdl
Definition: Server.hh:112
std::future< std::string > job
Definition: Server.hh:168
std::string architecture() const
Definition: Server.cc:73
Connection()
Definition: Server.cc:192
bool handles(const std::string &otype) const
Definition: Server.cc:363
pybind11::scoped_interpreter guard
Definition: Server.hh:177
std::string output
Definition: Server.hh:136
Stopwatch sympy_stopwatch
Definition: Server.hh:65
Block(websocketpp::connection_hdl, const std::string &, uint64_t id)
Definition: Server.cc:292
websocketpp::server< websocketpp::config::asio > WebsocketServer
Definition: Server.hh:95
boost::uuids::uuid uuid
Definition: Server.hh:113
ConnectionMap connections
Definition: Server.hh:117