Cpp Lesson 4

Tutorial for Visual xAgentBuilder for C++

Lesson 4: HTTP Agent

HTTP agent functionality is embedded in the BWebAgent class. HTTP agent is in fact web server with plug-in for processing SSI tags for accessing MIB objects.

To create a fully functional HTTP agent all we have to do is to create object of BWebAgent class and call Start method. This method will create a thread required for operation of the agent and return control to the main program. When user enters ‘q’ and exits CLI, Stop is invoked. Even if we forget to call Stop at the end, agent properly releases all allocated resources in the destructor.
 

int main(int argc, char*
argv[])

{

   BTime tm;

   BMibHandlerEx mh;

   mh.Init(&tm, NULL);

   BWebAgent webAgent(tm, &mh, NULL);

   tInt32 err = webAgent.Init(80, “”,
“ndmib?fileName=System.shtml”);

   if (err != NDERR_NO_ERROR)

       return err;

   BCLI cli( &mh );

   err = webAgent.Start();

   if (err != NDERR_NO_ERROR)

       return err;

   cli.Stream().Write(HEADING);

   cli.Start(“ndmp# “);

   webAgent.Stop();

   return 0;

}

 
BWebAgent’s constructor requires following parameters:

   BWebAgent(          BTime& tm,

               IMIBHandlerEx* pMibHndlr,

                INonVolatile* pNV)

 
The first one is the reference to the time object.

The second one is address of the object that implements IMIBHandlerEx interface. In this case this is address of mh, which is instance of BMibHandlerEx class. HTTP agent will be using this interface to resolve all requests for the management variables. Please note that HTTP agent requires IMIBHandlerEx interface, and not IMIBHandler interface.
 
The third parameter is pointer to the object that implements INonVolatile interface. If not supplied (i.e. if it is NULL as in this lesson), we have to programmatically initialize agent. This is done in the following line:

   tInt32 err = webAgent.Init(80, “”,
“ndmib?fileName=System.shtml”);

The first parameter specifies that we want HTTP agent to listen on port 80.

The second parameter is the root directory. In this case it is the same directory as executable.

The last parameter is default page:

             “ndmib?fileName=System.shtml”);

 
What kind of page is this? This is in fact query string and it means that it is intended for “ndmib” processor (which is SSI parser for MIB specific tags), and the parameter “fileName” tells us that the requested page is “System.shtml”.

Please build this project, start the agent and use Web Browser to inspect the agent’s only page: “System.shtml

Let’s take a peek in “System.shtml”.
Here is the excerpt:

   <td>

       <!–# GET name=SNMPv2-MIB.sysDescr respFmt=value–>

   </td>

 
This is familiar syntax of the SSI but the name of the command is not a standard one. GET is command implemented in SSI parser that is part of our web agent. Parameters for this command are: “name” which specifies the name of the variable to retrieve, and “respFmt” which means “response format”. This parameter specifies the format of the result. In this case we requested only value. For the list of other possible formats please consult reference.

 

Please proceed to Lesson 5: Multiprotocol Agent topic.

Previous    Next