using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation; using System.Drawing; using System.Xml; using System.Xml.Schema; using System.IO; namespace WoL_Manager { class Host { private string name; private IPAddress ip; private string mac_address; private IPAddress broadcast; private string description; private int status; private PingThread ping; private PingReply lastPingReply; private System.DateTime wakeTime; private static MainWindow parent = null; private static Icon[] iconList = null; private static List hostList = new List(); private Host(string name, IPAddress ip, string mac, IPAddress broadcast, string description) { this.name = name; this.ip = ip; this.mac_address = mac; this.broadcast = broadcast; this.description = description; status = 0; ping = null; lastPingReply = null; wakeTime = DateTime.Now; } ~Host() { if (ping != null) { ping.stopThread(); } } public static void clearList() { hostList.Clear(); } public Icon getStatusIcon() { if (iconList != null && iconList.Length > status) { return iconList[status]; } else { return null; } } public static MainWindow Parent { set { parent = value; } get { return parent; } } public static Icon[] IconList { set { iconList = value; } get { return iconList; } } public PingThread pingThread { set { ping = value; } get { return ping; } } public string getStatusString() { switch (status) { case 0: return "Unknown Status"; case 1: return string.Format("Host online ({0} ms)",lastPingReply.RoundtripTime); case 2: return string.Format("Host not reachable ({0})", lastPingReply.Status); case 3: TimeSpan temp = (TimeSpan)(DateTime.Now - wakeTime); return string.Format("Waking Host (waiting for {0} m {1} s)", temp.Minutes, temp.Seconds); default: return "???"; } } public PingReply LastPingResult { get { return lastPingReply; } set { lastPingReply = value; if (value.Status == IPStatus.Success) { status = 1; } else if (status == 3) { double temp = ((TimeSpan)(DateTime.Now - wakeTime)).TotalSeconds; if (temp > ManagerSettings.WakeWaitTime) { status = 2; } } else if (status != 3) { status = 2; } parent.updateDataView(); } } public bool wakeHost() { bool temp = Magic_Packet.sendPacket(this); if (temp) { status = 3; wakeTime = System.DateTime.Now; } return temp; } private static void validationEventHandler(object sender, ValidationEventArgs args) { valitationResult = false; } private static bool valitationResult = true; public static bool readFromFile(string file) { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; settings.IgnoreProcessingInstructions = true; settings.ProhibitDtd = false; settings.ValidationType = ValidationType.DTD; settings.ValidationEventHandler += new ValidationEventHandler(validationEventHandler); XmlReader reader = XmlReader.Create(file, settings); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.LocalName.CompareTo("host") == 0) { string name; string ip; string bc; string mac; string desc; System.Net.IPAddress ip_adr; System.Net.IPAddress bc_adr; reader.Read(); name = reader.ReadString(); reader.Read(); ip = reader.ReadString(); reader.Read(); bc = reader.ReadString(); reader.Read(); mac = reader.ReadString(); if (!MainWindow.checkMAC(mac)) { reader.Close(); return false; } reader.Read(); desc = reader.ReadString(); try { ip_adr = System.Net.IPAddress.Parse(ip); bc_adr = System.Net.IPAddress.Parse(bc); } catch (Exception) { reader.Close(); return false; } if (valitationResult == false) { reader.Close(); return false; } Host.createHost(name, ip_adr, mac, bc_adr, desc); } } reader.Close(); return true; } public static void createDTD(string file) { string temp = "\n\n\n\n\n\n\n\n"; File.WriteAllText(file, temp); } public static void writeToFile(string file) { Host[] hostList = Host.getHostList(); string temp = "\n\n\n"; if (hostList != null && hostList.Length > 0) { for (int t = 0; t < hostList.Length; t++) { temp = String.Concat(temp, "\n\n", hostList[t].Name, ""); temp = String.Concat(temp, "\n", hostList[t].IP.ToString(), ""); temp = String.Concat(temp, "\n", hostList[t].Broadcast.ToString(), ""); temp = String.Concat(temp, "\n", hostList[t].MAC, ""); temp = String.Concat(temp, "\n", hostList[t].Description, "\n"); } } temp = String.Concat(temp,"\n"); File.WriteAllText(file,temp); } public int Status { get { return status; } set { status = value; } } public string Name { get { return name; } set { name = value; } } public IPAddress IP { get { return ip; } set { ip = value; } } public string MAC { get { return mac_address; } set { mac_address = value; } } public IPAddress Broadcast { get { return broadcast; } set { broadcast = value; } } public string Description { get { return description; } set { description = value; } } public static Host createHost(string name, IPAddress ip, string mac, IPAddress broadcast, string description) { Host temp = new Host(name, ip, mac, broadcast, description); hostList.Add(temp); return temp; } public static bool deleteHost(Host host) { host.pingThread.stopThread(); return hostList.Remove(host); } public static Host[] getHostList() { return hostList.ToArray(); } } }