diff options
author | Grant Limberg <grant.limberg@zerotier.com> | 2016-11-18 13:52:48 -0800 |
---|---|---|
committer | Grant Limberg <grant.limberg@zerotier.com> | 2016-11-18 13:52:48 -0800 |
commit | e48d6880c92b87efb694798b43873dc07a570035 (patch) | |
tree | 55e050bddd6ba6106902be4034d6fd70af023c27 /windows/WinUI | |
parent | dbe1461886256bb5e9c1f7c6cb02525e57ee12c2 (diff) | |
download | infinitytier-e48d6880c92b87efb694798b43873dc07a570035.tar.gz infinitytier-e48d6880c92b87efb694798b43873dc07a570035.zip |
Wired up connected checkbox and delete button. just something not working with refresh.
Diffstat (limited to 'windows/WinUI')
-rw-r--r-- | windows/WinUI/APIHandler.cs | 10 | ||||
-rw-r--r-- | windows/WinUI/NetworkInfoView.xaml | 5 | ||||
-rw-r--r-- | windows/WinUI/NetworkInfoView.xaml.cs | 31 | ||||
-rw-r--r-- | windows/WinUI/NetworkListView.xaml | 64 | ||||
-rw-r--r-- | windows/WinUI/NetworkListView.xaml.cs | 79 | ||||
-rw-r--r-- | windows/WinUI/NetworkMonitor.cs | 58 |
6 files changed, 96 insertions, 151 deletions
diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs index 53a9c117..177d336f 100644 --- a/windows/WinUI/APIHandler.cs +++ b/windows/WinUI/APIHandler.cs @@ -25,10 +25,6 @@ namespace WinUI public delegate void NetworkListCallback(List<ZeroTierNetwork> networks); public delegate void StatusCallback(ZeroTierStatus status); - - private NetworkListCallback _networkCallbacks; - private StatusCallback _statusCallbacks; - public static APIHandler Instance { get @@ -187,7 +183,7 @@ namespace WinUI request.Method = "GET"; request.ContentType = "application/json"; - request.Timeout = 2000; + request.Timeout = 10000; try { @@ -233,7 +229,7 @@ namespace WinUI request.Method = "POST"; request.ContentType = "applicaiton/json"; - request.Timeout = 2000; + request.Timeout = 10000; using (var streamWriter = new StreamWriter(((HttpWebRequest)request).GetRequestStream())) { @@ -273,7 +269,7 @@ namespace WinUI } request.Method = "DELETE"; - request.Timeout = 2000; + request.Timeout = 10000; try { diff --git a/windows/WinUI/NetworkInfoView.xaml b/windows/WinUI/NetworkInfoView.xaml index b3e8cae0..6b32569e 100644 --- a/windows/WinUI/NetworkInfoView.xaml +++ b/windows/WinUI/NetworkInfoView.xaml @@ -73,9 +73,12 @@ <Grid Grid.Column="0" Grid.Row="14" Grid.ColumnSpan="3" Background="GhostWhite"> <Grid.ColumnDefinitions> + <ColumnDefinition Width="auto"/> <ColumnDefinition Width="*"/> + <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> - <Button x:Name="leaveButton" Content="Leave" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75" Background="#FFFFB354" Click="leaveButton_Click"/> + <Button x:Name="deleteButton" Grid.Column="0" Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Background="#FFFFB354" Click="deleteButton_Click"/> + <CheckBox x:Name="connectedCheckBox" Grid.Column="2" Content="Connected" HorizontalAlignment="Right" VerticalAlignment="Center" Checked="connectedCheckBox_Checked" Unchecked="connectedCheckbox_Unchecked"/> </Grid> </Grid> </Border> diff --git a/windows/WinUI/NetworkInfoView.xaml.cs b/windows/WinUI/NetworkInfoView.xaml.cs index ed4cc628..d58ba20a 100644 --- a/windows/WinUI/NetworkInfoView.xaml.cs +++ b/windows/WinUI/NetworkInfoView.xaml.cs @@ -79,6 +79,8 @@ namespace WinUI this.allowDefault.IsChecked = network.AllowDefault; this.allowGlobal.IsChecked = network.AllowGlobal; this.allowManaged.IsChecked = network.AllowManaged; + + this.connectedCheckBox.IsChecked = network.IsConnected; } public bool HasNetwork(ZeroTierNetwork network) @@ -96,9 +98,10 @@ namespace WinUI UpdateNetworkData(); } - private void leaveButton_Click(object sender, RoutedEventArgs e) + private void deleteButton_Click(object sender, RoutedEventArgs e) { APIHandler.Instance.LeaveNetwork(network.NetworkId); + NetworkMonitor.Instance.RemoveNetwork(network.NetworkId); } private void AllowManaged_CheckStateChanged(object sender, RoutedEventArgs e) @@ -127,5 +130,31 @@ namespace WinUI allowGlobal.IsChecked ?? false, allowDefault.IsChecked ?? false); } + + private void connectedCheckBox_Checked(object sender, RoutedEventArgs e) + { + onConnectedCheckboxUpdated(true); + } + + private void connectedCheckbox_Unchecked(object sender, RoutedEventArgs e) + { + onConnectedCheckboxUpdated(false); + } + + private void onConnectedCheckboxUpdated(bool isChecked) + { + if (isChecked) + { + bool global = allowGlobal.IsChecked.Value; + bool managed = allowManaged.IsChecked.Value; + bool defRoute = allowDefault.IsChecked.Value; + + APIHandler.Instance.JoinNetwork(networkId.Text, managed, global, defRoute); + } + else + { + APIHandler.Instance.LeaveNetwork(networkId.Text); + } + } } } diff --git a/windows/WinUI/NetworkListView.xaml b/windows/WinUI/NetworkListView.xaml index a1e74531..30a3e257 100644 --- a/windows/WinUI/NetworkListView.xaml +++ b/windows/WinUI/NetworkListView.xaml @@ -75,60 +75,14 @@ </Window.Resources> <DockPanel> - <StatusBar DockPanel.Dock="Bottom" Height="26" Background="#FF234447" Margin="0"> - <StatusBar.ItemsPanel> - <ItemsPanelTemplate> - <Grid> - <Grid.RowDefinitions> - <RowDefinition Height="*"/> - </Grid.RowDefinitions> - <Grid.ColumnDefinitions> - <ColumnDefinition Width="Auto"/> - <ColumnDefinition Width="Auto"/> - <ColumnDefinition Width="Auto"/> - <ColumnDefinition Width="*"/> - <ColumnDefinition Width="Auto"/> - <ColumnDefinition Width="Auto"/> - </Grid.ColumnDefinitions> - </Grid> - </ItemsPanelTemplate> - </StatusBar.ItemsPanel> - <StatusBarItem Grid.Column="0" x:Name="networkId_placeholder"> - <TextBox x:Name="networkId" Text="deadbeef00" HorizontalAlignment="Left" Grid.Column="0" Foreground="White" FontFamily="Lucida Console" BorderThickness="0" IsReadOnly="true" Background="Transparent"/> - </StatusBarItem> - <StatusBarItem Grid.Column="1" x:Name="onlineStatus" Content="ONLINE" Foreground="White" FontFamily="Lucida Console"/> - <StatusBarItem Grid.Column="2" x:Name="versionString" Content="1.0.5" Foreground="White" FontFamily="Lucida Console"/> - <StatusBarItem Grid.Column="3" x:Name="blank" Content="" Height="43" Foreground="White"/> - <StatusBarItem Grid.Column="4"> - <TextBox x:Name="joinNetworkID" TextWrapping="Wrap" Width="140" HorizontalAlignment="Right" ToolTip="Enter Network ID" PreviewTextInput="OnNetworkEntered" MaxLength="16" FontFamily="Lucida Console" FontSize="12" BorderThickness="1"/> - </StatusBarItem> - <StatusBarItem Grid.Column="5" x:Name="statusBarButton" Foreground="White" RenderTransformOrigin="0.789,0.442"> - <Button x:Name="joinButton" Content="Join" Background="#FFFFB354" Width="76" Click="joinButton_Click"/> - </StatusBarItem> - </StatusBar> - <!--<TabControl Margin="0,0,0,0"> - <TabItem x:Name="Networks" Header="Networks" Foreground="White" IsSelected="True" IsManipulationEnabled="True">--> - <Grid Background="LightGray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> - <Grid.ColumnDefinitions> - <ColumnDefinition Width="*"/> - </Grid.ColumnDefinitions> - <Grid.RowDefinitions> - <RowDefinition Height="*"/> - </Grid.RowDefinitions> - <local:NetworksPage x:Name="networksPage" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="0" Margin="0,0,0,0"/> - </Grid> - <!--</TabItem>--> - <!--<TabItem x:Name="Peers" Header="Peers" Foreground="White"> - <Grid Background="#FFE5E5E5" HorizontalAlignment="Left" VerticalAlignment="Top"> - <Grid.ColumnDefinitions> - <ColumnDefinition Width="*"/> - </Grid.ColumnDefinitions> - <Grid.RowDefinitions> - <RowDefinition Height="*"/> - </Grid.RowDefinitions> - <local:PeersPage x:Name="peersPage" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="0" Grid.Row="0"/> - </Grid> - </TabItem>--> - <!--</TabControl>--> + <Grid Background="LightGray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> + <Grid.ColumnDefinitions> + <ColumnDefinition Width="*"/> + </Grid.ColumnDefinitions> + <Grid.RowDefinitions> + <RowDefinition Height="*"/> + </Grid.RowDefinitions> + <local:NetworksPage x:Name="networksPage" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="0" Margin="0,0,0,0"/> + </Grid> </DockPanel> </Window> diff --git a/windows/WinUI/NetworkListView.xaml.cs b/windows/WinUI/NetworkListView.xaml.cs index c89d4bcc..586f5dc2 100644 --- a/windows/WinUI/NetworkListView.xaml.cs +++ b/windows/WinUI/NetworkListView.xaml.cs @@ -28,69 +28,22 @@ namespace WinUI 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(); - APIHandler.Instance.GetStatus(updateStatus); - - if (!connected) - { - MessageBox.Show("Unable to connect to ZerOTier Service"); - return; - } - - APIHandler.Instance.GetNetworks(updateNetworks); - - DataObject.AddPastingHandler(joinNetworkID, OnPaste); - - timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer); - timer.Interval = 2000; - timer.Enabled = true; + Closed += onClosed; - + NetworkMonitor.Instance.SubscribeNetworkUpdates(updateNetworks); } - private void updateStatus(ZeroTierStatus status) + ~NetworkListView() { - if (status != null) - { - connected = true; - - networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => - { - this.networkId.Text = 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.Text = ""; - })); - versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => - { - this.versionString.Content = "0"; - })); - onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => - { - this.onlineStatus.Content = "OFFLINE"; - })); - } + private void onClosed(object sender, System.EventArgs e) + { + NetworkMonitor.Instance.UnsubscribeNetworkUpdates(updateNetworks); } private void updateNetworks(List<ZeroTierNetwork> networks) @@ -104,24 +57,6 @@ namespace WinUI } } - private void OnUpdateTimer(object source, ElapsedEventArgs e) - { - APIHandler.Instance.GetStatus(updateStatus); - APIHandler.Instance.GetNetworks(updateNetworks); - } - - 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); diff --git a/windows/WinUI/NetworkMonitor.cs b/windows/WinUI/NetworkMonitor.cs index de5543f6..7743c5b9 100644 --- a/windows/WinUI/NetworkMonitor.cs +++ b/windows/WinUI/NetworkMonitor.cs @@ -68,9 +68,13 @@ namespace WinUI { var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); netList = (List<ZeroTierNetwork>)bformatter.Deserialize(stream); + stream.Close(); } - _knownNetworks = netList; + lock (_knownNetworks) + { + _knownNetworks = netList; + } } } @@ -86,28 +90,36 @@ namespace WinUI using (Stream stream = File.Open(dataFile, FileMode.OpenOrCreate)) { - var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - bformatter.Serialize(stream, _knownNetworks); + lock (_knownNetworks) + { + var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + bformatter.Serialize(stream, _knownNetworks); + stream.Flush(); + stream.Close(); + } } } private void apiNetworkCallback(List<ZeroTierNetwork> networks) { - _knownNetworks = _knownNetworks.Union(networks, new NetworkEqualityComparer()).ToList(); - - foreach (ZeroTierNetwork n in _knownNetworks) + lock (_knownNetworks) { - if (networks.Contains(n)) - { - n.IsConnected = true; - } - else + _knownNetworks = _knownNetworks.Union(networks, new NetworkEqualityComparer()).ToList(); + + foreach (ZeroTierNetwork n in _knownNetworks) { - n.IsConnected = false; + if (networks.Contains(n)) + { + n.IsConnected = true; + } + else + { + n.IsConnected = false; + } } - } - _nwCb(_knownNetworks); + _nwCb(_knownNetworks); + } writeNetworks(); } @@ -136,7 +148,7 @@ namespace WinUI } catch { - + Console.WriteLine("Monitor Thread Ended"); } } @@ -159,5 +171,21 @@ namespace WinUI { _nwCb -= cb; } + + public void RemoveNetwork(String networkID) + { + lock(_knownNetworks) + { + foreach (ZeroTierNetwork n in _knownNetworks) + { + if (n.NetworkId.Equals(networkID)) + { + _knownNetworks.Remove(n); + writeNetworks(); + break; + } + } + } + } } } |