proper feature discovery, restructured Connections statics, added consts for queries

This commit is contained in:
veclav talica 2023-08-27 11:46:51 +05:00
parent 2d970c5914
commit e3a5224389
2 changed files with 24 additions and 18 deletions

View File

@ -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",
"<query xmlns='http://jabber.org/protocol/disco#items'/>",
_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",
"<query xmlns='http://jabber.org/protocol/disco#items'/>",
n_Connection.disco_items_queury,
_service_discovery()) != OK:
push_error("Connection failed")
return

View File

@ -1,5 +1,8 @@
extends Node
const disco_info_queury := "<query xmlns='http://jabber.org/protocol/disco#info'/>"
const disco_items_queury := "<query xmlns='http://jabber.org/protocol/disco#items'/>"
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()