menu now dynamically populates the network list

This commit is contained in:
Grant Limberg 2016-11-10 14:17:57 -08:00
parent 005b5aacaf
commit fd71ceeab5
6 changed files with 260 additions and 51 deletions

View File

@ -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<ZeroTierNetwork> GetNetworks()
public delegate void NetworkListCallback(List<ZeroTierNetwork> 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<List<ZeroTierNetwork>>(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<ZeroTierPeer> GetPeers()
public delegate void PeersCallback(List<ZeroTierPeer> 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);
}
}
}

View File

@ -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<ZeroTierNetwork> 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)

View File

@ -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; }

View File

@ -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">
<Window.Resources>
<CollectionViewSource Source="{Binding ElementName=Toolbar, Path=NetworkCollection}" x:Key="KnownNetworks">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="NetworkId" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<tb:TaskbarIcon x:Name="MyNotifyIcon"
IconSource="ZeroTierIcon.ico"
@ -16,17 +26,34 @@
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Node ID: abeb9f9bc5"
Click="ToolbarItem_NodeIDClicked"/>
Click="ToolbarItem_NodeIDClicked"
x:Name="nodeIdMenuItem"/>
<Separator/>
<MenuItem Header="Join Network..."/>
<MenuItem Header="Join Network..."
Click="ToolbarItem_JoinNetworkClicked"/>
<MenuItem Header="Show Networks..."
Click="ToolbarItem_ShowNetworksClicked"/>
Click="ToolbarItem_ShowNetworksClicked"/>
<Separator/>
<MenuItem Header="Networks">
<MenuItem.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource KnownNetworks}}"/>
</CompositeCollection>
</MenuItem.ItemsSource>
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Header" Value="{Binding Title}"/>
<Setter Property="MenuItem.IsCheckable" Value="True"/>
<Setter Property="MenuItem.IsChecked" Value="{Binding IsConnected}"/>
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
<Separator/>
<MenuItem Header="About..."/>
<MenuItem Header="Preferences..."/>
<Separator/>
<MenuItem Header="Quit"/>
</ContextMenu>
</ContextMenu>
</tb:TaskbarIcon.ContextMenu>
</tb:TaskbarIcon>

View File

@ -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<ZeroTierNetwork> networkList = null;
private ObservableCollection<ZeroTierNetwork> _networkCollection = new ObservableCollection<ZeroTierNetwork>();
public ObservableCollection<ZeroTierNetwork> 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<ZeroTierNetwork> 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)
{
}
}
}

View File

@ -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<ZeroTierNetwork>, IComparable<ZeroTierNetwork>
{
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;
}
}
}
}