From 617c5e2dc37583b788f20136993d0ccb85530ee7 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Mon, 14 May 2018 11:06:19 -0500 Subject: [PATCH] Get network links to use Actions --- EduNetworkBuilder/ActionClass.cs | 22 ++++++++++++++++++---- EduNetworkBuilder/LinkEditor.cs | 10 ++++++++-- EduNetworkBuilder/NB.cs | 16 ++++++++++++++-- EduNetworkBuilder/Network.cs | 1 + 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/EduNetworkBuilder/ActionClass.cs b/EduNetworkBuilder/ActionClass.cs index a349022..6d7137f 100644 --- a/EduNetworkBuilder/ActionClass.cs +++ b/EduNetworkBuilder/ActionClass.cs @@ -146,7 +146,7 @@ namespace EduNetworkBuilder public HostNicID DestNic; //Used for links public Point Location; //Used when making a new device or moving an old device public NetworkComponentType newItemType = NetworkComponentType.none; //Making new device - public NetworkDevice ChangedDevice = null; + public NetworkComponent ChangedComponent = null; public void DoAction() { @@ -156,7 +156,21 @@ namespace EduNetworkBuilder switch (Action) { - case NBAction.changedevice: + case NBAction.changecomponent: + //This could be a link or a device we are changing. + if(ChangedComponent is NetworkDevice) + { + source.UpdateFromComponent(ChangedComponent); //Copy any changes across + } + else + { //It is a link. Delete the old, make the new. Mark as linked + if(source != null) + myNet.RemoveComponent(source); + NetworkLink NL = (NetworkLink)ChangedComponent; + myNet.MarkAsLinked(NL.Src, NL.GetUniqueIdentifier); + myNet.MarkAsLinked(NL.Dst, NL.GetUniqueIdentifier); + myNet.AddItem(ChangedComponent); + } break; case NBAction.changelocation: if (source != null) @@ -195,7 +209,7 @@ namespace EduNetworkBuilder Point TopRight = new Point(TopLeft.X + myNet.itemsize, TopLeft.Y); Point BottomRight = new Point(TopLeft.X + myNet.itemsize, TopLeft.Y + myNet.itemsize); Point BottomLeft = new Point(TopLeft.X, TopLeft.Y + myNet.itemsize); - ChangedDevice = null; //if we cannot place it, this = null for reference + ChangedComponent = null; //if we cannot place it, this = null for reference if (myNet.BlockedByTree(Location)) CanDo = false; if (myNet.BlockedByTree(TopLeft)) CanDo = false; if (myNet.BlockedByTree(TopRight)) CanDo = false; @@ -203,7 +217,7 @@ namespace EduNetworkBuilder if (myNet.BlockedByTree(BottomLeft)) CanDo = false; if (CanDo) { - ChangedDevice = (NetworkDevice)myNet.AddItem(newItemType, Location); + ChangedComponent = (NetworkDevice)myNet.AddItem(newItemType, Location); } else { diff --git a/EduNetworkBuilder/LinkEditor.cs b/EduNetworkBuilder/LinkEditor.cs index 76c6097..a7ab0f0 100644 --- a/EduNetworkBuilder/LinkEditor.cs +++ b/EduNetworkBuilder/LinkEditor.cs @@ -228,11 +228,17 @@ namespace EduNetworkBuilder if(EditingLink != null) { Net.RemoveComponent(EditingLink); //get rid of the old one and put the new one in - EditingLink = null; } NetworkLink newLink = NewLinkFromSettings(); - Net.AddItem(newLink); + //Net.AddItem(newLink); + int OldLinkID = -1; + if (EditingLink != null) OldLinkID = EditingLink.GetUniqueIdentifier; + NB.DoActionChangeComponent(OldLinkID,newLink); + if (EditingLink != null) + { + EditingLink = null; + } Close(); } diff --git a/EduNetworkBuilder/NB.cs b/EduNetworkBuilder/NB.cs index 2ff4381..76f75c3 100644 --- a/EduNetworkBuilder/NB.cs +++ b/EduNetworkBuilder/NB.cs @@ -104,7 +104,7 @@ namespace EduNetworkBuilder public enum HowToBreak { PowerOff, EthernetBreak, EthernetCorrupt, EthernetRemoveLink, LockOutSwitch, WirelessBreakSSID, WirelessBreakKey, DeviceChangeIP, DeviceChangeGW, StaticRouteClear, StaticRouteCorrupt, VLANChange, VPNChangeEndpoint, VPNChangeKey, BreakDevice } - public enum NBAction { none, newdevice, changelocation, changedevice, deletecomponent, + public enum NBAction { none, newdevice, changelocation, changecomponent, deletecomponent, ping, traceroute, arp, cleararp, dhcp } @@ -1408,7 +1408,7 @@ namespace EduNetworkBuilder AC.DoAction(); - return AC.ChangedDevice; + return AC.ChangedComponent; } public static void DoActionPingDevice(int HostID, NB_IPAddress Destination) @@ -1469,6 +1469,18 @@ namespace EduNetworkBuilder AC.DoAction(); + } + public static void DoActionChangeComponent(int HostID, NetworkComponent Changed) + { + ActionClass AC = new ActionClass(); + AC.Action = NBAction.changecomponent; + AC.SourceID = HostID; + AC.ChangedComponent = Changed; + + RegisterAction(AC); + + AC.DoAction(); + } #endregion ActionFunctions diff --git a/EduNetworkBuilder/Network.cs b/EduNetworkBuilder/Network.cs index 586b8f3..acd6366 100644 --- a/EduNetworkBuilder/Network.cs +++ b/EduNetworkBuilder/Network.cs @@ -815,6 +815,7 @@ namespace EduNetworkBuilder public void RemoveComponent(NetworkComponent tItem) { + if (tItem == null) return; //Exit early if we are not removing anything for (int i = NetComponents.Count -1; i >= 0; i--) { if (NetComponents[i] == tItem)