Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ComputeThread.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <websocketpp/config/asio_no_tls_client.hpp>
5 #include <websocketpp/client.hpp>
6 #include <websocketpp/common/thread.hpp>
7 #include <websocketpp/common/functional.hpp>
8 #include <thread>
9 #include <glibmm/spawn.h>
10 
11 typedef websocketpp::client<websocketpp::config::asio_client> WSClient;
12 typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
13 typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
14 
15 #include "DataCell.hh"
16 
17 namespace cadabra {
18 
19  class GUIBase;
20  class DocumentThread;
21 
33 
34  class ComputeThread {
35  public:
38 
39  ComputeThread();
40  ComputeThread(const ComputeThread& )=delete; // You cannot copy this object
42 
46 
51 
52  void run();
53 
64 
65  void execute_cell(DTree::iterator);
66 
69 
70  void stop();
71 
73 
74  void restart_kernel();
75 
76  // Determine if there are still cells running on the server.
77  // FIXME: this does not guarantee thread-safety but at the moment
78  // is only used for updating status bars etc.
79  // FIXME: can be moved to DocumentThread.
80 
81  int number_of_cells_executing(void) const;
82 
85 
86  void terminate();
87 
88  private:
91 
92  // For debugging purposes, we keep record of the gui thread id,
93  // so that we can flag when code runs on the wrong thread.
94  // Gets initialised in the ComputeThread constructor.
95  std::thread::id gui_thread_id;
96 
97  // Keeping track of cells which are running on the server, in
98  // a form which allows us to look them up quickly based only
99  // on the id (which is all that the server knows about).
100 
101  // FIXME: moving this away into documentthread, so that we only need to refer to id's.
102  std::map<DataCell::id_t, DTree::iterator> running_cells;
103 
104  // WebSocket++ things.
107  WSClient::connection_ptr connection;
108  websocketpp::connection_hdl our_connection_hdl;
109  void init();
110  void try_connect();
111  void try_spawn_server();
112  void on_open(websocketpp::connection_hdl hdl);
113  void on_fail(websocketpp::connection_hdl hdl);
114  void on_close(websocketpp::connection_hdl hdl);
115  void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
116 
118 
121  void all_cells_nonrunning();
122 
123 
124  // Self-started server
125  Glib::Pid server_pid;
127  unsigned short port;
128  };
129 
130 }
void on_fail(websocketpp::connection_hdl hdl)
Definition: ComputeThread.cc:142
int server_stderr
Definition: ComputeThread.hh:126
int server_stdout
Definition: ComputeThread.hh:126
A base class with all the logic to manipulate a Cadabra notebook document.
Definition: DocumentThread.hh:38
Glib::Pid server_pid
Definition: ComputeThread.hh:125
void on_open(websocketpp::connection_hdl hdl)
Definition: ComputeThread.cc:198
websocketpp::config::asio_client::message_type::ptr message_ptr
Definition: ComputeThread.hh:12
void terminate()
Terminate the compute thread, in preparation for shutting down the client altogether.
Definition: ComputeThread.cc:105
GUIBase * gui
Definition: ComputeThread.hh:89
void try_spawn_server()
Definition: ComputeThread.cc:164
DocumentThread * docthread
Definition: ComputeThread.hh:90
websocketpp::client< websocketpp::config::asio_client > WSClient
Definition: ComputeThread.hh:11
void on_close(websocketpp::connection_hdl hdl)
Definition: ComputeThread.cc:225
void restart_kernel()
Restart the kernel.
Definition: ComputeThread.cc:476
Each cell is identified by a serial number 'id' which is used to keep track of it across network call...
Definition: DataCell.hh:51
void all_cells_nonrunning()
Set all cells to be non-running (e.g.
Definition: ComputeThread.cc:130
void init()
Definition: ComputeThread.cc:46
std::thread::id gui_thread_id
Definition: ComputeThread.hh:95
void cell_finished_running(DataCell::id_t)
Definition: ComputeThread.cc:239
Abstract base class with methods that need to be implemented by any GUI.
Definition: GUIBase.hh:16
websocketpp::lib::lock_guard< websocketpp::lib::mutex > scoped_lock
Definition: ComputeThread.hh:13
void try_connect()
Definition: ComputeThread.cc:54
void on_message(websocketpp::connection_hdl hdl, message_ptr msg)
Definition: ComputeThread.cc:250
void execute_cell(DTree::iterator)
In order to execute code on the server, call the following from the GUI thread.
Definition: ComputeThread.cc:382
void set_master(GUIBase *, DocumentThread *)
Determine the objects that this compute thread should be talking to.
Definition: ComputeThread.cc:40
WSClient wsclient
Definition: ComputeThread.hh:105
void stop()
Stop the current cell execution on the server and remove all other cells from the run queue as well...
Definition: ComputeThread.cc:457
std::map< DataCell::id_t, DTree::iterator > running_cells
Definition: ComputeThread.hh:102
~ComputeThread()
Definition: ComputeThread.cc:28
int number_of_cells_executing(void) const
Definition: ComputeThread.cc:452
bool restarting_kernel
Definition: ComputeThread.hh:106
WSClient::connection_ptr connection
Definition: ComputeThread.hh:107
ComputeThread()
If the ComputeThread is constructed with a null pointer to the gui, there will be no gui updates...
Definition: ComputeThread.cc:17
bool connection_is_open
Definition: ComputeThread.hh:106
unsigned short port
Definition: ComputeThread.hh:127
void run()
Main entry point, which will connect to the server and then start an event loop to handle communicati...
Definition: ComputeThread.cc:93
Base class which talks to the server and sends Action objects back to the DocumentThread.
Definition: ComputeThread.hh:34
websocketpp::connection_hdl our_connection_hdl
Definition: ComputeThread.hh:108