Cadabra
Computer algebra system for field theory problems
Classes | Public Member Functions | Public Attributes | Protected Types | Protected Member Functions | Protected 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:
cadabra::CadabraJupyter

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(). More...
 
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. More...
 
void send_json (const std::string &)
 
bool handles (const std::string &otype) const
 
std::string architecture () const
 
void wait_for_job ()
 Start a thread which waits for blocks to appear on the block queue, and executes them in turn. More...
 

Public Attributes

CatchOutput catchOut
 
CatchOutput catchErr
 
Stopwatch server_stopwatch
 
Stopwatch sympy_stopwatch
 

Protected Types

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

Protected 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)
 
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. More...
 
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. 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...
 

Protected Attributes

WebsocketServer wserver
 
std::string socket_name
 
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
 
std::queue< Blockblock_queue
 
websocketpp::connection_hdl current_hdl
 
uint64_t current_id
 
bool started
 
std::future< std::string > job
 
pybind11::scoped_interpreter guard
 
pybind11::module main_module
 
pybind11::object main_namespace
 

Member Typedef Documentation

◆ ConnectionMap

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

◆ WebsocketServer

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

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 ( )
virtual

Member Function Documentation

◆ architecture()

std::string Server::architecture ( ) const

◆ dispatch_message()

void Server::dispatch_message ( websocketpp::connection_hdl  hdl,
const std::string &  json_string 
)
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

◆ handles()

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

◆ init()

void Server::init ( )
protected

◆ on_block_error()

void Server::on_block_error ( Block  blk)
protectedvirtual

Reimplemented in cadabra::CadabraJupyter.

◆ on_block_finished()

void Server::on_block_finished ( Block  blk)
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.

◆ on_close()

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

◆ on_kernel_fault()

void Server::on_kernel_fault ( Block  blk)
protectedvirtual

◆ on_message()

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

◆ on_open()

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

◆ on_socket_init()

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

◆ run()

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).

◆ run_string()

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

◆ send()

uint64_t Server::send ( const std::string &  output,
const std::string &  msg_type,
uint64_t  parent_id = 0,
bool  last_in_sequence = false 
)
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":  "..." },
   "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.

Reimplemented in cadabra::CadabraJupyter.

◆ send_json()

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

◆ stop_block()

void Server::stop_block ( )
protected

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 ( )

Start a thread which waits for blocks to appear on the block queue, and executes them in turn.

Member Data Documentation

◆ authentication_token

std::string Server::authentication_token
protected

◆ block_available

std::condition_variable Server::block_available
protected

◆ block_available_mutex

std::mutex Server::block_available_mutex
protected

◆ block_queue

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

◆ catchErr

CatchOutput Server::catchErr

◆ catchOut

CatchOutput Server::catchOut

◆ connections

ConnectionMap Server::connections
protected

◆ current_hdl

websocketpp::connection_hdl Server::current_hdl
protected

◆ current_id

uint64_t Server::current_id
protected

◆ exit_on_disconnect

bool Server::exit_on_disconnect
protected

◆ guard

pybind11::scoped_interpreter Server::guard
protected

◆ job

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

◆ main_module

pybind11::module Server::main_module
protected

◆ main_namespace

pybind11::object Server::main_namespace
protected

◆ runner

std::thread Server::runner
protected

◆ server_stopwatch

Stopwatch Server::server_stopwatch

◆ socket_name

std::string Server::socket_name
protected

◆ started

bool Server::started
protected

◆ sympy_stopwatch

Stopwatch Server::sympy_stopwatch

◆ ws_mutex

std::mutex Server::ws_mutex
protected

◆ wserver

WebsocketServer Server::wserver
protected

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