Cadabra
Computer algebra system for field theory problems
Classes | Public Member Functions | Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
Server Class Reference

Description

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)
 
 ~Server ()
 
void run ()
 The only user-visible part: just instantiate a server object and start it with run(). More...
 
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. More...
 
void send_json (const std::string &)
 
bool handles (const std::string &otype) const
 
std::string architecture () const
 

Public Attributes

CatchOutput catchOut
 
CatchOutput catchErr
 
Stopwatch server_stopwatch
 
Stopwatch sympy_stopwatch
 

Private Types

typedef websocketpp::server< websocketpp::config::asio > WebsocketServer
 
typedef std::map< websocketpp::connection_hdl, Connection, std::owner_less< websocketpp::connection_hdl > > ConnectionMap
 

Private Member Functions

void init ()
 
void on_socket_init (websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket &s)
 
void on_message (websocketpp::connection_hdl hdl, WebsocketServer::message_ptr msg)
 
void on_open (websocketpp::connection_hdl hdl)
 
void on_close (websocketpp::connection_hdl hdl)
 
void wait_for_job ()
 
std::string run_string (const std::string &, bool handle_output=true)
 
void on_block_finished (Block)
 Called by the run_block() thread upon completion of the task. More...
 
void on_block_error (Block)
 
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. More...
 
void dispatch_message (websocketpp::connection_hdl, const std::string &json_string)
 Takes a JSON encoded message and performs the required action to process it. More...
 

Private Attributes

WebsocketServer wserver
 
std::string socket_name
 
ConnectionMap connections
 
std::mutex ws_mutex
 
std::thread runner
 
std::mutex block_available_mutex
 
std::condition_variable block_available
 
std::queue< Blockblock_queue
 
websocketpp::connection_hdl current_hdl
 
uint64_t current_id
 
uint64_t return_cell_id
 
bool started
 
std::future< std::string > job
 
pybind11::scoped_interpreter guard
 
pybind11::module main_module
 
pybind11::object main_namespace
 
int cells_ran
 

Member Typedef Documentation

◆ ConnectionMap

typedef std::map<websocketpp::connection_hdl, Connection, std::owner_less<websocketpp::connection_hdl> > Server::ConnectionMap
private

◆ WebsocketServer

typedef websocketpp::server<websocketpp::config::asio> Server::WebsocketServer
private

Constructor & Destructor Documentation

◆ Server() [1/3]

Server::Server ( )

◆ Server() [2/3]

Server::Server ( const Server )
delete

◆ Server() [3/3]

Server::Server ( const std::string &  socket)

◆ ~Server()

Server::~Server ( )

Member Function Documentation

◆ architecture()

std::string Server::architecture ( ) const

◆ dispatch_message()

void Server::dispatch_message ( websocketpp::connection_hdl  hdl,
const std::string &  json_string 
)
private

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

◆ handles()

bool Server::handles ( const std::string &  otype) const

◆ init()

void Server::init ( )
private

◆ on_block_error()

void Server::on_block_error ( Block  blk)
private

◆ on_block_finished()

void Server::on_block_finished ( Block  blk)
private

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.

◆ on_close()

void Server::on_close ( websocketpp::connection_hdl  hdl)
private

◆ on_kernel_fault()

void Server::on_kernel_fault ( Block  blk)
private

◆ on_message()

void Server::on_message ( websocketpp::connection_hdl  hdl,
WebsocketServer::message_ptr  msg 
)
private

◆ on_open()

void Server::on_open ( websocketpp::connection_hdl  hdl)
private

◆ on_socket_init()

void Server::on_socket_init ( websocketpp::connection_hdl  hdl,
boost::asio::ip::tcp::socket &  s 
)
private

◆ run()

void Server::run ( )

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.

◆ run_string()

std::string Server::run_string ( const std::string &  blk,
bool  handle_output = true 
)
private

◆ send()

uint64_t Server::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.

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": "..." }, "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.

◆ send_json()

void Server::send_json ( const std::string &  msg)

◆ stop_block()

void Server::stop_block ( )
private

Halt the currently running block and prevent execution of any further blocks that may still be on the queue.

◆ wait_for_job()

void Server::wait_for_job ( )
private

Member Data Documentation

◆ block_available

std::condition_variable Server::block_available
private

◆ block_available_mutex

std::mutex Server::block_available_mutex
private

◆ block_queue

std::queue<Block> Server::block_queue
private

◆ catchErr

CatchOutput Server::catchErr

◆ catchOut

CatchOutput Server::catchOut

◆ cells_ran

int Server::cells_ran
private

◆ connections

ConnectionMap Server::connections
private

◆ current_hdl

websocketpp::connection_hdl Server::current_hdl
private

◆ current_id

uint64_t Server::current_id
private

◆ guard

pybind11::scoped_interpreter Server::guard
private

◆ job

std::future<std::string> Server::job
private

◆ main_module

pybind11::module Server::main_module
private

◆ main_namespace

pybind11::object Server::main_namespace
private

◆ return_cell_id

uint64_t Server::return_cell_id
private

◆ runner

std::thread Server::runner
private

◆ server_stopwatch

Stopwatch Server::server_stopwatch

◆ socket_name

std::string Server::socket_name
private

◆ started

bool Server::started
private

◆ sympy_stopwatch

Stopwatch Server::sympy_stopwatch

◆ ws_mutex

std::mutex Server::ws_mutex
private

◆ wserver

WebsocketServer Server::wserver
private

The documentation for this class was generated from the following files: