Cadabra
Computer algebra system for field theory problems
|
Object representing a Cadabra server, capable of receiving messages on a websocket, running Python code, and sending output back to the client.
Contains the logic to intercept raw Python output but also provides functionality to the Python side which enables clients to send various objects in JSON encoded form. See Server::on_block_finished for the format of these messages.
Every block is run on the same Python scope. They run sequentially, one at a time, on a thread separate from the websocket++ main loop. When the Python code finishes (or when it is interrupted), this thread locks the socket_mutex and calls on_block_finished().
#include <Server.hh>
Classes | |
class | Block |
class | CatchOutput |
Python output catching. More... | |
class | Connection |
Public Member Functions | |
Server () | |
Server (const Server &)=delete | |
Server (const std::string &socket) | |
virtual | ~Server () |
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(). | |
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. | |
void | send_progress_update (const std::string &msg, int n, int total) |
void | send_json (const std::string &) |
bool | handles (const std::string &otype) const |
std::string | architecture () const |
void | wait_for_job () |
Thread entry point for the code that waits for blocks to appear on the block queue, and executes them in turn. | |
void | wait_for_websocket () |
Thread entry point for code that sets up and runs the websocket listener. | |
Public Attributes | |
CatchOutput | catchOut |
CatchOutput | catchErr |
Stopwatch | server_stopwatch |
Stopwatch | sympy_stopwatch |
Protected Types | |
typedef std::map< websocket_server::id_type, Connection > | ConnectionMap |
Protected Member Functions | |
void | init () |
void | on_message (websocket_server::id_type id, const std::string &msg, const websocket_server::request_type &req, const std::string &ip_address) |
void | on_open (websocket_server::id_type id) |
void | on_close (websocket_server::id_type id) |
std::string | run_string (const std::string &, bool handle_output=true) |
virtual void | on_block_finished (Block) |
Called by the run_block() thread upon completion of the task. | |
virtual void | on_block_error (Block) |
virtual void | on_kernel_fault (Block) |
void | stop_block () |
Halt the currently running block and prevent execution of any further blocks that may still be on the queue. | |
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. | |
Protected Attributes | |
websocket_server | wserver |
ConnectionMap | connections |
std::string | authentication_token |
std::mutex | ws_mutex |
std::thread | runner |
std::mutex | block_available_mutex |
std::condition_variable | block_available |
bool | exit_on_disconnect |
int | run_on_port |
unsigned long | main_thread_id |
std::queue< Block > | block_queue |
websocket_server::id_type | current_ws_id |
uint64_t | current_id |
bool | started |
std::future< std::string > | job |
pybind11::scoped_interpreter | guard |
pybind11::module | main_module |
pybind11::object | main_namespace |
|
protected |
Server::Server | ( | ) |
|
delete |
Server::Server | ( | const std::string & | socket | ) |
|
virtual |
std::string Server::architecture | ( | ) | const |
|
protected |
Takes a JSON encoded message and performs the required action to process it.
Where applicable these messages are compatible with IPython's message types, http://ipython.org/ipython-doc/dev/development/messaging.html
bool Server::handles | ( | const std::string & | otype | ) | const |
|
protected |
|
protectedvirtual |
Reimplemented in cadabra::CadabraJupyter.
|
protectedvirtual |
Called by the run_block() thread upon completion of the task.
This will send any output generated by printing directly to stdout or stderr from Python (so, output not generated by using the 'display' function). Indicates to the client that this block has finished executing. Will send an empty string if there has been no output 'print'ed.
|
protected |
|
protectedvirtual |
|
protected |
|
protected |
void Server::run | ( | int | port = 0 , |
bool | exit_on_disconnect = true |
||
) |
The only user-visible part: just instantiate a server object and start it with run().
This will not return until the server has been shut down. Picks a random port when port==0. If exit_on_disconnect==false
, keep the server alive on the same porta and with the same authentication token when the connection drops (so you can reconnect).
|
protected |
|
virtual |
Raw code to send a string (which must be JSON formatted) as a message to the client.
Handles communication of the result back to the client in JSON format. This is always of the form
{ "header": { "parent_id": "...", "parent_origin": "client" | "server", "cell_id": "...", "cell_origin": "client" | "server" }, "content": { "output": "...", "width": int (optional), "height": int (optional)
}, "msg_type": "..." }
msg_type can be "output", "latex", "image_png" and so on, corresponding to the possible values of DataCell::CellType.
Returns the serial number of the new cell sent.
void Server::send_json | ( | const std::string & | msg | ) |
void Server::send_progress_update | ( | const std::string & | msg, |
int | n, | ||
int | total | ||
) |
|
protected |
Halt the currently running block and prevent execution of any further blocks that may still be on the queue.
void Server::wait_for_job | ( | ) |
Thread entry point for the code that waits for blocks to appear on the block queue, and executes them in turn.
In practice we run this on the main thread.
void Server::wait_for_websocket | ( | ) |
Thread entry point for code that sets up and runs the websocket listener.
|
protected |
|
protected |
|
protected |
|
protected |
CatchOutput Server::catchErr |
CatchOutput Server::catchOut |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
Stopwatch Server::server_stopwatch |
|
protected |
Stopwatch Server::sympy_stopwatch |
|
protected |
|
protected |