diff --git a/Makefile.am b/Makefile.am index 1e53974..e946c95 100644 --- a/Makefile.am +++ b/Makefile.am @@ -56,6 +56,7 @@ mconnect_SOURCES = \ src/mconnect/discovery.vala \ src/mconnect/packet.vala \ src/mconnect/device.vala \ + src/mconnect/discovereddevice.vala \ src/mconnect/device-proxy.vala \ src/mconnect/devicemanager.vala \ src/mconnect/devicemanager-proxy.vala \ diff --git a/src/mconnect/application.vala b/src/mconnect/application.vala index 949f75e..3d03db2 100644 --- a/src/mconnect/application.vala +++ b/src/mconnect/application.vala @@ -60,8 +60,8 @@ namespace Mconn { Notify.init("mconnect"); - discovery.device_found.connect((disc, dev) => { - manager.handle_new_device(dev); + discovery.device_found.connect((disc, discdev) => { + manager.handle_discovered_device(discdev); }); try { diff --git a/src/mconnect/device.vala b/src/mconnect/device.vala index c7180f4..534f472 100644 --- a/src/mconnect/device.vala +++ b/src/mconnect/device.vala @@ -57,19 +57,15 @@ class Device : Object { * @param pkt identity packet * @param host source host that the packet came from */ - public Device.from_identity(Packet pkt, InetAddress host) { - - debug("got packet: %s", pkt.to_string()); - - var body = pkt.body; - this.host = host; - this.device_name = body.get_string_member("deviceName"); - this.device_id = body.get_string_member("deviceId"); - this.device_type = body.get_string_member("deviceType"); - this.protocol_version = (int) body.get_int_member("protocolVersion"); - this.tcp_port = (uint) body.get_int_member("tcpPort"); - - debug("added new device: %s", this.to_string()); + public Device.from_discovered_device(DiscoveredDevice disc) { + this.host = disc.host; + this.device_name = disc.device_name; + this.device_id = disc.device_id; + this.device_type = disc.device_type; + this.protocol_version = disc.protocol_version; + this.tcp_port = disc.tcp_port; + + debug("new device: %s", this.to_string()); } /** diff --git a/src/mconnect/devicemanager.vala b/src/mconnect/devicemanager.vala index 4beffa4..5d43f64 100644 --- a/src/mconnect/devicemanager.vala +++ b/src/mconnect/devicemanager.vala @@ -99,9 +99,15 @@ class DeviceManager : GLib.Object } } - public void handle_new_device(Device new_dev) { - debug("found device: %s", new_dev.to_string()); + public void handle_discovered_device(DiscoveredDevice discovered_dev) { + debug("found device: %s", discovered_dev.to_string()); + + var new_dev = new Device.from_discovered_device(discovered_dev); + handle_new_device(new_dev); + } + + public void handle_new_device(Device new_dev) { var is_new = false; string unique = new_dev.to_unique_string(); debug("device key: %s", unique); diff --git a/src/mconnect/discovereddevice.vala b/src/mconnect/discovereddevice.vala new file mode 100644 index 0000000..cb1a7c6 --- /dev/null +++ b/src/mconnect/discovereddevice.vala @@ -0,0 +1,61 @@ +/* ex:ts=4:sw=4:sts=4:et */ +/* -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * AUTHORS + * Maciek Borzecki + */ + +/** + * Newly discovered device wrapper. + */ +class DiscoveredDevice : Object { + + public string device_id { get; private set; default = ""; } + public string device_name { get; private set; default = ""; } + public string device_type { get; private set; default = ""; } + public uint protocol_version {get; private set; default = 5; } + public uint tcp_port {get; private set; default = 1714; } + public InetAddress host { get; private set; default = null; } + + /** + * Constructs DiscoveredDevice based on identity packet. + * + * @param pkt identity packet + * @param host source host that the packet came from + */ + public DiscoveredDevice.from_identity(Packet pkt, InetAddress host) { + + debug("got packet: %s", pkt.to_string()); + + var body = pkt.body; + this.host = host; + this.device_name = body.get_string_member("deviceName"); + this.device_id = body.get_string_member("deviceId"); + this.device_type = body.get_string_member("deviceType"); + this.protocol_version = (int) body.get_int_member("protocolVersion"); + this.tcp_port = (uint) body.get_int_member("tcpPort"); + + debug("discovered new device: %s", this.to_string()); + } + + public string to_string() { + return "discovered-%s-%s-%s-%u".printf(this.device_id, + this.device_name, + this.device_type, + this.protocol_version); + } + +} \ No newline at end of file diff --git a/src/mconnect/discovery.vala b/src/mconnect/discovery.vala index a6dadb0..2123fcb 100644 --- a/src/mconnect/discovery.vala +++ b/src/mconnect/discovery.vala @@ -22,10 +22,10 @@ class Discovery : GLib.Object { private Socket socket = null; - public signal void device_found(Device dev); + public signal void device_found(DiscoveredDevice dev); public Discovery() { - } + } ~Discovery() { debug("cleaning up discovery..."); @@ -91,7 +91,7 @@ class Discovery : GLib.Object return; } - var dev = new Device.from_identity(pkt, host); + var dev = new DiscoveredDevice.from_identity(pkt, host); message("connection from device: \'%s\', responds at: %s:%u", dev.device_name, host.to_string(), dev.tcp_port);