Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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>

Inheritance diagram for Server:
ProgressMonitor

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...
 
- Public Member Functions inherited from ProgressMonitor
 ProgressMonitor ()
 
virtual ~ProgressMonitor ()
 
virtual void group (std::string name="")
 
virtual void progress (int n, int total)
 
void print () const
 
std::vector< Totaltotals () 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)
 
std::string architecture () const
 
bool handles (const std::string &otype) const
 
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 &)
 
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
 
boost::python::object main_module
 
boost::python::object main_namespace
 
int cells_ran
 

Member Typedef Documentation

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

Constructor & Destructor Documentation

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

Member Function Documentation

std::string Server::architecture ( ) const
private
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

bool Server::handles ( const std::string &  otype) const
private
void Server::init ( )
private
void Server::on_block_error ( Block  blk)
private
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.

void Server::on_close ( websocketpp::connection_hdl  hdl)
private
void Server::on_kernel_fault ( Block  blk)
private
void Server::on_message ( websocketpp::connection_hdl  hdl,
WebsocketServer::message_ptr  msg 
)
private
void Server::on_open ( websocketpp::connection_hdl  hdl)
private
void Server::on_socket_init ( websocketpp::connection_hdl  hdl,
boost::asio::ip::tcp::socket &  s 
)
private
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.

std::string Server::run_string ( const std::string &  blk,
bool  handle_output = true 
)
private
uint64_t Server::send ( const std::string &  output,
const std::string &  msg_type,
uint64_t  parent_id = 0,
bool  last_in_sequence = false 
)
private

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.

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

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

void Server::wait_for_job ( )
private

Member Data Documentation

std::condition_variable Server::block_available
private
std::mutex Server::block_available_mutex
private
std::queue<Block> Server::block_queue
private
CatchOutput Server::catchErr
CatchOutput Server::catchOut
int Server::cells_ran
private
ConnectionMap Server::connections
private
websocketpp::connection_hdl Server::current_hdl
private
uint64_t Server::current_id
private
std::future<std::string> Server::job
private
boost::python::object Server::main_module
private
boost::python::object Server::main_namespace
private
uint64_t Server::return_cell_id
private
std::thread Server::runner
private
Stopwatch Server::server_stopwatch
std::string Server::socket_name
private
bool Server::started
private
Stopwatch Server::sympy_stopwatch
std::mutex Server::ws_mutex
private
WebsocketServer Server::wserver
private

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