tbf/TestBenchFramework/BenchControl/Network/Adapter/NetadapterCfgCtrl.cs

156 lines
3.9 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows.Forms;
using log4net;
using TBF.BenchControl.Generic;
using System.IO;
namespace TBF.BenchControl.Network.Adapter
{
public partial class NetadapterCfgCtrl : UserControl, IComponentCfgCtrl
{
static readonly ILog log = LogManager.GetLogger(typeof(NetadapterCfgCtrl));
public bool ShowMore { get { return false; } }
NetadapterCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as NetadapterCfg;
Redraw();
}
}
public NetadapterCfgCtrl()
{
InitializeComponent();
}
private void EntryFormCfgCtrl_Load(object sender, EventArgs e)
{
if (ParentForm == null) return; /// Return is executed when tab page is open in Designer
if (config == null) return; /// Control was not loaded, settings were not changed
Localize();
Redraw();
}
void Redraw()
{
classNameLabel.Text = config.Factory.ClassName;
nameTextBox.Text = config.Name;
tftpEnabledCheckBox.Checked = config.TftpServerEnabled;
tftpDirectoryTextBox.Text = config.TftpServerDirectory;
AdapterInfo.RefreshNetAdaptersInfo();
IList<AdapterInfo> adapters = AdapterInfo.NetAdapters;
string cfgAdapter = string.Empty;
foreach (AdapterInfo ai in adapters)
{
string record;
if (ai.IPAddress == null)
{
/// This happens with network adapters that currently have no IP address,
/// for instance not connected wireless or dial-up adapters
record = "???.???.???.???";
}
else
{
/// Do not consider IPv6 adapters as well as "Any", "Broadcast", "Loopback" or "None" addresses
if ((ai.IPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) ||
ai.IPAddress.Equals(IPAddress.Any) ||
ai.IPAddress.Equals(IPAddress.Broadcast) ||
ai.IPAddress.Equals(IPAddress.IPv6Any) ||
ai.IPAddress.Equals(IPAddress.IPv6Loopback) ||
ai.IPAddress.Equals(IPAddress.IPv6None) ||
ai.IPAddress.Equals(IPAddress.Loopback) ||
ai.IPAddress.Equals(IPAddress.None))
{
continue;
}
record = ai.IPAddress.ToString();
}
record += " - " + ai.Description;
int i = adapterComboBox.Items.Add(record);
if (ai.Description == config.Description)
{
/// Select the adapter currently in the configuration
adapterComboBox.SelectedIndex = i;
}
}
/// Select the first one if the adapter from the configuration does not exist on the system
if (adapterComboBox.SelectedIndex < 0 && adapterComboBox.Items.Count > 0)
{
adapterComboBox.SelectedIndex = 0;
}
}
void Localize()
{
}
public void Closing()
{
}
public void Unlock()
{
nameTextBox.Enabled = true;
adapterComboBox.Enabled = true;
tftpEnabledCheckBox.Enabled = true;
tftpDirectoryTextBox.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
if (!adapterComboBox.Items.Contains(adapterComboBox.Text))
{
flags = CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid 'Parent Name'";
}
if (tftpEnabledCheckBox.Checked && !Directory.Exists(tftpDirectoryTextBox.Text))
{
flags = CfgUpdateFlags.Error;
message += Environment.NewLine + "TFTP Directory does not exist";
}
return flags;
}
public CfgUpdateFlags UpdateCfg()
{
CfgUpdateFlags flags = CfgUpdateFlags.RestartRqrd;
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
config.Name = nameTextBox.Text;
/// Network adapter
string strAdapter = adapterComboBox.Text;
int iDash = strAdapter.IndexOf(" - ");
config.Description = iDash < 0 ? "" : strAdapter.Substring(iDash + 3);
config.TftpServerEnabled = tftpEnabledCheckBox.Checked;
config.TftpServerDirectory = tftpDirectoryTextBox.Text;
return flags;
}
}
}