/// <summary> /// Fires the variable operation event. /// </summary> /// <param name="variable">The variable.</param> /// <param name="eventHandler">The event handler.</param> private void FireVariableLinkOperationEvent(PacketPropertyVariable variable, PacketPropertyVariableLink variableLink, VariableLinkOperationEventHandler eventHandler) { if (null != eventHandler) { eventHandler(variable, variableLink); } }
public void ProcessPacket(Packet packet) { // Cache type of packet Type packetType = packet.GetType(); // Each variable has multiple links variableEntriesList.ForEach(delegate(VariableEntry entry) { // Variable can link only to single property of a packet hence only single check PacketPropertyVariableLink link = GetVariableLinkToPacket(entry.variable, packetType); if (null != link) { // Find first reachable packet property value ObjectReflectionProcessor processor = new ObjectReflectionProcessor(delegate(object o) { // Property value found - save it entry.Value = o; // Always stop iteration return(true); }); processor.IterateObjectTreePathValues(packet, link.PacketClassMemberPath.members, PacketPropertyValueFilterForm.RECURSIVE_SEARCH_DEPTH); } }); }
/// <summary> /// Replaces the variable link. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="oldVariableLink">The old variable link.</param> /// <param name="newVariableLink">The new variable link.</param> /// <returns><code>true</code> if old variable and link exists, <code>false</code> otherwise.</returns> public bool ReplaceVariableLink(string variableName, PacketPropertyVariableLink oldVariableLink, PacketPropertyVariableLink newVariableLink) { bool ret = DoVariableOperation( variableName, // Replace the link delegate(PacketPropertyVariable variable) { // Check if variable is already linked to new packet and that link is not the link we are replacing // The fact that only single packet can be linked to single variable provides needed guarantees bool opRet = !IsVariableLinkedToPacket(variable, newVariableLink.PacketClass.type) || newVariableLink.PacketClass.type == oldVariableLink.PacketClass.type; if (opRet) { int index = variable.Links.IndexOf(oldVariableLink); opRet = (0 <= index); if (opRet) { variable.Links[index] = newVariableLink; } } return(opRet); }, // Fire event on success delegate(PacketPropertyVariable variable) { FireVariableLinkOperationEvent(variable, newVariableLink, VariableLinkReplaced); }) ; return(ret); }
/// <summary> /// Compares this link to another link. /// </summary> /// <param name="packetPropertyVariableLink">The packet property variable link.</param> /// <returns><code>true</code> if all fields are equal, <code><false/code> otherwise.</returns> protected bool Equals(PacketPropertyVariableLink packetPropertyVariableLink) { if (packetPropertyVariableLink == null) { return(false); } return(Equals(packetClass, packetPropertyVariableLink.packetClass) && Equals(packetClassMemberPath, packetPropertyVariableLink.packetClassMemberPath)); }
/// <summary> /// Removes the variable link. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="variableLink">The variable link.</param> /// <returns><code>true</code> if like is removed, <code>false</code> otherwise.</returns> public bool RemoveVariableLink(string variableName, PacketPropertyVariableLink variableLink) { bool ret = DoVariableOperation( variableName, // Add the link delegate(PacketPropertyVariable variable) { return(variable.Links.Remove(variableLink)); }, // Fire event on success delegate(PacketPropertyVariable variable) { FireVariableLinkOperationEvent(variable, variableLink, VariableLinkRemoved); }) ; return(ret); }
/// <summary> /// Initializes a new instance of the <see cref="T:PacketPropertyVariable"/> class. /// </summary> /// <param name="packetClass">The packet class.</param> /// <param name="packetClassMemberPath">The packet class member path.</param> /// <param name="name">The name.</param> public PacketPropertyVariable(PacketPropertyValueFilterForm.PacketClass packetClass, PacketPropertyValueFilterForm.ClassMemberPath packetClassMemberPath, string name) { PacketPropertyVariableLink loc = new PacketPropertyVariableLink(packetClass, packetClassMemberPath); this.links.Add(loc); if (null == name) { name = string.Empty; } this.name = name; }
/// <summary> /// Determines whether variable is linked to packet. /// </summary> /// <param name="variable">The variable.</param> /// <param name="packetType">Type of the packet.</param> /// <returns> /// <c>true</c> if variable is linked to packet; otherwise, <c>false</c>. /// </returns> private bool IsVariableLinkedToPacket(PacketPropertyVariable variable, Type packetType) { bool ret = false; IList <PacketPropertyVariableLink> links = variable.Links; for (int i = links.Count - 1; 0 <= i; --i) { PacketPropertyVariableLink link = links[i]; if (link.PacketClass.type == packetType) { ret = true; } } return(ret); }
/// <summary> /// Gets the variable link to packet. /// </summary> /// <param name="variable">The variable.</param> /// <param name="packetType">Type of the packet.</param> /// <returns>Found variable link, or <code>null</code> otherwise.</returns> private PacketPropertyVariableLink GetVariableLinkToPacket(PacketPropertyVariable variable, Type packetType) { PacketPropertyVariableLink ret = null; // Check every link of a variable IList <PacketPropertyVariableLink> links = variable.Links; for (int i = links.Count - 1; 0 <= i; --i) { PacketPropertyVariableLink link = links[i]; if (link.PacketClass.type.IsAssignableFrom(packetType)) { // Found link - stop the loop ret = link; break; } } return(ret); }
/// <summary> /// Adds the variable link. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="variableLink">The variable link.</param> /// <returns><code>true</code> if link is added successfully, <code>false</code> otherwise.</returns> public bool AddVariableLink(string variableName, PacketPropertyVariableLink variableLink) { bool ret = DoVariableOperation( variableName, // Add the link delegate(PacketPropertyVariable variable) { bool opRet = !IsVariableLinkedToPacket(variable, variableLink.PacketClass.type); if (opRet) { variable.Links.Add(variableLink); } return(opRet); }, // Fire event on success delegate(PacketPropertyVariable variable) { FireVariableLinkOperationEvent(variable, variableLink, VariableLinkAdded); }) ; return(ret); }
/// <summary> /// Compares this link to another link. /// </summary> /// <param name="packetPropertyVariableLink">The packet property variable link.</param> /// <returns><code>true</code> if all fields are equal, <code><false/code> otherwise.</returns> protected bool Equals(PacketPropertyVariableLink packetPropertyVariableLink) { if (packetPropertyVariableLink == null) return false; return Equals(packetClass, packetPropertyVariableLink.packetClass) && Equals(packetClassMemberPath, packetPropertyVariableLink.packetClassMemberPath); }
/// <summary> /// Removes the variable link. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="variableLink">The variable link.</param> /// <returns><code>true</code> if like is removed, <code>false</code> otherwise.</returns> public bool RemoveVariableLink(string variableName, PacketPropertyVariableLink variableLink) { bool ret = DoVariableOperation( variableName, // Add the link delegate(PacketPropertyVariable variable) { return variable.Links.Remove(variableLink); }, // Fire event on success delegate(PacketPropertyVariable variable) { FireVariableLinkOperationEvent(variable, variableLink, VariableLinkRemoved); }) ; return ret; }
/// <summary> /// Replaces the variable link. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="oldVariableLink">The old variable link.</param> /// <param name="newVariableLink">The new variable link.</param> /// <returns><code>true</code> if old variable and link exists, <code>false</code> otherwise.</returns> public bool ReplaceVariableLink(string variableName, PacketPropertyVariableLink oldVariableLink, PacketPropertyVariableLink newVariableLink) { bool ret = DoVariableOperation( variableName, // Replace the link delegate(PacketPropertyVariable variable) { // Check if variable is already linked to new packet and that link is not the link we are replacing // The fact that only single packet can be linked to single variable provides needed guarantees bool opRet = !IsVariableLinkedToPacket(variable, newVariableLink.PacketClass.type) || newVariableLink.PacketClass.type == oldVariableLink.PacketClass.type; if (opRet) { int index = variable.Links.IndexOf(oldVariableLink); opRet = (0 <= index); if (opRet) { variable.Links[index] = newVariableLink; } } return opRet; }, // Fire event on success delegate(PacketPropertyVariable variable) { FireVariableLinkOperationEvent(variable, newVariableLink, VariableLinkReplaced); }) ; return ret; }
/// <summary> /// Variableses the manager_ variable link replaced. /// </summary> /// <param name="variable">The variable.</param> /// <param name="link">The link.</param> private void variablesManager_VariableLinkReplaced(PacketPropertyVariable variable, PacketPropertyVariableLink link) { // Update links listbox only if variable is currently selected (and hence links list contains variables links) if (varListBox.SelectedItem == variable) { // Save current selection int selectedIndex = varPacketsLinksListBox.SelectedIndex; // Clear listbox items varPacketsLinksListBox.Items.Clear(); // Add all variable items ListBoxHelpers.AddRange(varPacketsLinksListBox, variable.Links); // Restore selection index varPacketsLinksListBox.SelectedIndex = selectedIndex; } }
/// <summary> /// Variableses the manager_ variable link removed. /// </summary> /// <param name="variable">The variable.</param> /// <param name="link">The link.</param> private void variablesManager_VariableLinkRemoved(PacketPropertyVariable variable, PacketPropertyVariableLink link) { // Update links listbox only if variable is currently selected (and hence links list contains variables links) if (varListBox.SelectedItem == variable) { varPacketsLinksListBox.Items.Remove(link); } }
/// <summary> /// Adds the variable link. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="variableLink">The variable link.</param> /// <returns><code>true</code> if link is added successfully, <code>false</code> otherwise.</returns> public bool AddVariableLink(string variableName, PacketPropertyVariableLink variableLink) { bool ret = DoVariableOperation( variableName, // Add the link delegate(PacketPropertyVariable variable) { bool opRet = !IsVariableLinkedToPacket(variable, variableLink.PacketClass.type); if (opRet) { variable.Links.Add(variableLink); } return opRet; }, // Fire event on success delegate(PacketPropertyVariable variable) { FireVariableLinkOperationEvent(variable, variableLink, VariableLinkAdded); }) ; return ret; }
/// <summary> /// Handles the Click event of the varPacketsFieldsModifyButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param> private void varPacketsFieldsModifyButton_Click(object sender, EventArgs e) { try { // Get currently selected data PacketPropertyVariable var = (PacketPropertyVariable)varListBox.SelectedItem; PacketPropertyVariableLink oldLink = (PacketPropertyVariableLink)varPacketsLinksListBox.SelectedItem; // Both variable and link must be selected if (null == var || null == oldLink) { Log.Info("Please select a variable and link first."); } else { // Create new variable link PacketClass pak = (PacketClass)packetClassComboBox.SelectedItem; ClassMemberPath mem = (ClassMemberPath)packetClassPropertyComboBox.SelectedItem; PacketPropertyVariableLink newLink = new PacketPropertyVariableLink(pak, mem); // Update selected link if (!variablesManager.ReplaceVariableLink(var.Name, oldLink, newLink)) { Log.Info("Failed to update variable link."); } } } catch (Exception ex) { Log.Error("Error", ex); } }
/// <summary> /// Handles the Click event of the varPacketsFieldsAddButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param> private void varPacketsFieldsAddButton_Click(object sender, EventArgs e) { try { // Get currently selected variable PacketPropertyVariable var = (PacketPropertyVariable)varListBox.SelectedItem; if (null == var) { Log.Info("Please select a variable first."); } else { // Create variable link PacketClass pak = (PacketClass) packetClassComboBox.SelectedItem; ClassMemberPath mem = (ClassMemberPath) packetClassPropertyComboBox.SelectedItem; PacketPropertyVariableLink link = new PacketPropertyVariableLink(pak, mem); // Add selected link to selected variable if (!variablesManager.AddVariableLink(var.Name, link)) { Log.Info("Failed to add variable link."); } } } catch (Exception ex) { Log.Error("Error", ex); } }