From e3a522438925f0cffad4e8573b4ce12851c5c41e Mon Sep 17 00:00:00 2001 From: veclav talica Date: Sun, 27 Aug 2023 11:46:51 +0500 Subject: [PATCH] proper feature discovery, restructured Connections statics, added consts for queries --- scenes/App.gd | 15 +++++++++------ scenes/Connections.gd | 27 +++++++++++++++------------ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/scenes/App.gd b/scenes/App.gd index 836a6ad..5f453d8 100644 --- a/scenes/App.gd +++ b/scenes/App.gd @@ -2,16 +2,19 @@ extends Node var _connection +onready var n_Connection := get_node("Connections") + func _service_discovery(): var iq := yield() as Xml.XmlElement var feature_promises := Array() for item in iq.children[0].children: - feature_promises.push_back(_connection.promise_iq(item.attributes["jid"], "get", - "", - _connection.iq_as_is())) + feature_promises.push_back(_connection.promise_iq( + item.attributes["jid"], "get", + n_Connection.disco_info_queury, + n_Connection.yield_as_is())) - while not _connection.are_promises_done(feature_promises): + while not n_Connection.are_promises_done(feature_promises): yield() for feature_promise in feature_promises: @@ -23,13 +26,13 @@ func _service_discovery(): print(feature.as_string()) func _ready(): - _connection = $Connections.establish_new_connection("poto.cafe", "veclavtalica", "-") + _connection = n_Connection.establish_new_connection("poto.cafe", "veclavtalica", "-") if _connection == null: push_error("Connection failed") return if _connection.push_iq(_connection.domain, "get", - "", + n_Connection.disco_items_queury, _service_discovery()) != OK: push_error("Connection failed") return diff --git a/scenes/Connections.gd b/scenes/Connections.gd index 200ba14..3f32923 100644 --- a/scenes/Connections.gd +++ b/scenes/Connections.gd @@ -1,5 +1,8 @@ extends Node +const disco_info_queury := "" +const disco_items_queury := "" + class Connection extends Reference: var stream: StreamPeer var identity: String @@ -83,19 +86,19 @@ class Connection extends Reference: return OK - static func are_promises_done(promises: Array) -> bool: - for promise in promises: - assert(promise is Promise) - if not promise.is_done: - return false - return true - - static func iq_as_is(): - return yield() - ## Registry of connections used for poking of pending iqs. var _connections: Array # of WeakRef to Connection +static func are_promises_done(promises: Array) -> bool: + for promise in promises: + assert(promise is Connection.Promise) + if not promise.is_done: + return false + return true + +static func yield_as_is(): + return yield() + func _ready(): # todo: Some better interval? if get_tree().connect("physics_frame", self, "_process_connections") != OK: @@ -160,14 +163,14 @@ func _process_connections() -> void: func establish_new_connection(domain: String, identity: String, password: String) -> Connection: var stream := StreamPeerTCP.new() if stream.connect_to_host(domain, 5222) != OK: - push_error("Cannot establish client->server pipe to " + domain) + push_error("Cannot establish connection to " + domain) return null while stream.get_status() == StreamPeerTCP.STATUS_CONNECTING: pass if stream.get_status() == StreamPeerTCP.STATUS_ERROR: - push_error("Cannot establish client->server pipe to " + domain) + push_error("Cannot establish connection to " + domain) return null var result := Connection.new()