/// <summary> /// Given a Path, attempts to read the file from the specified path /// </summary> /// <param name="path"></param> public void LoadFromFile(string path, FileMode fileMode) { try { using (BinaryReader reader = new BinaryReader(File.Open(@path, FileMode.OpenOrCreate, FileAccess.ReadWrite))) { // Header int fileVersion = reader.ReadInt32(); int devicesCount = reader.ReadInt32(); // Begin the Read for (int index = 0; index < devicesCount; index++) { Device.Attributes attributes = new Device.Attributes(); IPAddress ip = new IPAddress(reader.ReadInt32()); attributes.StringAddress = ip.ToString(); attributes.PingInterval = reader.ReadInt32(); attributes.PingTimeout = reader.ReadInt32(); attributes.PingTimeToLive = reader.ReadInt32(); DeviceList.Add(new Device(attributes)); } reader.Close(); } } catch (IOException exception) { Console.WriteLine(Tag + ": " + exception.Message); } }
/// <summary> /// Default Constructor. Uses the Device Attributes Parameter /// to bind the views with the state of the given Device attributes. /// </summary> /// <param name="attributes"></param> public PingSettings(Device.Attributes attributes) { InitializeComponent(); _attributes = attributes; Bind(_attributes); }
/// <summary> /// Binds the given Device.Attibutes instance to the view /// </summary> /// <param name="attributes"></param> protected void Bind(Device.Attributes attributes) { if (attributes != null) { IPAddressLabel.Content = attributes.StringAddress; TTLLabel.Content = attributes.PingTimeToLive.ToString(); TimeoutLabel.Content = MillisecondsToTimeString(attributes.PingTimeout); IntervalLabel.Content = MillisecondsToTimeString(attributes.PingInterval); } }
/// <summary> /// Writes the appropriate values for each of the current devices in the analysis /// </summary> /// <param name="writer"></param> protected void WriteDevices(BinaryWriter writer) { try { if (_deviceList != null) { foreach (Device device in _deviceList) { if (device != null) { Device.Attributes attributes = device.CurrentAttributes; // Address int address = EntryWindow.ReverseIntBytes(EntryWindow.IPIntRepresentation(attributes.StringAddress)); writer.Write(address); // Ping Interval if (attributes.PingInterval > 0) { writer.Write(attributes.PingInterval); } else { writer.Write(Device.Attributes.DefaultPingInterval); } // Ping Timeout if (attributes.PingTimeout > 0) { writer.Write(attributes.PingTimeout); } else { writer.Write(Device.Attributes.DefaultTimeout); } // Ping TTL if (attributes.PingTimeToLive > 0) { writer.Write(attributes.PingTimeToLive); } else { writer.Write(Device.Attributes.DefaultTimeToLive); } } } } } catch (IOException exception) { Console.WriteLine(Tag + ": " + exception.Message); } }
/// <summary> /// Raised when the PingSettings Window has changed the attributes. /// Binds the current Attribute Set after mutating the state of the current attributes /// </summary> /// <param name="sender"></param> /// <param name="eventArguments"></param> protected virtual void SettingChanged(object sender, EventArgs eventArguments) { try { PingSettings settingsWindow = (PingSettings)sender; _attributes = settingsWindow.Attributes; Bind(_attributes); settingsWindow.Closed -= SettingChanged; }catch (Exception exception) { Console.WriteLine(exception.Message); } }
/// <summary> /// Default Constructor. Accepts a set of Attributes as the Parameter. /// If the Given Attribute Set is Null, a new set is created /// </summary> /// <param name="attributes"></param> public Device(Device.Attributes attributes) { if (attributes != null) { CurrentAttributes = attributes; } else { CurrentAttributes = new Attributes(); } SetDefaults(); }
/// <summary> /// Initializes the MEmber Variables to their default state /// </summary> /// <param name="address"></param> protected void SetDefaults(IPAddress address) { _attributes = new Device.Attributes(); if (address == null) { _attributes.StringAddress = DefaultIPAddress.ToString(); } else { _attributes.StringAddress = address.ToString(); } }
/// <summary> /// Extracts the Attributes from the TextBoxes /// </summary> /// <param name="attributes"></param> protected void ExtractAttributesTo(Device.Attributes attributes) { if (attributes != null) { attributes.PingTimeout = ParseNumber(TimeoutInput.Text); attributes.PingTimeToLive = ParseNumber(TTLInput.Text); int milliseconds = 0; int buffer = 0; buffer = ParseNumber(IntervalDayInput.Text) * 24; buffer = (ParseNumber(IntervalHourInput.Text) + buffer) * 60; buffer = (ParseNumber(IntervalMinuteInput.Text) + buffer) * 60; buffer = (ParseNumber(IntervalSecondsInput.Text) + buffer) * 1000; milliseconds = buffer; attributes.PingInterval = milliseconds; } }
/// <summary> /// Binds the Window TextBoxes to the attribute values /// </summary> /// <param name="attributes"></param> protected void Bind(Device.Attributes attributes) { if (attributes != null) { TimeoutInput.Text = attributes.PingTimeout.ToString(); TTLInput.Text = attributes.PingTimeToLive.ToString(); int timeout = attributes.PingInterval; timeout /= 1000; IntervalSecondsInput.Text = (timeout % 60).ToString(); timeout /= 60; IntervalMinuteInput.Text = (timeout % 60).ToString(); timeout /= 60; IntervalHourInput.Text = (timeout % 24).ToString(); timeout /= 24; IntervalDayInput.Text = timeout.ToString(); } }
/// <summary> /// Removes the handle from the appropriate members /// and releases any additional resources /// </summary> public void Dispose() { _attributes = null; }