Cadabra
Computer algebra system for field theory problems
ComputeThread.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <signal.h>
5 #include <websocketpp/config/asio_no_tls_client.hpp>
6 #include <websocketpp/client.hpp>
7 #include <websocketpp/common/thread.hpp>
8 #include <websocketpp/common/functional.hpp>
9 #include <thread>
10 #include <glibmm/spawn.h>
11 
12 typedef websocketpp::client<websocketpp::config::asio_client> WSClient;
13 typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
14 typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
15 
16 #include "DataCell.hh"
17 
18 namespace cadabra {
19 
20  class GUIBase;
21  class DocumentThread;
22 
34 
35  class ComputeThread {
36  public:
39 
40  ComputeThread(int server_port=0, std::string token="");
41  ComputeThread(const ComputeThread& )=delete; // You cannot copy this object
43 
47 
52 
53  void run();
54 
65 
66  void execute_cell(DTree::iterator);
67 
68  void execute_interactive(const std::string& code);
69 
70  void register_interactive_cell(uint64_t id);
71 
74 
75  void stop();
76 
78 
79  void restart_kernel();
80 
81  // Determine if there are still cells running on the server.
82  // FIXME: this does not guarantee thread-safety but at the moment
83  // is only used for updating status bars etc.
84  // FIXME: can be moved to DocumentThread.
85 
86  int number_of_cells_executing(void) const;
87 
90 
91  void terminate();
92 
93  private:
96 
97  // For debugging purposes, we keep record of the gui thread id,
98  // so that we can flag when code runs on the wrong thread.
99  // Gets initialised in the ComputeThread constructor.
100  std::thread::id gui_thread_id;
101 
102  // Keeping track of cells which are running on the server, in
103  // a form which allows us to look them up quickly based only
104  // on the id (which is all that the server knows about).
105 
106  // FIXME: moving this away into documentthread, so that we only need to refer to id's.
107  std::map<DataCell::id_t, DTree::iterator> running_cells;
108 
109  // WebSocket++ things.
112  WSClient::connection_ptr connection;
113  websocketpp::connection_hdl our_connection_hdl;
114  void init();
115  void try_connect();
116  void try_spawn_server();
117  void on_open(websocketpp::connection_hdl hdl);
118  void on_fail(websocketpp::connection_hdl hdl);
119  void on_close(websocketpp::connection_hdl hdl);
120  void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
121 
123 
126  void all_cells_nonrunning();
127 
129  std::vector<uint64_t> console_child_ids;
130 
131  // Self-started server
132  Glib::Pid server_pid;
134  unsigned short port;
135  std::string authentication_token;
137  std::string forced_server_token;
138  };
139 
140  }
cadabra::ComputeThread::interactive_cell
uint64_t interactive_cell
Definition: ComputeThread.hh:128
message_ptr
websocketpp::config::asio_client::message_type::ptr message_ptr
Definition: ComputeThread.hh:13
cadabra::ComputeThread::all_cells_nonrunning
void all_cells_nonrunning()
Set all cells to be non-running (e.g.
Definition: ComputeThread.cc:132
cadabra::ComputeThread::connection_is_open
bool connection_is_open
Definition: ComputeThread.hh:111
cadabra::ComputeThread::on_open
void on_open(websocketpp::connection_hdl hdl)
Definition: ComputeThread.cc:221
cadabra::ComputeThread::cell_finished_running
void cell_finished_running(DataCell::id_t)
Definition: ComputeThread.cc:262
cadabra::ComputeThread::server_pid
Glib::Pid server_pid
Definition: ComputeThread.hh:132
cadabra::ComputeThread
Definition: ComputeThread.hh:35
cadabra::ComputeThread::our_connection_hdl
websocketpp::connection_hdl our_connection_hdl
Definition: ComputeThread.hh:113
cadabra::ComputeThread::~ComputeThread
~ComputeThread()
Definition: ComputeThread.cc:30
cadabra::ComputeThread::try_spawn_server
void try_spawn_server()
Definition: ComputeThread.cc:168
cadabra::ComputeThread::wsclient
WSClient wsclient
Definition: ComputeThread.hh:110
cadabra::ComputeThread::try_connect
void try_connect()
Definition: ComputeThread.cc:56
cadabra::GUIBase
Definition: GUIBase.hh:16
cadabra::ComputeThread::terminate
void terminate()
Terminate the compute thread, in preparation for shutting down the client altogether.
Definition: ComputeThread.cc:107
cadabra::ComputeThread::stop
void stop()
Stop the current cell execution on the server and remove all other cells from the run queue as well.
Definition: ComputeThread.cc:525
cadabra::ComputeThread::init
void init()
Definition: ComputeThread.cc:48
cadabra::ComputeThread::forced_server_port
int forced_server_port
Definition: ComputeThread.hh:136
cadabra::ComputeThread::port
unsigned short port
Definition: ComputeThread.hh:134
cadabra::ComputeThread::restarting_kernel
bool restarting_kernel
Definition: ComputeThread.hh:111
cadabra::ComputeThread::server_stdout
int server_stdout
Definition: ComputeThread.hh:133
cadabra::ComputeThread::run
void run()
Main entry point, which will connect to the server and then start an event loop to handle communicati...
Definition: ComputeThread.cc:95
cadabra::ComputeThread::running_cells
std::map< DataCell::id_t, DTree::iterator > running_cells
Definition: ComputeThread.hh:107
cadabra::ComputeThread::on_message
void on_message(websocketpp::connection_hdl hdl, message_ptr msg)
Definition: ComputeThread.cc:273
cadabra::ComputeThread::console_child_ids
std::vector< uint64_t > console_child_ids
Definition: ComputeThread.hh:129
DataCell.hh
cadabra::ComputeThread::restart_kernel
void restart_kernel()
Restart the kernel.
Definition: ComputeThread.cc:545
cadabra::DocumentThread
Definition: DocumentThread.hh:39
cadabra::ComputeThread::execute_interactive
void execute_interactive(const std::string &code)
Definition: ComputeThread.cc:416
cadabra::ComputeThread::forced_server_token
std::string forced_server_token
Definition: ComputeThread.hh:137
cadabra::ComputeThread::connection
WSClient::connection_ptr connection
Definition: ComputeThread.hh:112
cadabra::ComputeThread::number_of_cells_executing
int number_of_cells_executing(void) const
Definition: ComputeThread.cc:520
cadabra::ComputeThread::set_master
void set_master(GUIBase *, DocumentThread *)
Determine the objects that this compute thread should be talking to.
Definition: ComputeThread.cc:42
cadabra
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:80
WSClient
websocketpp::client< websocketpp::config::asio_client > WSClient
Definition: ComputeThread.hh:12
cadabra::ComputeThread::ComputeThread
ComputeThread(int server_port=0, std::string token="")
If the ComputeThread is constructed with a null pointer to the gui, there will be no gui updates,...
Definition: ComputeThread.cc:19
cadabra::ComputeThread::on_fail
void on_fail(websocketpp::connection_hdl hdl)
Definition: ComputeThread.cc:146
cadabra::ComputeThread::docthread
DocumentThread * docthread
Definition: ComputeThread.hh:95
cadabra::DataCell::id_t
Each cell is identified by a serial number 'id' which is used to keep track of it across network call...
Definition: DataCell.hh:51
cadabra::ComputeThread::execute_cell
void execute_cell(DTree::iterator)
In order to execute code on the server, call the following from the GUI thread.
Definition: ComputeThread.cc:447
cadabra::ComputeThread::authentication_token
std::string authentication_token
Definition: ComputeThread.hh:135
cadabra::ComputeThread::register_interactive_cell
void register_interactive_cell(uint64_t id)
Definition: ComputeThread.cc:442
cadabra::ComputeThread::on_close
void on_close(websocketpp::connection_hdl hdl)
Definition: ComputeThread.cc:248
cadabra::ComputeThread::server_stderr
int server_stderr
Definition: ComputeThread.hh:133
cadabra::ComputeThread::gui
GUIBase * gui
Definition: ComputeThread.hh:94
scoped_lock
websocketpp::lib::lock_guard< websocketpp::lib::mutex > scoped_lock
Definition: ComputeThread.hh:14
cadabra::ComputeThread::gui_thread_id
std::thread::id gui_thread_id
Definition: ComputeThread.hh:100