Don't go kaboom when the ZeroTier service isn't running.

This commit is contained in:
Grant Limberg 2015-11-04 18:28:07 -08:00
parent a95ff21aaf
commit 7b86176d0e
4 changed files with 146 additions and 54 deletions

View file

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Windows;
using Newtonsoft.Json;
namespace WinUI
@ -36,6 +37,8 @@ namespace WinUI
request.ContentType = "application/json";
}
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
@ -53,6 +56,15 @@ namespace WinUI
return status;
}
}
catch (System.Net.Sockets.SocketException)
{
return null;
}
catch (System.Net.WebException)
{
return null;
}
}
public List<ZeroTierNetwork> GetNetworks()
{
@ -65,6 +77,8 @@ namespace WinUI
request.Method = "GET";
request.ContentType = "application/json";
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
@ -82,6 +96,15 @@ namespace WinUI
return networkList;
}
}
catch (System.Net.Sockets.SocketException)
{
return null;
}
catch (System.Net.WebException)
{
return null;
}
}
public void JoinNetwork(string nwid)
{
@ -93,6 +116,8 @@ namespace WinUI
request.Method = "POST";
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
if (httpResponse.StatusCode != HttpStatusCode.OK)
@ -100,6 +125,15 @@ namespace WinUI
Console.WriteLine("Error sending join network message");
}
}
catch (System.Net.Sockets.SocketException)
{
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
}
catch (System.Net.WebException)
{
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
}
}
public void LeaveNetwork(string nwid)
{
@ -111,6 +145,8 @@ namespace WinUI
request.Method = "DELETE";
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
if (httpResponse.StatusCode != HttpStatusCode.OK)
@ -118,6 +154,15 @@ namespace WinUI
Console.WriteLine("Error sending leave network message");
}
}
catch (System.Net.Sockets.SocketException)
{
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
}
catch (System.Net.WebException)
{
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
}
}
public List<ZeroTierPeer> GetPeers()
{
@ -130,6 +175,8 @@ namespace WinUI
request.Method = "GET";
request.ContentType = "application/json";
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
@ -147,5 +194,14 @@ namespace WinUI
return peerList;
}
}
catch (System.Net.Sockets.SocketException)
{
return null;
}
catch (System.Net.WebException)
{
return null;
}
}
}
}

View file

@ -29,6 +29,8 @@ namespace WinUI
Timer timer = new Timer();
bool connected = false;
public MainWindow()
{
InitializeComponent();
@ -36,6 +38,11 @@ namespace WinUI
networksPage.SetAPIHandler(handler);
updateStatus();
if (!connected)
{
MessageBox.Show("Unable to connect to ZeroTier Service.");
}
updateNetworks();
updatePeers();
@ -50,6 +57,10 @@ namespace WinUI
{
var status = handler.GetStatus();
if (status != null)
{
connected = true;
networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.networkId.Content = status.Address;
@ -63,6 +74,24 @@ namespace WinUI
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()
{

View file

@ -35,6 +35,10 @@ namespace WinUI
public void setNetworks(List<ZeroTierNetwork> networks)
{
this.wrapPanel.Children.Clear();
if (networks == null)
{
return;
}
for (int i = 0; i < networks.Count; ++i)
{

View file

@ -31,6 +31,9 @@ namespace WinUI
public void SetPeers(List<ZeroTierPeer> peerList)
{
if (peerList == null)
return;
this.peersList = peerList;
dataGrid.ItemsSource = this.peersList;
dataGrid.Items.Refresh();