Cleaned up WebServer.

This commit is contained in:
Relintai 2022-06-26 21:34:29 +02:00
parent 47ee34044a
commit 633acfcd54
4 changed files with 86 additions and 30 deletions

View File

@ -16,6 +16,24 @@
#include "database/table_builder.h" #include "database/table_builder.h"
#endif #endif
/*
void WebNode::update() {
if (!_root_node) {
return;
}
_root_node->notification(Node::NOTIFICATION_UPDATE);
if (_write_lock_requested) {
_rw_lock.write_lock();
_root_node->notification(Node::NOTIFICATION_TREE_WRITE_LOCKED);
_rw_lock.write_unlock();
_write_lock_requested = false;
}
}
*/
String WebNode::get_uri_segment() { String WebNode::get_uri_segment() {
return _uri_segment; return _uri_segment;
} }
@ -342,4 +360,4 @@ WebNode::WebNode() :
} }
WebNode::~WebNode() { WebNode::~WebNode() {
} }

View File

@ -17,10 +17,17 @@ class TableBuilder;
class QueryBuilder; class QueryBuilder;
#endif #endif
//note add an alterative queue_delete() -> (with different name) -> if called node stops getting routed to, and when rwlock can be locked gets queue_deleted.
class WebNode : public Node { class WebNode : public Node {
RCPP_OBJECT(WebNode, Node); RCPP_OBJECT(WebNode, Node);
public: public:
enum {
//todo likely needs a different constant number
NOTIFICATION_SERVER_TREE_WRITE_LOCKED = 2000,
};
String get_uri_segment(); String get_uri_segment();
void set_uri_segment(const String &val); void set_uri_segment(const String &val);
@ -49,7 +56,7 @@ public:
virtual void render_index(Request *request); virtual void render_index(Request *request);
virtual void render_preview(Request *request); virtual void render_preview(Request *request);
virtual void render_menu(Request *request); virtual void render_menu(Request *request);
virtual void _render_menu(Request *request); virtual void _render_menu(Request *request);
@ -93,4 +100,4 @@ protected:
Ref<WebPermission> _web_permission; Ref<WebPermission> _web_permission;
}; };
#endif #endif

View File

@ -1,23 +1,33 @@
#include "web_server.h" #include "web_server.h"
#include "request.h" #include "web_node.h"
#include "web/http/web_node.h" #include "web_server_request.h"
NodePath WebServer::get_web_root_path() const {
return _web_root_path;
}
void WebServer::set_web_root_path(const NodePath &path) {
_web_root_path = path;
}
WebNode *WebServer::get_web_root() { WebNode *WebServer::get_web_root() {
return _web_root; return _web_root;
} }
void WebServer::set_root(Node *root) { void WebServer::set_web_root(WebNode *root) {
WebNode *web_root = Object::cast_to<WebNode>(root); _web_root = root;
ERR_FAIL_COND(!web_root);
_web_root = web_root;
NodeTree::set_root(root);
} }
void WebServer::handle_request(Request *request) { Node *WebServer::get_web_root_bind() {
return _web_root;
}
void WebServer::set_web_root_bind(Node *root) {
WebNode *web_root = Object::cast_to<WebNode>(root);
_web_root = web_root;
}
void WebServer::handle_request(Ref<WebServerRequest> request) {
ERR_FAIL_COND(!_web_root); ERR_FAIL_COND(!_web_root);
_rw_lock.read_lock(); _rw_lock.read_lock();
@ -25,14 +35,24 @@ void WebServer::handle_request(Request *request) {
_rw_lock.read_unlock(); _rw_lock.read_unlock();
} }
float WebServer::get_update_delta_time() { WebServer::WebServer() {
return _update_interval;
}
WebServer::WebServer() : NodeTree() {
_web_root = nullptr; _web_root = nullptr;
_update_interval = 0.5;
_write_lock_requested = false;
} }
WebServer::~WebServer() { WebServer::~WebServer() {
} }
void WebServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_web_root_path"), &WebServer::get_web_root_path);
ClassDB::bind_method(D_METHOD("set_web_root_path", "val"), &WebServer::set_web_root_path);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "web_root_path"), "set_web_root_path", "get_web_root_path");
ClassDB::bind_method(D_METHOD("get_web_root_bind"), &WebServer::get_web_root_bind);
ClassDB::bind_method(D_METHOD("set_web_root_bind", "val"), &WebServer::set_web_root_bind);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "web_root", PROPERTY_HINT_RESOURCE_TYPE, "Node"), "set_web_root_bind", "get_web_root_bind");
ClassDB::bind_method(D_METHOD("handle_request", "request"), &WebServer::handle_request);
ClassDB::bind_method(D_METHOD("request_write_lock"), &WebServer::request_write_lock);
}

View File

@ -1,29 +1,40 @@
#ifndef WEB_SERVER_H #ifndef WEB_SERVER_H
#define WEB_SERVER_H #define WEB_SERVER_H
#include "core/nodes/node_tree.h" #include "core/os/rw_lock.h"
#include "scene/main/node.h"
class Request; class WebServerRequest;
class WebNode; class WebNode;
class WebServer : public NodeTree { class WebServer : public Node {
RCPP_OBJECT(WebServer, NodeTree); GDCLASS(WebServer, Node);
public: public:
NodePath get_web_root_path() const;
void set_web_root_path(const NodePath &path);
WebNode *get_web_root(); WebNode *get_web_root();
void set_web_root(WebNode *root);
void set_root(Node *root); Node *get_web_root_bind();
void set_web_root_bind(Node *root);
void handle_request(Request *request); void handle_request(Ref<WebServerRequest> request);
float get_update_delta_time(); void request_write_lock();
WebServer(); WebServer();
virtual ~WebServer(); ~WebServer();
protected: protected:
static void _bind_methods();
NodePath _web_root_path;
WebNode *_web_root; WebNode *_web_root;
float _update_interval;
bool _write_lock_requested;
RWLock _rw_lock;
}; };
#endif #endif