Home
About NuDesign
Sales
Support
Eval Download
Partners
Testimonials
SNMP Links
Tutorials
News

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. 

 

//--------------------------------------------------

// LoadFromNV

//

int DGarageObjects::LoadFromNV()

{

      if (m_pNV)

      {

            m_pNV->GetString(s_pszNVSection, "garageAddress", m_strGarageAddress);

            m_pNV->GetInt(s_pszNVSection, "garageNumVehicles", m_nGarageNumVehicles);

            m_pNV->GetInt(s_pszNVSection, "garageCOLevel", m_nGarageCOLevel);

            m_pNV->GetInt(s_pszNVSection, "garageCOLevelRisingThreshold", m_nGarageCOLevelRisingThreshold);

            m_pNV->GetInt(s_pszNVSection, "garageCOLevelFallingThreshold", m_nGarageCOLevelFallingThreshold);

      }

      return NDERR_NO_ERROR;

}

 

Now let’s look at the Initialize function of DGarageObjects. 

 

//--------------------------------------------------

// Initialize

//

void DGarageObjects::Initialize()

{

      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

      // LoadFromNV();

      SetReady(true);

}

 

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.