 Welcome to the Network Engineering video blog. I am your host, Michael Crane. In this second half of our two-part video on config file open, we will be using Microsoft's interaction modules called by name function to set the properties at run time in our class node objects and class link objects. We will introduce a new key value pair class used to help parse the config file and we will look at modifications made in our windows in our main window code behind node class and the link class. We will also discuss the three reasons why the code was written with refactoring an interaction and basically the reasons are single file to add, change and delete properties, dynamically add properties to a class and give the user the ability to add run time custom properties to nodes and links. It's very cool. So let's get started. Okay, so in the last video if you remember we were implementing the file save and file save as, right? So in this video we are going to implement the opposite of that or I don't know if it's opposite but the fruits of our labor with the file open, all right? Then we're going to get our config file which is a text file and open it and I'm sorry it's running a little bit slow it's my screen capture software that's making it kind of chug along so you'll just have to bear with me, okay? Let's go take a look at the open test bed now. One of the things to note it's got a, I'm instantiating OBJ as just a generic object that's so we can put different object types in it, okay? And you'll see here in a second nothing real fancy here, got a scream raider. Here's our little key value pair of class that I like using. And so the first thing is get config and this should probably should be like get config line or something like that because it's just getting one line and we'll jump down here to get configured real quick and just look at it and it's a pretty simple little function it's just returning this key value pair of class and it's just using read line and so just saying okay if the line is not nothing or then, oh if the line is nothing then this is the end of the file and that's why you probably noticed I have no spaces in here. Spaces would probably make it look a little nicer but I'd have to fix code and I didn't really want to do that right now it's not that important. Yeah so and then this else if is if it's got a start or an end and we're just going to trim the spaces off the line and set the value and put that as the key and set the value to empty string or if it's got an equal signs in there which means it's a key value pair then we're going to split it and put the first part of the split in the key and the second part of the split into the value trim them all up and that's what we're going to return the key value pair of class right. Else it's going to catch all it's just going to put two blanks in there or if it gets an exception from this read line because it can throw IO exceptions it's just going to send an end of file back to this guy and so you know once we get the C object back this KVP object back we're going to you know look okay as a key Cisco router start Cisco router end we'll just start from the top real quick I won't go through all these they're all pretty much the same so it says okay if a key is like Cisco router start set this generic object to a new Cisco Cisco router node okay and this is why I had to put Cisco router start Cisco router right so I knew what kind of child class to instantiate out of our class node parent right so okay so we got the new new node okay so then it goes might be better if we look at the file so okay so we found the Cisco router start okay this guy right here right and so once it gets this it's going to drop out drop out of this loop and it's going to loop back up and it's going to go get another line and in this line is going to be name equals router okay well it's not going to match you know any of these start or end statements right so it's going to drop all the way down to this else right here and this is a something that I put together using this call by name interaction function okay and this is one of Microsoft's public module for different functions and and this is a handy little little object so anyway let's start from the top so if the key is not null and the and the value is not null then and it's going to drop in here to you know set the key value to the key and and the value value to the value and I guess I didn't really need to do this I just did it to make it easier for me to read even though it's fairly easy to read anyway isn't it okay so if key is like name then then we're going to do this call by name object and don't confuse the name the call by name and the name name there are two different things right and so this call by name if you look at them so what it does is as you pass it an object that you're the object you're trying to manipulate proceed your name okay and the call type what you're trying to do with it and in any arguments so here's our object here's our key which is our property object right well it's not in this one it's the name property which actually isn't in here anyway so name is actually a it's a user control property which is it even inherits it from something another I guess this controls object or something I forget where it inherits it but I had to kind of peel them out because a name is it set somewhere else and you'll see so call by name the object is our node object that we assigned our Cisco router node object the key is name right the call type is set so this is a property so we're going to do a set on it that's what I was showing you earlier in the properties and then we're gonna set its value and that's all I'm doing is I'm just going through all the key value pairs and setting them in the object so let's look at another case it might be a little easier to look at so okay so index equals zero right okay so it's gonna fall down here the key is index of value zero and it's gonna come down to this this guy right here and here's so it's the object dot property so it's our object is still a node our Cisco router node but now we're gonna set the property object inside the Cisco router node with a key the function we're calling a set and we're gonna set its value this little function just gonna go through every one of these key value pairs until it gets to the router end and when it hits the router end it's just gonna add that node that's populated with all the variables into the collection nodes right okay and it's just gonna go through this whole file until it gets to the very end down here and then we will get the end of file then it's gonna close our stream reader if the collection of nodes is greater than zero it's gonna return true and if it's if it's equal to zero or less it'll be false does that make sense so yeah so this call by name thing we can look at it just real quick you can Google it yourself if you want it's interaction call by name here's where it talks about the object required a pointer to the object exposing the property of method the name a string expressing the Canadian the name of the property or method so it has to be a property or method right and then the call type it can either be a method you know like for calling a function or subroutine or if it's a property you can do a set or get and that's why I made that little class and then their arguments of course or what you're trying to do with it you're trying to set a value or or get a value that sort of thing right and that's the reason why I made all these members properties right so we can do a get a set on them mainly the set at this point okay I hope that makes sense yeah so once it hits the end of this this is what gets returned this return to return false back to our our main window guy right this open file this is this is where he's getting the the return value from this open test bed right okay and to finish off here let's go back in our our diff just to go over this so we did add this this property prop as a class node properties and just basically removed all the all the properties from the the class node right and put in its own little object the same way with the link the class link here yeah so it's it's exact same things just using the link properties class also on this class link the set location I had to add in class node and and set end points I had to add in and class link and I don't remember did we we didn't look at set location in the class knows oh okay a little might as well start from from this guy here class node set location and and just feed it the the left value and the top value that we got from the config file go look at this guy real quick yeah so here's a so it's a top left corner and that we're setting the location with right and that's all I'm doing is just set left set set top that's pretty much it and so on the class link it's a little bit different because class link doesn't have a set location the way it was instantiated is I would do a new add new link and I would pass it this is when we're provisioning it the main window I'd pass it the a node object and the b node object which are the two end points basically of the link it said is a node and b node and this wasn't here and then he would draw the link because he had all the information you know he could get the location from the a node and b node so now if we do a you know do an ad with a new we have to populate this property which is a string a node and and b node in our property class node or sorry class link properties file but when we're doing this open so we're doing this open testbed though we're not getting that we're just doing link start the objects a new class link so he's not passing any endpoints right so he's using the default constructor this guy right here right and so none of this stuff is getting set so I had to add this subroutine in here which is set endpoints mode this open testbed populates the collection of links right and then the main window as he's going through all the links in the collection of links he's adding the links to the canvas okay and then he's calling the set end points and he's passing in the name the string name from this properties file of node a and node b and and then this guy right here is just going through all the nodes and saying okay if node a is like see in dot name he's just basically assigning node a and node b and then then he's calling this draw link I really hope this all makes sense if you kind of go through it you'll you'll understand it's it's pretty straightforward it it's just so I couldn't call this constructor I couldn't pass it in a node object because we didn't get it object out of the text file obviously we only got its name so somewhere I had to resolve the node into an object for the link and I figured this was the best place for it I don't know there might be other places that are better if you have any comments let me know in a couple other things I should probably point out so in the class node I I changed but where's it different maybe I talked about this already I don't remember to be honest with you anyway I changed this this function from get location to get center point because location is actually the top left as you see here in set location it's it's a top left and this get location function was actually returning the middle point and not the top left so I changed his name to get center point just little housekeeping and of course we added this at location which we already talked about so on our mouse mobile so I'm sorry when we move a node on our canvas this is where I set the in the properties follow the left in the top portion of it so you might be asking yourself you know why make this so generic like this you know I use reflection and this call by name and have to create a whole new class just to hold the the properties you know why go through all this and there's a there's there's three reasons why I'm going down this path so one of the things I like about this is for the links and the nodes between the link properties and node properties anytime I want to add another property for a node no matter what kind of node it is for the base class node all I have to do is add it in one spot I don't have to update any of the IO I don't have to change anything as far as opening files closing files saving the configuration all I have to do is just uncomment this guy and back we can do it let me just uncomment them save it start and let's see file open okay now if I do a file save I'll do a save as I'll just call them something test all right let's open this guy and there it is see automatically added it to to our config dot our config file that it saves off he might be saying well that's great but you still got to import it right well let's try that or import you still got a you know open it back up so I'm just to show you that it's gonna work I'm just gonna go ahead and X out of this start a fresh one brand new file open and here's our config dot test open now granite you can't really see those name in there but it added actually if we could probably if we look through the the debug window here so oh here we go here it is right here so see hostname equals hostname and if I did a file save it would just save it back off that's pretty nice right I don't have to touch any of the IO opening closing or saving files all I have to do is just add this property to the class and it's automatically there okay and you might be thinking well that's that's pretty nice that's pretty great but what about the user interface of GUI and that's something else too I don't have an example to really show you when we come up here and do an edit right which doesn't really do anything right now I'm going to try to make it where it just goes and pulls up all the properties you know and what they are strings integer types and just builds a GUI based on what's in this file right there was in this class right here right and so then I wouldn't have to even change the GUI all I have to do is just add a property to this class file right that would be pretty sweet so but the last thing and this is a what if what if we could at runtime dynamically add these properties to this class node okay and you following what I'm saying so at runtime let's say this this wasn't here let's say it wasn't there right at runtime so the user fires this guy up and says you know gosh I wish this thing had a string called hostname where I could store its hostname right from internet access you guys know what a hostname is I don't need to explain it to you so at runtime so all this GUI is running away clicks on he clicks on edit node and then clicks on like add new property and it automatically pops a brand new property up into this class nodes property object now obviously it's not going to save this not going to update the class with this at runtime but it could when you open the file backup automatically read it for you every time so because if you can do it during runtime you can do it oh you know with just an ad and these are clicking on add you can do it at that opening the file config right just go populate all the all the custom properties and if you think about that that would allow you let's just ignore these for the moment that would allow you to take these guys and say you know I want to add a field called and I could be the owner maybe I could just call this owner I'm owner of the Cisco router node right and what's nice about this is is this can be edited with any text file a text editor right this can be done at runtime so when you distribute your Mogwai application you as the coder no longer has to add custom properties that that everybody wants to the code recompile it redistribute to all that stuff they can go at it themselves and I think this this would be very very powerful and because right now if you think about it you take this guy out for a second because that won't work we know oh neither will this right now I can take this and go troll C V and I need to change them to a new router I'll just call them let's just put them way out there how about router 50 alright index 50 now I don't know if this gonna work I have not tried this I mean it should and let's see where can we put them we'll say at 200 all right this should work now remember we have to take the spaces out okay we'll save that I'll run this guy and this may or may not work I have not actually tried this so we're we're gonna try it together so file open oops test oh I took the oops I took the host name out of where is it right yeah my bad see if it was dynamic wouldn't let's try that one last time if it doesn't work anyway you can't you kind of get the idea of what I'm trying to do here I think you know it's basically where you can change the provisioning of this and change properties are in the classes at runtime right with using just nothing but a text file the users can do it oh yeah see there we go here's our node router 50 so that already works this is something that the user can go add right now I mean this this works already right don't forget you can support the network engineering video blog by donation using a credit card and PayPal or by purchasing products at the mux all store details and links are in the description under this video well that's it for this video if you like the video give it a big thumbs up that helps and hit the subscribe button that really helps if you have any questions or comments post them in the comments under this video thanks for watching and I'll see you next time