Lang nichts mehr passiert, refactoring ist geil

This commit is contained in:
2014-07-11 14:38:10 +00:00
parent f019b4e493
commit cd21034b41
62 changed files with 2132 additions and 561 deletions
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetMonitorUtils.Execptions
{
[Serializable()]
class ServiceControlArgumentException : System.Exception
{
public ServiceControlArgumentException() : base() { }
public ServiceControlArgumentException(string message) : base(message) { }
public ServiceControlArgumentException(string message, System.Exception inner) : base(message, inner) { }
protected ServiceControlArgumentException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetMonitorUtils.Execptions
{
[Serializable()]
class ServiceControlException : System.Exception
{
public ServiceControlException() : base() { }
public ServiceControlException(string message) : base(message) { }
public ServiceControlException(string message, System.Exception inner) : base(message, inner) { }
protected ServiceControlException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetMonitorUtils.Execptions
{
[Serializable()]
class ServiceControlNetworkException : System.Exception
{
public ServiceControlNetworkException() : base() { }
public ServiceControlNetworkException(string message) : base(message) { }
public ServiceControlNetworkException(string message, System.Exception inner) : base(message, inner) { }
protected ServiceControlNetworkException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
}
}
+64
View File
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{45EE003D-5572-48FE-B90E-6BFE18D9EEC8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NetMonitorUtils</RootNamespace>
<AssemblyName>NetMonitorUtils</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Execptions\ServiceControlNetworkException.cs" />
<Compile Include="Execptions\ServiceControlArgumentException.cs" />
<Compile Include="Execptions\ServiceControlException.cs" />
<Compile Include="NetworkSetter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServiceControl.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Utils\Utils\Utils.csproj">
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
<Name>Utils</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+182
View File
@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlubbFish.Utils;
namespace NetMonitorUtils
{
public class NetworkSetter : OwnObject
{
private ServiceControl serviceController;
private InIReader networkini;
private enum netsetterType
{
Auto,
IpAuto,
Ip,
DnsAuto,
Dns,
WinsAuto,
Wins
}
public NetworkSetter(string networkfile, string configfile)
{
this.addLog("NetworkSetter", "networkfile: " + networkfile + "; configfile: " + configfile + ";", LogLevel.Debug);
this.serviceController = ServiceControl.getInstance(configfile);
this.networkini = InIReader.getInstance(networkfile);
}
public bool setNetwork(string networkName)
{
this.addLog("setNetwork", "Setze Netzwerkeinstellungen: " + networkName, LogLevel.Info);
this.addLog("setNetwork", "networkName: " + networkName + ";", LogLevel.Debug);
String name = networkini.getValue(networkName, "Name");
String ip = networkini.getValue(networkName, "Ip");
String subnet = networkini.getValue(networkName, "Subnet");
String gateway = networkini.getValue(networkName, "Gateway");
String dns = networkini.getValue(networkName, "Dns");
String wins = networkini.getValue(networkName, "Wins");
String adapter = networkini.getValue(networkName, "Adapter");
this.addLog("setNetwork", "name: " + name, LogLevel.Debug);
this.addLog("setNetwork", "ip: " + ip, LogLevel.Debug);
this.addLog("setNetwork", "subnet: " + subnet, LogLevel.Debug);
this.addLog("setNetwork", "gateway: " + gateway, LogLevel.Debug);
this.addLog("setNetwork", "dns: " + dns, LogLevel.Debug);
this.addLog("setNetwork", "wins: " + wins, LogLevel.Debug);
this.addLog("setNetwork", "adapter: " + adapter, LogLevel.Debug);
bool ret = false;
if (ip.ToLower() == "auto" && dns.ToLower() == "auto" && wins.ToLower() == "auto")
{
ret = setNetworkAuto(adapter, name);
this.addLog("setNetwork.setNetworkAuto", "return " + ret, LogLevel.Debug);
}
else
{
if (ip.ToLower() == "auto")
{
ret = setNetworkIpAuto(adapter, name);
this.addLog("setNetwork.setNetworkIpAuto", "return " + ret, LogLevel.Debug);
}
else
{
ret = setNetworkIp(ip, subnet, gateway, adapter, name);
this.addLog("setNetwork.setNetworkIp", "return " + ret, LogLevel.Debug);
}
if (dns.ToLower() == "auto")
{
ret = setNetworkDnsAuto(adapter, name);
this.addLog("setNetwork.setNetworkDnsAuto", "return " + ret, LogLevel.Debug);
}
else
{
ret = setNetworkDns(dns, adapter, name);
this.addLog("setNetwork.setNetworkDns", "return " + ret, LogLevel.Debug);
}
if (wins.ToLower() == "auto")
{
ret = setNetworkWinsAuto(adapter, name);
this.addLog("setNetwork.setNetworkWinsAuto", "return " + ret, LogLevel.Debug);
}
else
{
ret = setNetworkWins(wins, adapter, name);
this.addLog("setNetwork.setNetworkWins", "return " + ret, LogLevel.Debug);
}
}
this.addLog("setNetwork", "return " + ret, LogLevel.Debug);
return ret;
}
public bool setNetworkAuto(string adapter, string name)
{
this.addLog("setNetworkAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Automatisch", LogLevel.Info);
this.addLog("setNetworkAuto", "datapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
return setNetwork(netsetterType.Auto, null, null, null, adapter, name, null, null);
}
public bool setNetworkIpAuto(string adapter, string name)
{
this.addLog("setNetworkIpAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf IP Automatisch", LogLevel.Info);
this.addLog("setNetworkIpAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
return setNetwork(netsetterType.IpAuto, null, null, null, adapter, name, null, null);
}
public bool setNetworkIp(string ip, string subnet, string gateway, string adapter, string name)
{
this.addLog("setNetworkIp", "Setze Netzwerkeinstellungen von " + adapter + " auf IP " + ip + "/" + subnet + " GW " + gateway, LogLevel.Info);
this.addLog("setNetworkIp", "ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
return setNetwork(netsetterType.Ip, ip, subnet, gateway, adapter, name, null, null);
}
public bool setNetworkDnsAuto(string adapter, string name)
{
this.addLog("setNetworkDnsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns Automatisch", LogLevel.Info);
this.addLog("setNetworkDnsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
return setNetwork(netsetterType.DnsAuto, null, null, null, adapter, name, null, null);
}
public bool setNetworkDns(string dns, string adapter, string name)
{
this.addLog("setNetworkDns", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns " + dns, LogLevel.Info);
this.addLog("setNetworkDns", "dns: " + dns + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
return setNetwork(netsetterType.Dns, null, null, null, adapter, name, dns, null);
}
public bool setNetworkWinsAuto(string adapter, string name)
{
this.addLog("setNetworkWinsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins Automatisch", LogLevel.Info);
this.addLog("setNetworkWinsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
return setNetwork(netsetterType.WinsAuto, null, null, null, adapter, name, null, null);
}
public bool setNetworkWins(string wins, string adapter, string name)
{
this.addLog("setNetworkWins", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins " + wins, LogLevel.Info);
this.addLog("setNetworkWins", "wins: " + wins + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
return setNetwork(netsetterType.Wins, null, null, null, adapter, name, null, wins);
}
private bool setNetwork(netsetterType option, string ip, string subnet, string gateway, string adapter, string name, string dns, string wins)
{
this.addLog("setNetwork", "option: " + option + "; ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + "; dns: " + dns + "; wins: " + wins + ";", LogLevel.Debug);
bool status = false;
try
{
if (option == netsetterType.Ip)
{
this.addLog("ServiceController.NetworkSetIp", "ip: "+ip+"; subnet: "+subnet+"; gateway: "+gateway+"; adapter: "+adapter+";", LogLevel.Debug);
status = this.serviceController.NetworkSetIp(ip, subnet, gateway, adapter);
}
if (option == netsetterType.Auto)
{
this.addLog("ServiceController.NetworkSetAuto", "adapter: "+adapter+";", LogLevel.Debug);
status = this.serviceController.NetworkSetAuto(adapter);
}
if (option == netsetterType.IpAuto)
{
this.addLog("ServiceController.NetworkSetIpAuto", "adapter: "+adapter+";", LogLevel.Debug);
status = this.serviceController.NetworkSetIpAuto(adapter);
}
if (option == netsetterType.DnsAuto)
{
this.addLog("ServiceController.NetworkSetDnsAuto", "adapter: "+adapter+";", LogLevel.Debug);
status = this.serviceController.NetworkSetDnsAuto(adapter);
}
if (option == netsetterType.WinsAuto)
{
this.addLog("ServiceController.NetworkSetWinsAuto", "adapter: "+adapter+";", LogLevel.Debug);
status = this.serviceController.NetworkSetWinsAuto(adapter);
}
if (option == netsetterType.Dns)
{
this.addLog("ServiceController.NetworkSetDNS", "dns: "+dns+"; adapter: "+adapter+";", LogLevel.Debug);
status = this.serviceController.NetworkSetDNS(dns, adapter);
}
if (option == netsetterType.Wins)
{
this.addLog("ServiceController.NetworkSetWINS", "wins: "+wins+"; adapter: "+adapter+";", LogLevel.Debug);
status = this.serviceController.NetworkSetWINS(wins, adapter);
}
}
catch (Exception e)
{
this.addLog("setNetwork", "Fehler beim Ausführen des Befehls: " + e.Message, LogLevel.Error);
throw e;
}
return status;
}
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die mit einer Assembly verknüpft sind.
[assembly: AssemblyTitle("NetMonitorUtils")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NetMonitorUtils")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("2c86a911-78de-4ced-a0fe-d63cd945c0ae")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+227
View File
@@ -0,0 +1,227 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Net.Sockets;
using System.Net;
using BlubbFish.Utils;
using NetMonitorUtils.Execptions;
namespace NetMonitorUtils
{
public class ServiceControl
{
private ServiceController sc = new ServiceController();
private static ServiceControl instance;
private InIReader config;
private ServiceControl(string configfile)
{
this.sc.ServiceName = "NetMonitorServer";
this.config = InIReader.getInstance(configfile);
}
public static ServiceControl getInstance(string configfile)
{
if (instance == null)
{
instance = new ServiceControl(configfile);
}
return instance;
}
private bool ServiceSetStatus(ServiceControllerStatus status)
{
if (this.sc.Status == status)
{
return true;
}
this.sc.Start();
try
{
this.sc.WaitForStatus(status, new TimeSpan(0, 0, 30));
}
catch (System.ServiceProcess.TimeoutException)
{
return false;
}
return true;
}
public bool ServiceStart()
{
return this.ServiceSetStatus(ServiceControllerStatus.Running);
}
public bool ServicePause()
{
return this.ServiceSetStatus(ServiceControllerStatus.Paused);
}
public bool ServiceStop()
{
return this.ServiceSetStatus(ServiceControllerStatus.Stopped);
}
public bool NetworkSetAuto(string adapter)
{
if (adapter == null)
{
throw new ServiceControlArgumentException("Adapter ist not Set!");
}
return this.sendMessage("NET","AUTO "+adapter);
}
private bool sendMessage(string method, string message)
{
IPEndPoint netRecieverPoint = new IPEndPoint(IPAddress.Loopback, 0);
int startPort = Int32.Parse(this.config.getValue("ports", "client_from"));
int rangePort = Int32.Parse(this.config.getValue("ports", "client_to"));
UdpClient netReciver = null;
int selectedPort = 0;
for (int i = startPort; i < rangePort; i++)
{
try
{
netReciver = new UdpClient(i);
}
catch (System.Net.Sockets.SocketException) { }
if (netReciver != null)
{
selectedPort = i;
break;
}
}
if (netReciver == null)
{
throw new ServiceControlException("No Free Port for Comunication");
}
byte[] data = Encoding.UTF8.GetBytes(method + " " + selectedPort + " " + message);
UdpClient netSender = new UdpClient();
IPEndPoint netSenderPoint = new IPEndPoint(IPAddress.Loopback, Int32.Parse(this.config.getValue("ports", "server")));
netSender.Send(data, data.Length, netSenderPoint);
netReciver.Client.ReceiveTimeout = 60000;
byte[] recivedData;
try
{
recivedData = netReciver.Receive(ref netRecieverPoint);
}
catch (Exception e)
{
throw new ServiceControlNetworkException(e.Message);
}
string recieveText = Encoding.UTF8.GetString(recivedData);
if (recieveText.ToLower() == "true")
{
return true;
}
else
{
throw new ServiceControlException(recieveText);
}
}
public bool NetworkSetIp(string ip, string subnet, string gateway, string adapter)
{
if (ip == null)
{
throw new ServiceControlArgumentException("IP address not Set!");
}
if (subnet == null)
{
throw new ServiceControlArgumentException("Subnet address not Set!");
}
if (gateway == null)
{
throw new ServiceControlArgumentException("Gateway address not Set!");
}
if (adapter == null)
{
throw new ServiceControlArgumentException("Adapter ist not Set!");
}
return this.sendMessage("NET", "IP " + ip + " " + subnet + " " + gateway + " " + adapter);
}
public bool NetworkSetDNS(string dns, string adapter)
{
if (dns == null)
{
throw new ServiceControlArgumentException("DNS address not Set!");
}
if (adapter == null)
{
throw new ServiceControlArgumentException("Adapter ist not Set!");
}
return this.sendMessage("NET", "DNS " + dns + " " + adapter);
}
public bool NetworkSetWINS(string wins, string adapter)
{
if (wins == null)
{
throw new ServiceControlArgumentException("WINS address not Set!");
}
if (adapter == null)
{
throw new ServiceControlArgumentException("Adapter ist not Set!");
}
return this.sendMessage("NET", "WINS " + wins + " " + adapter);
}
public bool NetworkSetIpAuto(string adapter)
{
if (adapter == null)
{
throw new ServiceControlArgumentException("Adapter ist not Set!");
}
return this.sendMessage("NET", "IP AUTO "+adapter);
}
public bool NetworkSetDnsAuto(string adapter)
{
if (adapter == null)
{
throw new ServiceControlArgumentException("Adapter ist not Set!");
}
return this.sendMessage("NET", "DNS AUTO " + adapter);
}
public bool NetworkSetWinsAuto(string adapter)
{
if (adapter == null)
{
throw new ServiceControlArgumentException("Adapter ist not Set!");
}
return this.sendMessage("NET", "WINS AUTO " + adapter);
}
public bool ServiceStart(string name)
{
if (name == null)
{
throw new ServiceControlArgumentException("Service name ist not set!");
}
return this.sendMessage("SERVICE", "START " + name);
}
public bool ServiceStop(string name)
{
if (name == null)
{
throw new ServiceControlArgumentException("Service name ist not set!");
}
return this.sendMessage("SERVICE", "STOP " + name);
}
public bool ServiceStatus(string name)
{
if (name == null)
{
throw new ServiceControlArgumentException("Service name ist not set!");
}
return this.sendMessage("SERVICE", "STATUS " + name);
}
}
}