Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
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 <set>
11#include <glibmm/spawn.h>
12
13typedef websocketpp::client<websocketpp::config::asio_client> WSClient;
14typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
15typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
16
17#include "DataCell.hh"
18
19namespace cadabra {
20
21 class GUIBase;
22 class DocumentThread;
23
35
37 public:
40
41 ComputeThread(int server_port=0, std::string token="", std::string ip_address="127.0.0.1");
42 ComputeThread(const ComputeThread& )=delete; // You cannot copy this object
44
48
53
54 void run();
55
66
67 void execute_cell(DTree::iterator);
68
69 void execute_interactive(uint64_t id, const std::string& code);
70
73
74 void stop();
75
77
78 void restart_kernel();
79
85
86 bool complete(DTree::iterator, int pos, int alternative);
87
88 // Determine if there are still cells running on the server.
89 // FIXME: this does not guarantee thread-safety but at the moment
90 // is only used for updating status bars etc.
91 // FIXME: can be moved to DocumentThread.
92
93 int number_of_cells_executing(void) const;
94
97
98 void terminate();
99
101 bool kernel_is_connected() const;
102
103 private:
106
107 // For debugging purposes, we keep record of the gui thread id,
108 // so that we can flag when code runs on the wrong thread.
109 // Gets initialised in the ComputeThread constructor.
110 std::thread::id gui_thread_id;
111
112 // Keeping track of cells which are running on the server, in
113 // a form which allows us to look them up quickly based only
114 // on the id (which is all that the server knows about).
115
116 // FIXME: moving this away into documentthread, so that we only need to refer to id's.
117 std::map<DataCell::id_t, DTree::iterator> running_cells;
118
119 // WebSocket++ things.
122 WSClient::connection_ptr connection;
123 websocketpp::connection_hdl our_connection_hdl;
124 void init();
125 void try_connect();
126 void try_spawn_server();
127 void on_open(websocketpp::connection_hdl hdl);
128 void on_fail(websocketpp::connection_hdl hdl);
129 void on_close(websocketpp::connection_hdl hdl);
130 void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
131
133
137
138 std::set<uint64_t> interactive_cells;
139
140 // Self-started server
141 Glib::Pid server_pid;
143 unsigned short port;
148 };
149
150 }
websocketpp::lib::lock_guard< websocketpp::lib::mutex > scoped_lock
Definition ComputeThread.hh:15
websocketpp::config::asio_client::message_type::ptr message_ptr
Definition ComputeThread.hh:14
websocketpp::client< websocketpp::config::asio_client > WSClient
Definition ComputeThread.hh:13
Base class which talks to the server and sends Action objects back to the DocumentThread.
Definition ComputeThread.hh:36
void execute_cell(DTree::iterator)
In order to execute code on the server, call the following from the GUI thread.
Definition ComputeThread.cc:505
ComputeThread(int server_port=0, std::string token="", std::string ip_address="127.0.0.1")
If the ComputeThread is constructed with a null pointer to the gui, there will be no gui updates,...
Definition ComputeThread.cc:19
void init()
Definition ComputeThread.cc:49
void try_connect()
Definition ComputeThread.cc:57
void stop()
Stop the current cell execution on the server and remove all other cells from the run queue as well.
Definition ComputeThread.cc:583
~ComputeThread()
Definition ComputeThread.cc:31
WSClient::connection_ptr connection
Definition ComputeThread.hh:122
std::thread::id gui_thread_id
Definition ComputeThread.hh:110
std::string authentication_token
Definition ComputeThread.hh:144
std::string forced_server_ip_address
Definition ComputeThread.hh:147
void run()
Main entry point, which will connect to the server and then start an event loop to handle communicati...
Definition ComputeThread.cc:96
Glib::Pid server_pid
Definition ComputeThread.hh:141
std::string forced_server_token
Definition ComputeThread.hh:146
bool kernel_is_connected() const
Return the status of the connection to the kernel.
Definition ComputeThread.cc:133
void restart_kernel()
Restart the kernel.
Definition ComputeThread.cc:604
ComputeThread(const ComputeThread &)=delete
bool connection_is_open
Definition ComputeThread.hh:121
int server_stdout
Definition ComputeThread.hh:142
bool restarting_kernel
Definition ComputeThread.hh:121
int forced_server_port
Definition ComputeThread.hh:145
void terminate()
Terminate the compute thread, in preparation for shutting down the client altogether.
Definition ComputeThread.cc:108
void on_close(websocketpp::connection_hdl hdl)
Definition ComputeThread.cc:264
void on_open(websocketpp::connection_hdl hdl)
Definition ComputeThread.cc:235
websocketpp::connection_hdl our_connection_hdl
Definition ComputeThread.hh:123
void on_message(websocketpp::connection_hdl hdl, message_ptr msg)
Definition ComputeThread.cc:294
void cell_finished_running(DataCell::id_t)
Definition ComputeThread.cc:283
void try_spawn_server()
Definition ComputeThread.cc:174
GUIBase * gui
Definition ComputeThread.hh:104
bool complete(DTree::iterator, int pos, int alternative)
Request completion of a string.
Definition ComputeThread.cc:633
std::map< DataCell::id_t, DTree::iterator > running_cells
Definition ComputeThread.hh:117
WSClient wsclient
Definition ComputeThread.hh:120
DocumentThread * docthread
Definition ComputeThread.hh:105
std::set< uint64_t > interactive_cells
Definition ComputeThread.hh:138
int number_of_cells_executing(void) const
Definition ComputeThread.cc:578
void all_cells_nonrunning()
Set all cells to be non-running (e.g.
Definition ComputeThread.cc:138
void set_master(GUIBase *, DocumentThread *)
Determine the objects that this compute thread should be talking to.
Definition ComputeThread.cc:43
void execute_interactive(uint64_t id, const std::string &code)
Definition ComputeThread.cc:478
int server_stderr
Definition ComputeThread.hh:142
void on_fail(websocketpp::connection_hdl hdl)
Definition ComputeThread.cc:152
unsigned short port
Definition ComputeThread.hh:143
Each cell is identified by a serial number 'id' which is used to keep track of it across network call...
Definition DataCell.hh:52
A base class with all the logic to manipulate a Cadabra notebook document.
Definition DocumentThread.hh:40
Abstract base class with methods that need to be implemented by any GUI.
Definition GUIBase.hh:16
Functions to handle the exchange properties of two or more symbols in a product.
Definition Adjform.cc:83