Quick Start – Initializing/Saving Objects on Startup/Shutdown
The xAgentBuilder code generator makes it very easy to initialize objects with default values or read them from a file on startup and save them to a file on shutdown. All the code required to perform these functions is generated for the developer. Mostly it is a matter of commenting or uncommenting a few lines of code.
The first thing to note is that each generated class that comprises of objects (scalar or tabular) has two member functions, ‘LoadFromNV’ and ‘SaveToNV’. These functions are used to store values in a local file or read values from it. The code for reading/writing values for objects is already present there. The developer can decide whether to save all the object value or some and then make modifications to the code. The LoadFromNV of GarageObjects is provided below.
m_pNV->GetInt(s_pszNVSection, "garageCOLevel", m_nGarageCOLevel);
m_pNV->GetInt(s_pszNVSection, "garageCOLevelRisingThreshold", m_nGarageCOLevelRisingThreshold);
Now let’s look at the Initialize function of DGarageObjects.
BMibDefVal& dv = DTutorial::defval;
m_strGarageAddress = dv.dispStr;
m_nGarageNumVehicles = dv.number;
m_nGarageCOLevel = dv.number;
m_nGarageCOLevelRisingThreshold = dv.number;
m_nGarageCOLevelFallingThreshold = dv.number;
// Uncomment following line to initialize values from NV
Notice that, by default, LoadFromNV has been commented out. If you need to read values from a local file, you will need to uncomment this function. Similarly, in the destructor of the class, SaveToNV has been commented out. You will need to uncomment that as well. The above function assigns default values from the mib default value class. For your agent, this is probably not appropriate. You will need to initialize these objects with default values if they are not being read from the file.
You will see in the code that the name of the file is, by default, the name of your project. You can change that name to whatever you wish. The saving of values need not be done only at the time of shutdown. The developer may wish to save the values of the objects periodically in the given file. This function can be called for that purpose as well.
The next page of this tutorial covers “Working with MIB Tables”. Please see the Quick Start – Working with MIB Tables for that page.