Hi,
I  have problem of freeing the messageTable_head linked list. I found it frees 
everytime cache expires by calling messageTable_free(). But I do not want that 
to be freed everytime. It will be created at start time and want to be freed at 
shutdown.
I have shown this by File* fp. But actually I am making connection to some 
fixed 
applications and want to retain the connections.  The other contents (except 
File*)can be freed and updated everytime . I am storing the File* value in 
messageTable_head linked list. The linked list also need to be freed at 
shutdown.

Even the skeleton code also tells the same . "/* Initialise the contents of the 
table here */. but where to free ?

Please give me some idea .

Thanks
Sujata






//global variables
#define FILECOUNT 3
    struct messageTable_entry  *messageTable_head;
    char files[] ={data1.txt, data2.txt, data3.txt }

/** Initializes the abcModule module */
void
init_abcModule(void)
{
  /* here we initialize all the tables we're planning on supporting */
    initialize_table_messageTable();
}



/** Initialize the messageTable table by defining its contents and how it's 
structured */
void
initialize_table_messageTable(void)
{
    const oid messageTable_oid[] = {1,3,6,1,4,1,11456,3,3};
    const size_t messageTable_oid_len   = OID_LENGTH(messageTable_oid);
    netsnmp_handler_registration    *reg;
    netsnmp_iterator_info           *iinfo;
    netsnmp_table_registration_info *table_info;
    int total = 0;
     DEBUGMSGTL(("token", "Calculating running total: "));
     DEBUGMSG(("token", "\n"));


    DEBUGMSGTL(("abcModule:init", "initializing table messageTable\n"));

    reg = netsnmp_create_handler_registration(
              "messageTable",     messageTable_handler,
              messageTable_oid, messageTable_oid_len,
              HANDLER_CAN_RWRITE
              );


    table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
/*    netsnmp_table_helper_add_indexes(table_info,
                           ASN_INTEGER,  
                           ASN_OCTET_STR,
                           0);
                           */
     netsnmp_table_helper_add_indexes(table_info, /*ASN_INTEGER, 
                                                 ASN_OCTET_STR,
                                                 ASN_INTEGER,
                                                 ASN_INTEGER, */
                                                 ASN_INTEGER, 0);
                                                 
    table_info->min_column = COLUMN_MESSAGETYPE;
    table_info->max_column = COLUMN_CURRENTMSGID;
    
    iinfo = SNMP_MALLOC_TYPEDEF( netsnmp_iterator_info );
    iinfo->get_first_data_point = messageTable_get_first_data_point;
    iinfo->get_next_data_point  = messageTable_get_next_data_point;
    iinfo->table_reginfo        = table_info;
    
    netsnmp_register_table_iterator( reg, iinfo );
    netsnmp_inject_handler_before( reg, 
        netsnmp_get_cache_handler(MESSAGETABLE_TIMEOUT,
                                  messageTable_load, messageTable_free,
                                  messageTable_oid, messageTable_oid_len),
            TABLE_ITERATOR_NAME);

    /* Initialise the contents of the table here */
    //////
    //Create the messageTable_entry list.
    //Open the file pointer and assign to it to messageTable_entry->fp 
    loadAll();
   // where should i close the fp ?
}

    /* Typical data structure for a row entry */
struct messageTable_entry {
    /* Index values */
    long messageIndex;
    //connection pointer to the Application
    FILE *fp;

    /* Column values */
    char messageType[10];
    long sentMsgCount;

    long errorMsgCount;

    long currentMsgID;


    /* Illustrate using a simple linked list */
    int   valid;
    struct messageTable_entry *next;
};



/* create a new row in the (unsorted) table */
#ifdef jsaj
struct messageTable_entry *
messageTable_createEntry(
                 long  messageIndex
                ) {
    struct messageTable_entry *entry;

    entry = SNMP_MALLOC_TYPEDEF(struct messageTable_entry);
    if (!entry)
        return NULL;

    entry->messageIndex = messageIndex;
    entry->next = messageTable_head;
    messageTable_head = entry;
    return entry;
}

/* remove a row from the table */

void
messageTable_removeEntry( struct messageTable_entry *entry ) {
    struct messageTable_entry *ptr, *prev;

    if (!entry)
        return;    /* Nothing to remove */

    for ( ptr  = messageTable_head, prev = NULL;
          ptr != NULL;
          prev = ptr, ptr = ptr->next ) {
        if ( ptr == entry )
            break;
    }
    if ( !ptr )
        return;    /* Can't find it */

    if ( prev == NULL )
        messageTable_head = ptr->next;
    else
        prev->next = ptr->next;

    SNMP_FREE( entry );   /* XXX - release any other internal resources */
}
#endif
/* Example cache handling - set up linked list from a suitable file */
loadAll()
{

    struct messageTable_entry *this;
    char *fileName;
    File *fp;
    int i =0;
    for(i=0; i<FILECOUNT;++i) {
    
       fileName = files[i];
       fp = fopen( fileName, "r" );
       
        if ( !fp ) {
            return -1;
        }
        snmp_log(LOG_INFO,"open the connection %s \n",fileName);
        this = SNMP_MALLOC_TYPEDEF( struct messageTable_entry );
        //store the connection pointer
        this->fp = fp;
        this->messageIndex = i;
        this->next = messageTable_head;
        messageTable_head = this;    /* Iterate helper is fine with unordered 
lists! */
    }
    
}
bool isValidConnection(File* fp) {
    //just returning true.
    return true;
}
int
messageTable_load( netsnmp_cache *cache, void *vmagic ) {
    FILE *fp;
    struct messageTable_entry *this;
    char buf[STRMAX];
    char type[10];
    int index = 0;
    int msg_id, msg_sent, msg_err = 0;
    memset(type,0x00,10);
   
    for ( this = messageTable_head; this; this=that ) {
        that = this->next;
        fp = this->fp
        //is this connection valid.

       
        if ( isValidConnection() && fgets( buf, STRMAX, fp )) {  
            //populate the values
                
            snmp_log(LOG_INFO,"populating the data.\n\n");
             sprintf(type, "%s", strtok(buf, ","));
            strcpy(this->messageType, type);
            
            msg_id = atoi(strtok(NULL, ","));
            this->currentMsgID = msg_id;

            msg_sent = atoi(strtok(NULL, ","));
            this->sentMsgCount = msg_sent;
            
            msg_err = atoi(strtok(NULL, ","));
            this->errorMsgCount = msg_err;
        }
         return 0;  /* OK */
}

void
messageTable_free( netsnmp_cache *cache, void *vmagic ) {
    struct messageTable_entry *this, *that;

    for ( this = messageTable_head; this; this=that ) {
        that = this->next;
        SNMP_FREE( this );   /* XXX - release any other internal resources */
    }
    messageTable_head = NULL;
}



      
------------------------------------------------------------------------------
The modern datacenter depends on network connectivity to access resources
and provide services. The best practices for maximizing a physical server's
connectivity to a physical network are well understood - see how these
rules translate into the virtual world? 
http://p.sf.net/sfu/oracle-sfdevnlfb
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to