diff options
author | fiaxh <git@lightrise.org> | 2020-07-03 21:14:39 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2020-07-16 23:31:19 +0200 |
commit | e159fd2492c28c1ef4ab64828ca0e8c2de877b41 (patch) | |
tree | aca3c42854b08b5683a1c85e7c7a74fd0a4bdf0d /xmpp-vala/src/module/xep | |
parent | 74f7fa897f9aec298eeadcfc7a7b971f06498858 (diff) | |
download | dino-e159fd2492c28c1ef4ab64828ca0e8c2de877b41.tar.gz dino-e159fd2492c28c1ef4ab64828ca0e8c2de877b41.zip |
Only query entity caps when we need them
Diffstat (limited to 'xmpp-vala/src/module/xep')
8 files changed, 47 insertions, 118 deletions
diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala b/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala index 7716ceff..ab311727 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/flag.vala @@ -5,8 +5,6 @@ namespace Xmpp.Xep.ServiceDiscovery { public class Flag : XmppStreamFlag { public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "service_discovery"); - public HashMap<Jid, Gee.List<string>?> entity_features = new HashMap<Jid, Gee.List<string>?>(Jid.hash_func, Jid.equals_func); - private HashMap<Jid, Gee.Set<Identity>?> entity_identities = new HashMap<Jid, Gee.Set<Identity>?>(Jid.hash_func, Jid.equals_func); private HashMap<Jid, Gee.List<Item>?> entity_items = new HashMap<Jid, Gee.List<Item>?>(Jid.hash_func, Jid.equals_func); private Gee.Set<string> own_features_ = new HashSet<string>(); @@ -23,33 +21,6 @@ public class Flag : XmppStreamFlag { owned get { return own_identities_.read_only_view; } } - public Gee.Set<Identity>? get_entity_identities(Jid jid) { - return entity_identities.has_key(jid) ? entity_identities[jid].read_only_view : null; // TODO isn’t this default for hashmap - } - - public bool? has_entity_identity(Jid jid, string category, string type) { - if (!entity_identities.has_key(jid)) return null; - if (entity_identities[jid] == null) return false; - foreach (Identity identity in entity_identities[jid]) { - if (identity.category == category && identity.type_ == type) return true; - } - return false; - } - - public bool? has_entity_feature(Jid jid, string feature) { - if (!entity_features.has_key(jid)) return null; - if (entity_features[jid] == null) return false; - return entity_features[jid].contains(feature); - } - - public void set_entity_identities(Jid jid, Gee.Set<Identity>? identities) { - entity_identities[jid] = identities; - } - - public void set_entity_features(Jid jid, Gee.List<string>? features) { - entity_features[jid] = features; - } - public void set_entity_items(Jid jid, Gee.List<Item>? features) { entity_items[jid] = features; } diff --git a/xmpp-vala/src/module/xep/0030_service_discovery/module.vala b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala index f21146f1..c61d3ab0 100644 --- a/xmpp-vala/src/module/xep/0030_service_discovery/module.vala +++ b/xmpp-vala/src/module/xep/0030_service_discovery/module.vala @@ -12,9 +12,10 @@ public class Module : XmppStreamModule, Iq.Handler { private HashMap<Jid, Future<InfoResult?>> active_info_requests = new HashMap<Jid, Future<InfoResult?>>(Jid.hash_func, Jid.equals_func); public Identity own_identity; + public CapsCache cache; public Module.with_identity(string category, string type, string? name = null) { - own_identity = new Identity(category, type, name); + this.own_identity = new Identity(category, type, name); } public void add_feature(XmppStream stream, string feature) { @@ -42,27 +43,11 @@ public class Module : XmppStreamModule, Iq.Handler { } public async bool has_entity_feature(XmppStream stream, Jid jid, string feature) { - Flag flag = stream.get_flag(Flag.IDENTITY); - - if (flag.has_entity_feature(jid, feature) == null) { - InfoResult? info_result = yield request_info(stream, jid); - stream.get_flag(Flag.IDENTITY).set_entity_features(jid, info_result != null ? info_result.features : null); - stream.get_flag(Flag.IDENTITY).set_entity_identities(jid, info_result != null ? info_result.identities : null); - } - - return flag.has_entity_feature(jid, feature) ?? false; + return yield this.cache.has_entity_feature(jid, feature); } public async Gee.Set<Identity>? get_entity_identities(XmppStream stream, Jid jid) { - Flag flag = stream.get_flag(Flag.IDENTITY); - - if (flag.get_entity_identities(jid) == null) { - InfoResult? info_result = yield request_info(stream, jid); - stream.get_flag(Flag.IDENTITY).set_entity_features(info_result.iq.from, info_result != null ? info_result.features : null); - stream.get_flag(Flag.IDENTITY).set_entity_identities(info_result.iq.from, info_result != null ? info_result.identities : null); - } - - return flag.get_entity_identities(jid); + return yield this.cache.get_entity_identities(jid); } public async InfoResult? request_info(XmppStream stream, Jid jid) { @@ -137,4 +122,9 @@ public class Module : XmppStreamModule, Iq.Handler { } } +public interface CapsCache : Object { + public abstract async bool has_entity_feature(Jid jid, string feature); + public abstract async Gee.Set<Identity> get_entity_identities(Jid jid); +} + } diff --git a/xmpp-vala/src/module/xep/0115_entitiy_capabilities.vala b/xmpp-vala/src/module/xep/0115_entitiy_capabilities.vala index be7db509..8e15e0c2 100644 --- a/xmpp-vala/src/module/xep/0115_entitiy_capabilities.vala +++ b/xmpp-vala/src/module/xep/0115_entitiy_capabilities.vala @@ -21,6 +21,16 @@ namespace Xmpp.Xep.EntityCapabilities { return ver_attribute; } + public string? get_server_caps_hash(XmppStream stream) { + StanzaNode? node = stream.features.get_subnode("c", NS_URI); + if (node == null) return null; + + string? ver_attribute = node.get_attribute("ver", NS_URI); + if (ver_attribute == null) return null; + + return ver_attribute; + } + public class Module : XmppStreamModule { public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0115_entity_capabilities"); @@ -40,15 +50,11 @@ namespace Xmpp.Xep.EntityCapabilities { public override void attach(XmppStream stream) { stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.connect(on_pre_send_presence_stanza); - stream.get_module(Presence.Module.IDENTITY).received_presence.connect(on_received_presence); stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); - - check_features_node_ver(stream); } public override void detach(XmppStream stream) { stream.get_module(Presence.Module.IDENTITY).pre_send_presence_stanza.disconnect(on_pre_send_presence_stanza); - stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(on_received_presence); stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); } @@ -64,51 +70,16 @@ namespace Xmpp.Xep.EntityCapabilities { } } - private void on_received_presence(XmppStream stream, Presence.Stanza presence) { - string? caps_hash = get_caps_hash(presence); - if (caps_hash == null) return; - - process_hash.begin(stream, presence.from, caps_hash); - } - - private void check_features_node_ver(XmppStream stream) { - StanzaNode? node = stream.features.get_subnode("c", NS_URI); - if (node == null) return; - - string? ver_attribute = node.get_attribute("ver", NS_URI); - if (ver_attribute == null) return; - - process_hash.begin(stream, stream.remote_name, ver_attribute); - } - - private async void process_hash(XmppStream stream, Jid jid_from, string caps_hash) { - Gee.List<string> capabilities = storage.get_features(caps_hash); - ServiceDiscovery.Identity identity = storage.get_identities(caps_hash); - if (identity == null) { - ServiceDiscovery.InfoResult? info_result = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, jid_from); - if (info_result == null) return; - store_entity_result(stream, caps_hash, info_result); - } else { - stream.get_flag(ServiceDiscovery.Flag.IDENTITY).set_entity_features(jid_from, capabilities); - } - } - - private void store_entity_result(XmppStream stream, string entity, ServiceDiscovery.InfoResult? query_result) { - if (query_result == null) return; - + public static string compute_hash_for_info_result(ServiceDiscovery.InfoResult info_result) { Gee.List<DataForms.DataForm> data_forms = new ArrayList<DataForms.DataForm>(); - foreach (StanzaNode node in query_result.iq.stanza.get_deep_subnodes(ServiceDiscovery.NS_URI_INFO + ":query", DataForms.NS_URI + ":x")) { + foreach (StanzaNode node in info_result.iq.stanza.get_deep_subnodes(ServiceDiscovery.NS_URI_INFO + ":query", DataForms.NS_URI + ":x")) { data_forms.add(DataForms.DataForm.create_from_node(node)); } - if (compute_hash(query_result.identities, query_result.features, data_forms) == entity) { - storage.store_identities(entity, query_result.identities); - storage.store_features(entity, query_result.features); - stream.get_flag(ServiceDiscovery.Flag.IDENTITY).set_entity_features(query_result.iq.from, query_result.features); - } + return compute_hash(info_result.identities, info_result.features, data_forms); } - private static string compute_hash(Gee.Set<ServiceDiscovery.Identity> identities_set, Gee.List<string> features, Gee.List<DataForms.DataForm> data_forms) { + public static string compute_hash(Gee.Set<ServiceDiscovery.Identity> identities_set, Gee.List<string> features, Gee.List<DataForms.DataForm> data_forms) { var identities = new ArrayList<ServiceDiscovery.Identity>(); foreach (var identity in identities_set) identities.add(identity); diff --git a/xmpp-vala/src/module/xep/0166_jingle.vala b/xmpp-vala/src/module/xep/0166_jingle.vala index 15c310bd..3a634222 100644 --- a/xmpp-vala/src/module/xep/0166_jingle.vala +++ b/xmpp-vala/src/module/xep/0166_jingle.vala @@ -149,7 +149,7 @@ public class Module : XmppStreamModule, Iq.Handler { } return transports[ns_uri]; } - public Transport? select_transport(XmppStream stream, TransportType type, Jid receiver_full_jid, Set<string> blacklist) { + public async Transport? select_transport(XmppStream stream, TransportType type, Jid receiver_full_jid, Set<string> blacklist) { Transport? result = null; foreach (Transport transport in transports.values) { if (transport.transport_type() != type) { @@ -158,7 +158,7 @@ public class Module : XmppStreamModule, Iq.Handler { if (transport.transport_ns_uri() in blacklist) { continue; } - if (transport.is_transport_available(stream, receiver_full_jid)) { + if (yield transport.is_transport_available(stream, receiver_full_jid)) { if (result != null) { if (result.transport_priority() >= transport.transport_priority()) { continue; @@ -180,20 +180,20 @@ public class Module : XmppStreamModule, Iq.Handler { return security_preconditions[ns_uri]; } - private bool is_jingle_available(XmppStream stream, Jid full_jid) { - bool? has_jingle = stream.get_flag(ServiceDiscovery.Flag.IDENTITY).has_entity_feature(full_jid, NS_URI); + private async bool is_jingle_available(XmppStream stream, Jid full_jid) { + bool? has_jingle = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); return has_jingle != null && has_jingle; } - public bool is_available(XmppStream stream, TransportType type, Jid full_jid) { - return is_jingle_available(stream, full_jid) && select_transport(stream, type, full_jid, Set.empty()) != null; + public async bool is_available(XmppStream stream, TransportType type, Jid full_jid) { + return (yield is_jingle_available(stream, full_jid)) && (yield select_transport(stream, type, full_jid, Set.empty())) != null; } - public Session create_session(XmppStream stream, TransportType type, Jid receiver_full_jid, Senders senders, string content_name, StanzaNode description, string? precondition_name = null, Object? precondation_options = null) throws Error { - if (!is_jingle_available(stream, receiver_full_jid)) { + public async Session create_session(XmppStream stream, TransportType type, Jid receiver_full_jid, Senders senders, string content_name, StanzaNode description, string? precondition_name = null, Object? precondation_options = null) throws Error { + if (!yield is_jingle_available(stream, receiver_full_jid)) { throw new Error.NO_SHARED_PROTOCOLS("No Jingle support"); } - Transport? transport = select_transport(stream, type, receiver_full_jid, Set.empty()); + Transport? transport = yield select_transport(stream, type, receiver_full_jid, Set.empty()); if (transport == null) { throw new Error.NO_SHARED_PROTOCOLS("No suitable transports"); } @@ -358,7 +358,7 @@ public delegate void SessionTerminate(Jid to, string sid, StanzaNode reason); public interface Transport : Object { public abstract string transport_ns_uri(); - public abstract bool is_transport_available(XmppStream stream, Jid full_jid); + public async abstract bool is_transport_available(XmppStream stream, Jid full_jid); public abstract TransportType transport_type(); public abstract int transport_priority(); public abstract TransportParameters create_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid) throws Error; @@ -613,7 +613,7 @@ public class Session { transport = null; } else { if (role == Role.INITIATOR) { - select_new_transport(stream); + select_new_transport.begin(stream); } else { state = State.WAITING_FOR_TRANSPORT_REPLACE; } @@ -640,8 +640,8 @@ public class Session { } content_type.handle_content_session_info(stream, this, info, iq); } - void select_new_transport(XmppStream stream) { - Transport? new_transport = stream.get_module(Module.IDENTITY).select_transport(stream, type_, peer_full_jid, tried_transport_methods); + async void select_new_transport(XmppStream stream) { + Transport? new_transport = yield stream.get_module(Module.IDENTITY).select_transport(stream, type_, peer_full_jid, tried_transport_methods); if (new_transport == null) { StanzaNode reason = new StanzaNode.build("reason", NS_URI) .put_node(new StanzaNode.build("failed-transport", NS_URI)); @@ -680,7 +680,7 @@ public class Session { throw new IqError.OUT_OF_ORDER("no outstanding transport-replace request"); } stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); - select_new_transport(stream); + select_new_transport.begin(stream); } void handle_transport_replace(XmppStream stream, StanzaNode transport_node, StanzaNode jingle, Iq.Stanza iq) throws IqError { Transport? transport = stream.get_module(Module.IDENTITY).get_transport(transport_node.ns_uri); diff --git a/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala b/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala index d3a1f4bf..1c0323be 100644 --- a/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala +++ b/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala @@ -42,12 +42,12 @@ public class Module : Jingle.ContentType, XmppStreamModule { public signal void file_incoming(XmppStream stream, FileTransfer file_transfer); - public bool is_available(XmppStream stream, Jid full_jid) { - bool? has_feature = stream.get_flag(ServiceDiscovery.Flag.IDENTITY).has_entity_feature(full_jid, NS_URI); + public async bool is_available(XmppStream stream, Jid full_jid) { + bool? has_feature = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); if (has_feature == null || !(!)has_feature) { return false; } - return stream.get_module(Jingle.Module.IDENTITY).is_available(stream, Jingle.TransportType.STREAMING, full_jid); + return yield stream.get_module(Jingle.Module.IDENTITY).is_available(stream, Jingle.TransportType.STREAMING, full_jid); } public async void offer_file_stream(XmppStream stream, Jid receiver_full_jid, InputStream input_stream, string basename, int64 size, string? precondition_name = null, Object? precondition_options = null) throws IOError { @@ -66,7 +66,7 @@ public class Module : Jingle.ContentType, XmppStreamModule { Jingle.Session session; try { - session = stream.get_module(Jingle.Module.IDENTITY) + session = yield stream.get_module(Jingle.Module.IDENTITY) .create_session(stream, Jingle.TransportType.STREAMING, receiver_full_jid, Jingle.Senders.INITIATOR, "a-file-offer", description, precondition_name, precondition_options); // TODO(hrxi): Why "a-file-offer"? } catch (Jingle.Error e) { throw new IOError.FAILED(@"couldn't create Jingle session: $(e.message)"); diff --git a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala index 2ae2881e..db2cd6a0 100644 --- a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala @@ -20,9 +20,8 @@ public class Module : Jingle.Transport, XmppStreamModule { public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } - public bool is_transport_available(XmppStream stream, Jid full_jid) { - bool? result = stream.get_flag(ServiceDiscovery.Flag.IDENTITY).has_entity_feature(full_jid, NS_URI); - return result != null && result; + public async bool is_transport_available(XmppStream stream, Jid full_jid) { + return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); } public string transport_ns_uri() { diff --git a/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala b/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala index fc6bc057..e26d63b7 100644 --- a/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala @@ -21,9 +21,8 @@ public class Module : Jingle.Transport, XmppStreamModule { public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } - public bool is_transport_available(XmppStream stream, Jid full_jid) { - bool? result = stream.get_flag(ServiceDiscovery.Flag.IDENTITY).has_entity_feature(full_jid, NS_URI); - return result != null && result; + public async bool is_transport_available(XmppStream stream, Jid full_jid) { + return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); } public string transport_ns_uri() { diff --git a/xmpp-vala/src/module/xep/0391_jingle_encrypted_transports.vala b/xmpp-vala/src/module/xep/0391_jingle_encrypted_transports.vala index 1161ec96..ff25d9b3 100644 --- a/xmpp-vala/src/module/xep/0391_jingle_encrypted_transports.vala +++ b/xmpp-vala/src/module/xep/0391_jingle_encrypted_transports.vala @@ -18,9 +18,8 @@ public class Module : XmppStreamModule, SecurityPrecondition { stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); } - public bool is_available(XmppStream stream, Jid full_jid) { - bool? has_feature = stream.get_flag(ServiceDiscovery.Flag.IDENTITY).has_entity_feature(full_jid, NS_URI); - return has_feature != null && (!)has_feature; + public async bool is_available(XmppStream stream, Jid full_jid) { + return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); } public void register_envelop_encoding(EnvelopEncoding encoding) { |