From c1f18c11c3049dfca880d37c60bcd92e223f578b Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Sun, 11 Jun 2017 19:53:55 +0200 Subject: [PATCH] devicemanager, packethandlers: rework device capability handling --- src/mconnect/devicemanager.vala | 37 +++++++++------- src/mconnect/packethandlers.vala | 72 ++++++++++---------------------- 2 files changed, 43 insertions(+), 66 deletions(-) diff --git a/src/mconnect/devicemanager.vala b/src/mconnect/devicemanager.vala index ee720f7..8c15beb 100644 --- a/src/mconnect/devicemanager.vala +++ b/src/mconnect/devicemanager.vala @@ -121,11 +121,17 @@ class DeviceManager : GLib.Object this.devices.@set(unique, new_dev); is_new = true; + } else { debug("device %s already present", unique); } var dev = this.devices.@get(unique); + + if (is_new) { + dev.capability_added.connect(this.device_capability_added_cb); + dev.capability_removed.connect(this.device_capability_removed_cb); + } // update device information dev.update_from_device(new_dev); @@ -187,12 +193,7 @@ class DeviceManager : GLib.Object update_cache(); - if (status == true) { - // register message handlers - this.enable_protocol_handlers(dev); - } else { - this.disable_protocol_handlers(dev); - + if (status == false) { // we're no longer interested in paired singnal dev.paired.disconnect(this.device_paired); @@ -202,23 +203,29 @@ class DeviceManager : GLib.Object } - private void enable_protocol_handlers(Device dev) { + private void device_capability_added_cb(Device dev, string cap) { + info("capability %s added to device %s", cap, dev.to_string()); + + if (dev.has_capability_handler(cap)) { + return; + } + var core = Core.instance(); - core.handlers.use_device(dev, (cap, handler) => { - device_capability_added(dev, cap, handler); - }); + var h = core.handlers.get_capability_handler(cap); + if (h != null) { + dev.register_capability_handler(cap, h); + } else { + warning("no handler for capability %s", cap); + } } - private void disable_protocol_handlers(Device dev) { - var core = Core.instance(); - core.handlers.release_device(dev); + private void device_capability_removed_cb(Device dev, string cap) { + info("capability %s removed from device %s", cap, dev.to_string()); } private void device_disconnected(Device dev) { debug("device %s got disconnected", dev.to_string()); - this.disable_protocol_handlers(dev); - dev.paired.disconnect(this.device_paired); dev.disconnected.disconnect(this.device_disconnected); } diff --git a/src/mconnect/packethandlers.vala b/src/mconnect/packethandlers.vala index 8603926..e47b5e7 100644 --- a/src/mconnect/packethandlers.vala +++ b/src/mconnect/packethandlers.vala @@ -17,30 +17,24 @@ * AUTHORS * Maciek Borzecki */ +using Gee; class PacketHandlers : Object { - private List _handlers = null; + private HashMap _handlers; - public List handlers { - get { - return _handlers; - } - private set { - _handlers = handlers.copy(); - } + public string[] interfaces { + owned get { return _handlers.keys.to_array(); } + private set {} } - public string[] interfaces { get; private set; default = null; } public PacketHandlers() { _handlers = load_handlers(); - string [] ifaces; - list_handlers(out ifaces); - interfaces = ifaces; } - private static List load_handlers() { - List hnd = new List(); + private static HashMap load_handlers() { + HashMap hnd = + new HashMap(); var notification = NotificationHandler.instance(); var battery = BatteryHandler.instance(); @@ -48,24 +42,15 @@ class PacketHandlers : Object { var mousepad = MousepadHandler.instance(); var ping = PingHandler.instance(); - hnd.append(notification); - hnd.append(battery); - hnd.append(telephony); - hnd.append(mousepad); - hnd.append(ping); + hnd.@set(notification.get_pkt_type(), notification); + hnd.@set(battery.get_pkt_type(), battery); + hnd.@set(telephony.get_pkt_type(), telephony); + hnd.@set(mousepad.get_pkt_type(), mousepad); + hnd.@set(ping.get_pkt_type(), ping); return hnd; } - public void list_handlers(out string[] interfaces) { - interfaces = new string[_handlers.length()]; - // string[] interfaces = new string[_handlers.length()]; - for (int i = 0; i < _handlers.length(); i++) { - interfaces[i] = _handlers.nth_data(i).get_pkt_type(); - } - // return interfaces; - } - /** * SupportedCapabilityFunc: * @capability: capability name @@ -77,30 +62,15 @@ class PacketHandlers : Object { public delegate void SupportedCapabilityFunc(string capability, PacketHandlerInterface handler); - /** - * use_device: - * @dev: device - * @cb: callback see @SupportedCapabilityFunc to details - * - * Enable protocol handlers supported by device - */ - public void use_device(Device dev, SupportedCapabilityFunc? cb) { - _handlers.foreach((h) => { - var cap = h.get_pkt_type(); - if (dev.supports_capability(cap)) { - h.use_device(dev); - if (cb != null) { - cb(cap, h); - } - } else { - warning("capability %s not supported by device", cap); - } - }); + public PacketHandlerInterface? get_capability_handler(string cap) { + // all handlers are singletones for now + var h = this._handlers.@get(cap); + return h; } - public void release_device(Device dev) { - _handlers.foreach((h) => { - h.release_device(dev); - }); + public static string to_capability(string pkttype) { + if (pkttype.has_suffix(".request")) + return pkttype.replace(".request", ""); + return pkttype; } } \ No newline at end of file