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  virtual ~Server();
38 
45 
46  void run(int port=0, bool exit_on_disconnect=true);
47 
48 
53 
54  class CatchOutput {
55  public:
56  CatchOutput();
57  CatchOutput(const CatchOutput&);
58 
59  void write(const std::string& txt);
60  void clear();
61  std::string str() const;
62  private:
63  std::string collect;
64  };
65 
67 
70 
88 
89  virtual uint64_t send(const std::string& output, const std::string& msg_type, uint64_t parent_id=0, bool last_in_sequence=false);
90 
91  void send_json(const std::string&);
92 
93  bool handles(const std::string& otype) const;
94  std::string architecture() const;
95 
98 
99  void wait_for_job();
100 
101  protected:
102  void init();
103 
104  // WebSocket++ dependent parts below.
105  typedef websocketpp::server<websocketpp::config::asio> WebsocketServer;
106  void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket & s);
107  void on_message(websocketpp::connection_hdl hdl, WebsocketServer::message_ptr msg);
108  void on_open(websocketpp::connection_hdl hdl);
109  void on_close(websocketpp::connection_hdl hdl);
111  std::string socket_name;
112 
113  // Connection tracking. There can be multiple connections to
114  // the server, but they all have access to the same Python
115  // scope. With multiple connections, one can inspect the Python
116  // stack from a different client (e.g. for debugging purposes).
117  // All connections share the same authentication token.
118 
119  class Connection {
120  public:
121  Connection();
122 
123  websocketpp::connection_hdl hdl;
124  boost::uuids::uuid uuid;
125  };
126  typedef std::map<websocketpp::connection_hdl, Connection,
127  std::owner_less<websocketpp::connection_hdl>> ConnectionMap;
129 
130  // Authentication token, needs to be sent along with any message.
131  // Gets set when the server announces its port.
132  std::string authentication_token;
133 
134  // Mutex to be able to use the websocket layer from both the
135  // main loop and the python-running thread.
136  std::mutex ws_mutex;
137 
138 
139  // Basics for the working thread that processes blocks.
140  std::thread runner;
142  std::condition_variable block_available;
144 
145  // Data and connection info for a single block of code.
146  class Block {
147  public:
148  Block(websocketpp::connection_hdl, const std::string&, uint64_t id);
149  websocketpp::connection_hdl hdl; // FIXME: decouple from websocket?
150  std::string input;
151  std::string output;
152  std::string error;
153  uint64_t cell_id;
154  };
155  std::queue<Block> block_queue;
156  websocketpp::connection_hdl current_hdl;
157  uint64_t current_id; // id of the block given to us by the client.
158 
159  // Run a piece of Python code. This is called from a separate
160  // thread constructed by on_message().
161  std::string run_string(const std::string&, bool handle_output=true);
162 
169 
170  virtual void on_block_finished(Block);
171  virtual void on_block_error(Block);
172  virtual void on_kernel_fault(Block);
173 
174 
175 
176 
177 // uint64_t return_cell_id; // serial number of cells generated by us.
178 
181  void stop_block();
182  bool started;
183  std::future<std::string> job;
184 
188 
189  void dispatch_message(websocketpp::connection_hdl, const std::string& json_string);
190 
191  // Python global info.
192  pybind11::scoped_interpreter guard;
193  pybind11::module main_module;
194  pybind11::object main_namespace;
195 
196  // int cells_ran;
197  };
198 
Server::send_json
void send_json(const std::string &)
Definition: Server.cc:455
Server::catchOut
CatchOutput catchOut
Definition: Server.hh:66
Server::Block::Block
Block(websocketpp::connection_hdl, const std::string &, uint64_t id)
Definition: Server.cc:334
message_ptr
websocketpp::config::asio_client::message_type::ptr message_ptr
Definition: ComputeThread.hh:13
Server::Block::input
std::string input
Definition: Server.hh:150
Server::connections
ConnectionMap connections
Definition: Server.hh:128
Server::runner
std::thread runner
Definition: Server.hh:140
Server::Block
Definition: Server.hh:146
Server::server_stopwatch
Stopwatch server_stopwatch
Definition: Server.hh:68
Server::Block::error
std::string error
Definition: Server.hh:152
Server::main_namespace
pybind11::object main_namespace
Definition: Server.hh:194
Server::~Server
virtual ~Server()
Definition: Server.cc:50
Server::Connection::hdl
websocketpp::connection_hdl hdl
Definition: Server.hh:123
Server::on_socket_init
void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket &s)
Definition: Server.cc:209
Server::socket_name
std::string socket_name
Definition: Server.hh:111
Server::Block::cell_id
uint64_t cell_id
Definition: Server.hh:153
Server::ws_mutex
std::mutex ws_mutex
Definition: Server.hh:136
Server::Connection::Connection
Connection()
Definition: Server.cc:216
Server::wserver
WebsocketServer wserver
Definition: Server.hh:110
Stopwatch
Definition: Stopwatch.hh:107
Server::CatchOutput::write
void write(const std::string &txt)
Definition: Server.cc:63
Server::started
bool started
Definition: Server.hh:182
Server::CatchOutput::clear
void clear()
Definition: Server.cc:69
Server::stop_block
void stop_block()
Halt the currently running block and prevent execution of any further blocks that may still be on the...
Definition: Server.cc:326
Server::CatchOutput::CatchOutput
CatchOutput()
Definition: Server.cc:55
Server::CatchOutput::collect
std::string collect
Definition: Server.hh:63
Server::wait_for_job
void wait_for_job()
Start a thread which waits for blocks to appear on the block queue, and executes them in turn.
Definition: Server.cc:247
Server::on_close
void on_close(websocketpp::connection_hdl hdl)
Definition: Server.cc:230
Server::block_available
std::condition_variable block_available
Definition: Server.hh:142
Stopwatch.hh
Server::Block::output
std::string output
Definition: Server.hh:151
Server::Connection
Definition: Server.hh:119
Server::authentication_token
std::string authentication_token
Definition: Server.hh:132
Server::init
void init()
Definition: Server.cc:100
Server::send
virtual 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:422
Server::job
std::future< std::string > job
Definition: Server.hh:183
Server::main_module
pybind11::module main_module
Definition: Server.hh:193
Server::architecture
std::string architecture() const
Definition: Server.cc:80
Server::catchErr
CatchOutput catchErr
Definition: Server.hh:66
Server::on_block_finished
virtual void on_block_finished(Block)
Called by the run_block() thread upon completion of the task.
Definition: Server.cc:411
Server::block_available_mutex
std::mutex block_available_mutex
Definition: Server.hh:141
Server::current_hdl
websocketpp::connection_hdl current_hdl
Definition: Server.hh:156
Server::handles
bool handles(const std::string &otype) const
Definition: Server.cc:416
Server::WebsocketServer
websocketpp::server< websocketpp::config::asio > WebsocketServer
Definition: Server.hh:105
Server::on_kernel_fault
virtual void on_kernel_fault(Block)
Definition: Server.cc:488
Server::Block::hdl
websocketpp::connection_hdl hdl
Definition: Server.hh:149
Server::Connection::uuid
boost::uuids::uuid uuid
Definition: Server.hh:124
Server::run_string
std::string run_string(const std::string &, bool handle_output=true)
Definition: Server.cc:156
Server::guard
pybind11::scoped_interpreter guard
Definition: Server.hh:192
Server::current_id
uint64_t current_id
Definition: Server.hh:157
Server::sympy_stopwatch
Stopwatch sympy_stopwatch
Definition: Server.hh:69
Server::CatchOutput
Python output catching.
Definition: Server.hh:54
Server::on_message
void on_message(websocketpp::connection_hdl hdl, WebsocketServer::message_ptr msg)
Definition: Server.cc:339
Server::run
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:514
Server::dispatch_message
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:356
Server::Server
Server()
Definition: Server.cc:33
Server::on_open
void on_open(websocketpp::connection_hdl hdl)
Definition: Server.cc:221
Server
Definition: Server.hh:32
Server::block_queue
std::queue< Block > block_queue
Definition: Server.hh:155
Server::on_block_error
virtual void on_block_error(Block)
Definition: Server.cc:462
Server::ConnectionMap
std::map< websocketpp::connection_hdl, Connection, std::owner_less< websocketpp::connection_hdl > > ConnectionMap
Definition: Server.hh:127
Server::CatchOutput::str
std::string str() const
Definition: Server.cc:75
Server::exit_on_disconnect
bool exit_on_disconnect
Definition: Server.hh:143