123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- namespace WinUI
- {
- [Serializable]
- public class ZeroTierNetwork : ISerializable, IEquatable<ZeroTierNetwork>, IComparable<ZeroTierNetwork>, INotifyPropertyChanged
- {
- private string networkId;
- private string macAddress;
- private string networkName;
- private string networkStatus;
- private string networkType;
- private Int32 mtu;
- private bool dhcp;
- private bool bridge;
- private bool broadcastEnabled;
- private Int32 portError;
- private Int32 netconfRevision;
- private string[] assignedAddresses;
- private NetworkRoute[] routes;
- private string deviceName;
- private bool allowManaged;
- private bool allowGlobal;
- private bool allowDefault;
- private bool allowDNS;
- private bool isConnected;
- protected ZeroTierNetwork(SerializationInfo info, StreamingContext ctx)
- {
- try
- {
- NetworkId = info.GetString("nwid");
- MacAddress = info.GetString("mac");
- NetworkName = info.GetString("name");
- NetworkStatus = info.GetString("status");
- NetworkType = info.GetString("type");
- MTU = info.GetInt32("mtu");
- DHCP = info.GetBoolean("dhcp");
- Bridge = info.GetBoolean("bridge");
- BroadcastEnabled = info.GetBoolean("broadcastEnabled");
- PortError = info.GetInt32("portError");
- NetconfRevision = info.GetInt32("netconfRevision");
- AssignedAddresses = (string[])info.GetValue("assignedAddresses", typeof(string[]));
- Routes = (NetworkRoute[])info.GetValue("routes", typeof(NetworkRoute[]));
- DeviceName = info.GetString("portDeviceName");
- AllowManaged = info.GetBoolean("allowManaged");
- AllowGlobal = info.GetBoolean("allowGlobal");
- AllowDefault = info.GetBoolean("allowDefault");
- AllowDNS = info.GetBoolean("allowDNS");
- }
- catch { }
- IsConnected = false;
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx)
- {
- info.AddValue("nwid", NetworkId);
- info.AddValue("mac", MacAddress);
- info.AddValue("name", NetworkName);
- info.AddValue("status", NetworkStatus);
- info.AddValue("type", NetworkType);
- info.AddValue("mtu", MTU);
- info.AddValue("dhcp", DHCP);
- info.AddValue("bridge", Bridge);
- info.AddValue("broadcastEnabled", BroadcastEnabled);
- info.AddValue("portError", PortError);
- info.AddValue("netconfRevision", NetconfRevision);
- info.AddValue("assignedAddresses", AssignedAddresses);
- info.AddValue("routes", Routes);
- info.AddValue("portDeviceName", DeviceName);
- info.AddValue("allowManaged", AllowManaged);
- info.AddValue("allowGlobal", AllowGlobal);
- info.AddValue("allowDefault", AllowDefault);
- info.AddValue("allowDNS", AllowDNS);
- }
- public void UpdateNetwork(ZeroTierNetwork network)
- {
- if (network == null)
- return;
- if (!NetworkId.Equals(network.NetworkId))
- {
- NetworkId = network.NetworkId;
- }
- if (!MacAddress.Equals(network.MacAddress))
- {
- MacAddress = network.MacAddress;
- }
- if (!NetworkName.Equals(network.NetworkName))
- {
- NetworkName = network.NetworkName;
- }
- if (!NetworkStatus.Equals(network.NetworkStatus))
- {
- NetworkStatus = network.NetworkStatus;
- }
- if (!NetworkType.Equals(network.NetworkType))
- {
- NetworkType = network.NetworkType;
- }
- if (MTU != network.MTU)
- {
- MTU = network.MTU;
- }
- if (DHCP != network.DHCP)
- {
- DHCP = network.DHCP;
- }
- if (Bridge != network.Bridge)
- {
- Bridge = network.Bridge;
- }
- if (BroadcastEnabled != network.BroadcastEnabled)
- {
- BroadcastEnabled = network.BroadcastEnabled;
- }
- if (PortError != network.PortError)
- {
- PortError = network.PortError;
- }
- if (NetconfRevision != network.NetconfRevision)
- {
- NetconfRevision = network.NetconfRevision;
- }
- AssignedAddresses = network.AssignedAddresses;
- Routes = network.Routes;
- if (!DeviceName.Equals(network.DeviceName))
- {
- DeviceName = network.DeviceName;
- }
- if (AllowManaged != network.AllowManaged)
- {
- AllowManaged = network.AllowManaged;
- }
- if (AllowGlobal != network.AllowGlobal)
- {
- AllowGlobal = network.AllowGlobal;
- }
- if (AllowDefault != network.AllowDefault)
- {
- AllowDefault = network.AllowDefault;
- }
- if (AllowDNS != network.AllowDNS)
- {
- AllowDNS = network.AllowDNS;
- }
- if (IsConnected != network.IsConnected)
- {
- IsConnected = network.IsConnected;
- }
- }
- protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- [JsonProperty("nwid")]
- public string NetworkId {
- get
- {
- return networkId;
- }
- set
- {
- networkId = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("mac")]
- public string MacAddress
- {
- get
- {
- return macAddress;
- }
- set
- {
- macAddress = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("name")]
- public string NetworkName
- {
- get
- {
- return networkName;
- }
- set
- {
- networkName = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("status")]
- public string NetworkStatus
- {
- get
- {
- return networkStatus;
- }
- set
- {
- networkStatus = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("type")]
- public string NetworkType
- {
- get
- {
- return networkType;
- }
- set
- {
- networkType = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("mtu")]
- public int MTU
- {
- get
- {
- return mtu;
- }
- set
- {
- mtu = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("dhcp")]
- public bool DHCP
- {
- get
- {
- return dhcp;
- }
- set
- {
- dhcp = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("bridge")]
- public bool Bridge
- {
- get
- {
- return bridge;
- }
- set
- {
- bridge = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("broadcastEnabled")]
- public bool BroadcastEnabled
- {
- get
- {
- return broadcastEnabled;
- }
- set
- {
- broadcastEnabled = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("portError")]
- public int PortError
- {
- get
- {
- return portError;
- }
- set
- {
- portError = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("netconfRevision")]
- public int NetconfRevision
- {
- get
- {
- return netconfRevision;
- }
- set
- {
- netconfRevision = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("assignedAddresses")]
- public string[] AssignedAddresses
- {
- get
- {
- return assignedAddresses;
- }
- set
- {
- assignedAddresses = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("routes")]
- public NetworkRoute[] Routes
- {
- get
- {
- return routes;
- }
- set
- {
- routes = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("portDeviceName")]
- public string DeviceName
- {
- get
- {
- return deviceName;
- }
- set
- {
- deviceName = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("allowManaged")]
- public bool AllowManaged
- {
- get
- {
- return allowManaged;
- }
- set
- {
- allowManaged = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("allowGlobal")]
- public bool AllowGlobal
- {
- get
- {
- return allowGlobal;
- }
- set
- {
- allowGlobal = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("allowDefault")]
- public bool AllowDefault
- {
- get
- {
- return allowDefault;
- }
- set
- {
- allowDefault = value;
- NotifyPropertyChanged();
- }
- }
- [JsonProperty("allowDNS")]
- public bool AllowDNS
- {
- get
- {
- return allowDNS;
- }
- set
- {
- allowDNS = value;
- NotifyPropertyChanged();
- }
- }
-
- public bool IsConnected
- {
- get
- {
- return isConnected;
- }
- set
- {
- isConnected = value;
- NotifyPropertyChanged();
- }
- }
- public String Title
- {
- get
- {
- if (NetworkName != null && NetworkName.Length > 0)
- {
- return NetworkId + " (" + NetworkName + ")";
- }
- else
- {
- return NetworkId;
- }
- }
- }
- public bool Equals(ZeroTierNetwork network)
- {
- if (NetworkId == null || network == null)
- return false;
- return NetworkId.Equals(network.NetworkId);
- }
-
- public int CompareTo(ZeroTierNetwork network)
- {
- if (NetworkId == null || network == null)
- return -1;
- UInt64 thisNwid = UInt64.Parse(NetworkId, System.Globalization.NumberStyles.HexNumber);
- UInt64 otherNwid = UInt64.Parse(network.NetworkId, System.Globalization.NumberStyles.HexNumber);
- if (thisNwid > otherNwid)
- {
- return 1;
- }
- else if (thisNwid < otherNwid)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
- }
- public class NetworkEqualityComparer : IEqualityComparer<ZeroTierNetwork>
- {
- public bool Equals(ZeroTierNetwork lhs, ZeroTierNetwork rhs)
- {
- if (lhs.NetworkId.Equals(rhs.NetworkId))
- {
- lhs.UpdateNetwork(rhs);
- return true;
- }
- return false;
- }
- public int GetHashCode(ZeroTierNetwork obj)
- {
- return obj.NetworkId.GetHashCode();
- }
- }
- }
|