microsoft / WindowsDevicePortalWrapper

A client library that wraps the Windows Device Portal REST APIs.
MIT License
182 stars 87 forks source link

Unambiguously identify devices and renaming #179

Open GregPettyjohn opened 7 years ago

GregPettyjohn commented 7 years ago

I was wondering if there is a way to get a normalized “identifier” for a DevicePortal/IDevicePortalConnection object. In particular, I’ve run into a couple of scenarios where device identification could become ambiguous/problematic.

First Scenario: Checking for duplicates. The sample tracks multiple device portal connections and I would like to restrict it to a single connection per device. Unfortunately, this becomes problematic because there could be multiple ways to identify the same physical device: • For example, specify the device using an IP address: https://1.2.3.4:11443 • For example, specify the device using the device name https://MyXbox:1443

Second Scenario: Renaming a device. In this scenario, you connect to a device using the device name, and then you rename the device. After renaming, you must reboot for the new name to take effect. I’ve noticed that neither the IDevicePortalConnection nor the DevicePortal object allows you to change the name/address that you used when you originally connected. So if you are using the same connection that you established before changing the name, it will eventually fail because it is not aware of the new device name.

What I would like is for the connection object to track both the name and address of the device: • The address can be used as a way to unambiguously compare connection objects. • The name is a convenient/friendly way to identify the device • The name should be updated opportunistically to keep it up-to-date with name changes.

GregPettyjohn commented 7 years ago

I was just looking at the UpdateConnection method in IDevicePortalConnection. From the code, it looks like it almost does what I want (it is very similar to what I implemented) but unfortunately, it looks like the code is broken: ///

    /// Updates the device's connection Uri.
    /// </summary>

    /// <param name="ipConfig">The device's IP configuration data.</param>
    /// <param name="requiresHttps">Indicates whether or not to always require a secure connection.</param>
    public void UpdateConnection(
        IpConfiguration ipConfig,
        bool requiresHttps = false)
    {
        Uri newConnection = null;
        foreach (NetworkAdapterInfo adapter in ipConfig.Adapters)
        {
            foreach (IpAddressInfo addressInfo in adapter.IpAddresses)
            {
                // We take the first, non-169.x.x.x address we find that is not 0.0.0.0.
                if ((addressInfo.Address != "0.0.0.0") && !addressInfo.Address.StartsWith("169."))
                {
                    newConnection = new Uri(
                        string.Format(
                            "{0}://{1}", 
                            requiresHttps ? "https" : "http",
                            this.Connection.Authority));
                    break;
                }
            }

            if (newConnection != null)
            {
                this.Connection = newConnection;
                break;
            }
        }
    }

Study the usage of addressInfo and answer this question: Did you intend to use the actual IP address from the addressInfo? Looks like you're just copying the Authority from the original connection -- so the IP Address that you find in the IpConfig is not really used for anything.