From 005b5aacaf504d6fdfec0088c0534fa9e606b844 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Wed, 9 Nov 2016 10:32:18 -0800 Subject: rename --- windows/WinUI/NetworkListView.xaml.cs | 153 ++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 windows/WinUI/NetworkListView.xaml.cs (limited to 'windows/WinUI/NetworkListView.xaml.cs') diff --git a/windows/WinUI/NetworkListView.xaml.cs b/windows/WinUI/NetworkListView.xaml.cs new file mode 100644 index 00000000..6983a3fb --- /dev/null +++ b/windows/WinUI/NetworkListView.xaml.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Timers; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.Windows.Threading; + +namespace WinUI +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class NetworkListView : Window + { + Regex charRegex = new Regex("[0-9a-fxA-FX]"); + Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$"); + + Timer timer = new Timer(); + + bool connected = false; + + public NetworkListView() + { + InitializeComponent(); + + updateStatus(); + + if (!connected) + { + MessageBox.Show("Unable to connect to ZerOTier Service"); + return; + } + + updateNetworks(); + + DataObject.AddPastingHandler(joinNetworkID, OnPaste); + + timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer); + timer.Interval = 2000; + timer.Enabled = true; + } + + private void updateStatus() + { + var status = APIHandler.Instance.GetStatus(); + + if (status != null) + { + connected = true; + + networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.networkId.Content = status.Address; + })); + versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.versionString.Content = status.Version; + })); + onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE"); + })); + } + else + { + connected = false; + + networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.networkId.Content = ""; + })); + versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.versionString.Content = "0"; + })); + onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + this.onlineStatus.Content = "OFFLINE"; + })); + } + } + + private void updateNetworks() + { + var networks = APIHandler.Instance.GetNetworks(); + + networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + networksPage.setNetworks(networks); + })); + } + + private void updatePeers() + { + //var peers = handler.GetPeers(); + + //peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + //{ + // peersPage.SetPeers(peers); + //})); + } + + private void OnUpdateTimer(object source, ElapsedEventArgs e) + { + updateStatus(); + updateNetworks(); + //updatePeers(); + } + + private void joinButton_Click(object sender, RoutedEventArgs e) + { + if (joinNetworkID.Text.Length < 16) + { + MessageBox.Show("Invalid Network ID"); + } + else + { + APIHandler.Instance.JoinNetwork(joinNetworkID.Text); + } + } + + private void OnNetworkEntered(object sender, TextCompositionEventArgs e) + { + e.Handled = !charRegex.IsMatch(e.Text); + } + + private void OnPaste(object sender, DataObjectPastingEventArgs e) + { + var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true); + if (!isText) return; + + var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string; + + if (!wholeStringRegex.IsMatch(text)) + { + e.CancelCommand(); + } + } + } +} -- cgit v1.2.3 From fd71ceeab542d81b94a9ceee69faf78357e5f3f3 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Thu, 10 Nov 2016 14:17:57 -0800 Subject: menu now dynamically populates the network list --- windows/WinUI/APIHandler.cs | 39 +++++++++------ windows/WinUI/NetworkListView.xaml.cs | 38 ++++++--------- windows/WinUI/NetworkRoute.cs | 20 +++++++- windows/WinUI/ToolbarItem.xaml | 37 ++++++++++++-- windows/WinUI/ToolbarItem.xaml.cs | 87 ++++++++++++++++++++++++++++++--- windows/WinUI/ZeroTierNetwork.cs | 90 ++++++++++++++++++++++++++++++++++- 6 files changed, 260 insertions(+), 51 deletions(-) (limited to 'windows/WinUI/NetworkListView.xaml.cs') diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs index 9cddd916..c7dc16b7 100644 --- a/windows/WinUI/APIHandler.cs +++ b/windows/WinUI/APIHandler.cs @@ -128,7 +128,9 @@ namespace WinUI this.authtoken = authtoken; } - public ZeroTierStatus GetStatus() + public delegate void StatusCallback(ZeroTierStatus status); + + public void GetStatus(StatusCallback cb) { var request = WebRequest.Create(url + "/status" + "?auth=" + authtoken) as HttpWebRequest; if (request != null) @@ -153,25 +155,27 @@ namespace WinUI { Console.WriteLine(e.ToString()); } - return status; + cb(status); } } catch (System.Net.Sockets.SocketException) { - return null; + cb(null); } catch (System.Net.WebException) { - return null; + cb(null); } } - public List GetNetworks() + public delegate void NetworkListCallback(List networks); + + public void GetNetworks(NetworkListCallback cb) { var request = WebRequest.Create(url + "/network" + "?auth=" + authtoken) as HttpWebRequest; if (request == null) { - return null; + cb(null); } request.Method = "GET"; @@ -188,21 +192,26 @@ namespace WinUI try { networkList = JsonConvert.DeserializeObject>(responseText); + foreach (ZeroTierNetwork n in networkList) + { + // all networks received via JSON are connected by definition + n.IsConnected = true; + } } catch (JsonReaderException e) { Console.WriteLine(e.ToString()); } - return networkList; + cb(networkList); } } catch (System.Net.Sockets.SocketException) { - return null; + cb(null); } catch (System.Net.WebException) { - return null; + cb(null); } } @@ -275,12 +284,14 @@ namespace WinUI } } - public List GetPeers() + public delegate void PeersCallback(List peers); + + public void GetPeers(PeersCallback cb) { var request = WebRequest.Create(url + "/peer" + "?auth=" + authtoken) as HttpWebRequest; if (request == null) { - return null; + cb(null); } request.Method = "GET"; @@ -302,16 +313,16 @@ namespace WinUI { Console.WriteLine(e.ToString()); } - return peerList; + cb(peerList); } } catch (System.Net.Sockets.SocketException) { - return null; + cb(null); } catch (System.Net.WebException) { - return null; + cb(null); } } } diff --git a/windows/WinUI/NetworkListView.xaml.cs b/windows/WinUI/NetworkListView.xaml.cs index 6983a3fb..b1ad3df6 100644 --- a/windows/WinUI/NetworkListView.xaml.cs +++ b/windows/WinUI/NetworkListView.xaml.cs @@ -36,7 +36,7 @@ namespace WinUI { InitializeComponent(); - updateStatus(); + APIHandler.Instance.GetStatus(updateStatus); if (!connected) { @@ -44,19 +44,19 @@ namespace WinUI return; } - updateNetworks(); + APIHandler.Instance.GetNetworks(updateNetworks); DataObject.AddPastingHandler(joinNetworkID, OnPaste); timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer); timer.Interval = 2000; timer.Enabled = true; + + } - private void updateStatus() + private void updateStatus(ZeroTierStatus status) { - var status = APIHandler.Instance.GetStatus(); - if (status != null) { connected = true; @@ -93,31 +93,21 @@ namespace WinUI } } - private void updateNetworks() + private void updateNetworks(List networks) { - var networks = APIHandler.Instance.GetNetworks(); - - networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + if (networks != null) { - networksPage.setNetworks(networks); - })); - } - - private void updatePeers() - { - //var peers = handler.GetPeers(); - - //peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => - //{ - // peersPage.SetPeers(peers); - //})); + networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + networksPage.setNetworks(networks); + })); + } } private void OnUpdateTimer(object source, ElapsedEventArgs e) { - updateStatus(); - updateNetworks(); - //updatePeers(); + APIHandler.Instance.GetStatus(updateStatus); + APIHandler.Instance.GetNetworks(updateNetworks); } private void joinButton_Click(object sender, RoutedEventArgs e) diff --git a/windows/WinUI/NetworkRoute.cs b/windows/WinUI/NetworkRoute.cs index 61616f44..cd1e549a 100644 --- a/windows/WinUI/NetworkRoute.cs +++ b/windows/WinUI/NetworkRoute.cs @@ -1,14 +1,32 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace WinUI { - public class NetworkRoute + [Serializable] + public class NetworkRoute : ISerializable { + protected NetworkRoute(SerializationInfo info, StreamingContext ctx) + { + Target = info.GetString("Target"); + Via = info.GetString("Via"); + Flags = info.GetInt32("Flags"); + Metric = info.GetInt32("Metric"); + } + + public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx) + { + info.AddValue("Target", Target); + info.AddValue("Via", Via); + info.AddValue("Flags", Flags); + info.AddValue("Metric", Metric); + } + [JsonProperty("target")] public string Target { get; set; } diff --git a/windows/WinUI/ToolbarItem.xaml b/windows/WinUI/ToolbarItem.xaml index 2a7af229..065919fd 100644 --- a/windows/WinUI/ToolbarItem.xaml +++ b/windows/WinUI/ToolbarItem.xaml @@ -5,8 +5,18 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WinUI" xmlns:tb="http://www.hardcodet.net/taskbar" + xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" mc:Ignorable="d" - Height="300" Width="300" Visibility="Hidden"> + Height="300" Width="300" Visibility="Hidden" Name="Toolbar"> + + + + + + + + + + Click="ToolbarItem_NodeIDClicked" + x:Name="nodeIdMenuItem"/> - + + Click="ToolbarItem_ShowNetworksClicked"/> + + + + + + + + + + + - + diff --git a/windows/WinUI/ToolbarItem.xaml.cs b/windows/WinUI/ToolbarItem.xaml.cs index d5cccbef..e53e4b35 100644 --- a/windows/WinUI/ToolbarItem.xaml.cs +++ b/windows/WinUI/ToolbarItem.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -26,11 +27,75 @@ namespace WinUI { private APIHandler handler = APIHandler.Instance; - NetworkListView netList = null; + private NetworkListView netListView = null; + private List networkList = null; + + private ObservableCollection _networkCollection = new ObservableCollection(); + + public ObservableCollection NetworkCollection + { + get { return _networkCollection; } + set { _networkCollection = value; } + } + + private Timer timer = null; public ToolbarItem() { InitializeComponent(); + + onUpdateTimer(this, null); + + timer = new Timer(); + timer.Elapsed += new ElapsedEventHandler(onUpdateTimer); + timer.Interval = 2000; + timer.Enabled = true; + + nodeIdMenuItem.Header = "OFFLINE"; + nodeIdMenuItem.IsEnabled = false; + } + + private void updateNetworks(List networks) + { + if (networks != null) + { + this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + foreach (ZeroTierNetwork n in networks) + { + int index = _networkCollection.IndexOf(n); + + if (index == -1) + { + _networkCollection.Add(n); + } + else + { + _networkCollection[index] = n; + } + } + + this.networkList = networks; + })); + } + } + + private void updateStatus(ZeroTierStatus status) + { + if (status != null) + { + Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + nodeIdMenuItem.Header = "Node ID: " + status.Address; + nodeIdMenuItem.IsEnabled = true; + })); + } + } + + private void onUpdateTimer(object source, ElapsedEventArgs e) + { + APIHandler.Instance.GetStatus(updateStatus); + APIHandler.Instance.GetNetworks(updateNetworks); } private void ToolbarItem_TrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e) @@ -50,17 +115,27 @@ namespace WinUI private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e) { - if (netList == null) + if (netListView == null) { - netList = new WinUI.NetworkListView(); - netList.Closed += ShowNetworksClosed; - netList.Show(); + netListView = new WinUI.NetworkListView(); + netListView.Closed += ShowNetworksClosed; + netListView.Show(); } } private void ShowNetworksClosed(object sender, System.EventArgs e) { - netList = null; + netListView = null; + } + + private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e) + { + + } + + private void JoinNetworkClosed(object sender, System.EventArgs e) + { + } } } diff --git a/windows/WinUI/ZeroTierNetwork.cs b/windows/WinUI/ZeroTierNetwork.cs index 6a6f8498..579e4e89 100644 --- a/windows/WinUI/ZeroTierNetwork.cs +++ b/windows/WinUI/ZeroTierNetwork.cs @@ -1,14 +1,60 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace WinUI { - public class ZeroTierNetwork + [Serializable] + public class ZeroTierNetwork : ISerializable, IEquatable, IComparable { + protected ZeroTierNetwork(SerializationInfo info, StreamingContext ctx) + { + NetworkId = info.GetString("NetworkId"); + MacAddress = info.GetString("MacAddress"); + NetworkName = info.GetString("NetworkName"); + NetworkStatus = info.GetString("NetworkStatus"); + NetworkType = info.GetString("NetworkType"); + 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("DeviceName"); + AllowManaged = info.GetBoolean("AllowManaged"); + AllowGlobal = info.GetBoolean("AllowGlobal"); + AllowDefault = info.GetBoolean("AllowDefault"); + IsConnected = false; + } + + public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx) + { + info.AddValue("NetworkId", NetworkId); + info.AddValue("MacAddress", MacAddress); + info.AddValue("NetworkName", NetworkName); + info.AddValue("NetworkStatus", NetworkStatus); + info.AddValue("NetworkType", 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("DeviceName", DeviceName); + info.AddValue("AllowManaged", AllowManaged); + info.AddValue("AllowGlobal", AllowGlobal); + info.AddValue("AllowDefault", AllowDefault); + } + + [JsonProperty("nwid")] public string NetworkId { get; set; } @@ -59,5 +105,47 @@ namespace WinUI [JsonProperty("allowDefault")] public bool AllowDefault { get; set; } + + public bool IsConnected { get; set; } = false; + + public String Title + { + get + { + + if (NetworkName != null && NetworkName.Length > 0) + { + return NetworkId + " (" + NetworkName + ")"; + } + else + { + return NetworkId; + } + } + } + + public bool Equals(ZeroTierNetwork network) + { + return NetworkId.Equals(network.NetworkId); + } + + public int CompareTo(ZeroTierNetwork network) + { + 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; + } + } } } -- cgit v1.2.3 From a4c274085233fe085efd68869dc979ea56183c87 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Thu, 17 Nov 2016 14:25:55 -0800 Subject: added skeletons for join and about windows. moving things around a little bit too --- windows/WinUI/NetworkInfoView.xaml | 82 --------------- windows/WinUI/NetworkInfoView.xaml.cs | 131 ----------------------- windows/WinUI/NetworkListView.xaml | 134 ----------------------- windows/WinUI/NetworkListView.xaml.cs | 143 ------------------------- windows/WinUI/NetworksPage.xaml | 13 --- windows/WinUI/NetworksPage.xaml.cs | 99 ----------------- windows/WinUI/PeersPage.xaml | 26 ----- windows/WinUI/PeersPage.xaml.cs | 54 ---------- windows/WinUI/ToolbarItem.xaml | 74 ------------- windows/WinUI/ToolbarItem.xaml.cs | 158 ---------------------------- windows/WinUI/Views/AboutView.xaml | 12 +++ windows/WinUI/Views/AboutView.xaml.cs | 27 +++++ windows/WinUI/Views/JoinNetworkView.xaml | 12 +++ windows/WinUI/Views/JoinNetworkView.xaml.cs | 27 +++++ windows/WinUI/Views/NetworkInfoView.xaml | 82 +++++++++++++++ windows/WinUI/Views/NetworkInfoView.xaml.cs | 131 +++++++++++++++++++++++ windows/WinUI/Views/NetworkListView.xaml | 134 +++++++++++++++++++++++ windows/WinUI/Views/NetworkListView.xaml.cs | 143 +++++++++++++++++++++++++ windows/WinUI/Views/NetworksPage.xaml | 13 +++ windows/WinUI/Views/NetworksPage.xaml.cs | 99 +++++++++++++++++ windows/WinUI/Views/PeersPage.xaml | 26 +++++ windows/WinUI/Views/PeersPage.xaml.cs | 54 ++++++++++ windows/WinUI/Views/ToolbarItem.xaml | 74 +++++++++++++ windows/WinUI/Views/ToolbarItem.xaml.cs | 158 ++++++++++++++++++++++++++++ 24 files changed, 992 insertions(+), 914 deletions(-) delete mode 100644 windows/WinUI/NetworkInfoView.xaml delete mode 100644 windows/WinUI/NetworkInfoView.xaml.cs delete mode 100644 windows/WinUI/NetworkListView.xaml delete mode 100644 windows/WinUI/NetworkListView.xaml.cs delete mode 100644 windows/WinUI/NetworksPage.xaml delete mode 100644 windows/WinUI/NetworksPage.xaml.cs delete mode 100644 windows/WinUI/PeersPage.xaml delete mode 100644 windows/WinUI/PeersPage.xaml.cs delete mode 100644 windows/WinUI/ToolbarItem.xaml delete mode 100644 windows/WinUI/ToolbarItem.xaml.cs create mode 100644 windows/WinUI/Views/AboutView.xaml create mode 100644 windows/WinUI/Views/AboutView.xaml.cs create mode 100644 windows/WinUI/Views/JoinNetworkView.xaml create mode 100644 windows/WinUI/Views/JoinNetworkView.xaml.cs create mode 100644 windows/WinUI/Views/NetworkInfoView.xaml create mode 100644 windows/WinUI/Views/NetworkInfoView.xaml.cs create mode 100644 windows/WinUI/Views/NetworkListView.xaml create mode 100644 windows/WinUI/Views/NetworkListView.xaml.cs create mode 100644 windows/WinUI/Views/NetworksPage.xaml create mode 100644 windows/WinUI/Views/NetworksPage.xaml.cs create mode 100644 windows/WinUI/Views/PeersPage.xaml create mode 100644 windows/WinUI/Views/PeersPage.xaml.cs create mode 100644 windows/WinUI/Views/ToolbarItem.xaml create mode 100644 windows/WinUI/Views/ToolbarItem.xaml.cs (limited to 'windows/WinUI/NetworkListView.xaml.cs') diff --git a/windows/WinUI/NetworkInfoView.xaml b/windows/WinUI/NetworkInfoView.xaml deleted file mode 100644 index b3e8cae0..00000000 --- a/windows/WinUI/NetworkInfoView.xaml +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -