85 lines
2.0 KiB
C#
85 lines
2.0 KiB
C#
///
|
|
/// Copyright (c) 2017 Sensus Metering Systems
|
|
///
|
|
using System.Net;
|
|
using log4net;
|
|
|
|
namespace TBF.BenchControl.Network.Adapter
|
|
{
|
|
public class Netadapter : ComponentBase, TBF.BenchControl.Generic.IDevice, GenericDevices.INetworkAdapter
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Netadapter));
|
|
public override string ToString()
|
|
{
|
|
return string.Format("Component={0} IPAddress={1} NetMask={2} Description={3}", Cfg.Name, IPAddress, NetMask, (Cfg as NetadapterCfg).Description);
|
|
}
|
|
|
|
public bool Running { get { return running; } }
|
|
bool running;
|
|
|
|
readonly NetadapterCfg netadapterCfg;
|
|
readonly IPAddress ipAddress;
|
|
readonly IPAddress netMask;
|
|
readonly IPAddress broadcastAddress;
|
|
|
|
public IPAddress IPAddress { get { return ipAddress; } }
|
|
public IPAddress NetMask { get { return netMask; } }
|
|
public IPAddress BroadcastAddress { get { return broadcastAddress; } }
|
|
|
|
Tftp.TftpServer tftpServer;
|
|
|
|
public Netadapter()
|
|
{
|
|
}
|
|
|
|
public Netadapter(Generic.IComponentCfg cfg)
|
|
: base(cfg)
|
|
{
|
|
netadapterCfg = cfg as NetadapterCfg;
|
|
AdapterInfo.RefreshNetAdaptersInfo();
|
|
AdapterInfo netadapter = AdapterInfo.GetNetAdapter(netadapterCfg.Description);
|
|
ipAddress = netadapter.IPAddress;
|
|
netMask = netadapter.NetMask;
|
|
broadcastAddress = netadapter.GetBroadcastAddress();
|
|
|
|
running = false;
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
|
|
public void Initialize()
|
|
{
|
|
if (netadapterCfg.DebugLevel != Config.Entities.DebugMode.Simulate)
|
|
{
|
|
if (netadapterCfg.TftpServerEnabled)
|
|
{
|
|
tftpServer = new Tftp.TftpServer();
|
|
tftpServer.Start(netadapterCfg.TftpServerDirectory, ipAddress);
|
|
}
|
|
else
|
|
{
|
|
tftpServer = null;
|
|
}
|
|
}
|
|
|
|
running = true;
|
|
|
|
log.FatalFormat("Successfully initialized device {0}", ToString());
|
|
}
|
|
|
|
public void StopDevice()
|
|
{
|
|
if (netadapterCfg.DebugLevel != Config.Entities.DebugMode.Simulate)
|
|
{
|
|
if (tftpServer != null) tftpServer.Stop();
|
|
}
|
|
|
|
running = false;
|
|
}
|
|
|
|
public void RunDeviceBefore() {}
|
|
public void RunDeviceAfter() {}
|
|
}
|
|
}
|