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 var _connection
onready var n_Connection := get_node("Connections")
func _service_discovery(): func _service_discovery():
var iq := yield() as Xml.XmlElement var iq := yield() as Xml.XmlElement
var feature_promises := Array() var feature_promises := Array()
for item in iq.children[0].children: for item in iq.children[0].children:
feature_promises.push_back(_connection.promise_iq(item.attributes["jid"], "get", feature_promises.push_back(_connection.promise_iq(
"<query xmlns='http://jabber.org/protocol/disco#items'/>", item.attributes["jid"], "get",
_connection.iq_as_is())) 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() yield()
for feature_promise in feature_promises: for feature_promise in feature_promises:
@ -23,13 +26,13 @@ func _service_discovery():
print(feature.as_string()) print(feature.as_string())
func _ready(): func _ready():
_connection = $Connections.establish_new_connection("poto.cafe", "veclavtalica", "-") _connection = n_Connection.establish_new_connection("poto.cafe", "veclavtalica", "-")
if _connection == null: if _connection == null:
push_error("Connection failed") push_error("Connection failed")
return return
if _connection.push_iq(_connection.domain, "get", if _connection.push_iq(_connection.domain, "get",
"<query xmlns='http://jabber.org/protocol/disco#items'/>", n_Connection.disco_items_queury,
_service_discovery()) != OK: _service_discovery()) != OK:
push_error("Connection failed") push_error("Connection failed")
return return

View File

@ -1,5 +1,8 @@
extends Node 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: class Connection extends Reference:
var stream: StreamPeer var stream: StreamPeer
var identity: String var identity: String
@ -83,19 +86,19 @@ class Connection extends Reference:
return OK 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. ## Registry of connections used for poking of pending iqs.
var _connections: Array # of WeakRef to Connection 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(): func _ready():
# todo: Some better interval? # todo: Some better interval?
if get_tree().connect("physics_frame", self, "_process_connections") != OK: 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: func establish_new_connection(domain: String, identity: String, password: String) -> Connection:
var stream := StreamPeerTCP.new() var stream := StreamPeerTCP.new()
if stream.connect_to_host(domain, 5222) != OK: 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 return null
while stream.get_status() == StreamPeerTCP.STATUS_CONNECTING: while stream.get_status() == StreamPeerTCP.STATUS_CONNECTING:
pass pass
if stream.get_status() == StreamPeerTCP.STATUS_ERROR: 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 return null
var result := Connection.new() var result := Connection.new()