/*
 * Note: this file originally auto-generated by mib2c using
 *        : mib2c.create-dataset.conf 9375 2004-02-02 19:06:54Z rstory $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "abc.h"

netsnmp_table_data_set *table_set;

/** Initialize the messageTable table by defining its contents and how it's structured */
void
initialize_table_messageTable(void)
{
    static oid messageTable_oid[] = {1,3,6,1,4,1,11456,3,3};
    size_t messageTable_oid_len = OID_LENGTH(messageTable_oid);

    //netsnmp_table_data_set *table_set;
    netsnmp_table_row *row;

    /* create the table structure itself */
    table_set = netsnmp_create_table_data_set("messageTable");

    /* comment this out or delete if you don't support creation of new rows */
    table_set->allow_creation = 1;

    /***************************************************
     * Adding indexes
     */
    DEBUGMSGTL(("initialize_table_messageTable",
                "adding indexes to table messageTable\n"));
    netsnmp_table_set_add_indexes(table_set,
                           ASN_INTEGER,  /* index: messageIndex */
                           0);

    DEBUGMSGTL(("initialize_table_messageTable",
                "adding column types to table messageTable\n"));		 
    netsnmp_table_set_multi_add_default_row(table_set,
                                          /*  COLUMN_MESSAGEINDEX, ASN_INTEGER, 0,
                                            NULL, 0, */
                                            COLUMN_MESSAGETYPE, ASN_OCTET_STR, 1,
                                            NULL, 0,
                                            COLUMN_SENTMSGCOUNT, ASN_INTEGER, 1,
                                            NULL, 0,
                                            COLUMN_ERRORMSGCOUNT, ASN_INTEGER, 1,
                                            NULL, 0,
                                            COLUMN_CURRENTMSGID, ASN_INTEGER, 1,
                                            NULL, 0,
                              0);
    
    /* registering the table with the master agent */
    /* note: if you don't need a subhandler to deal with any aspects
       of the request, change messageTable_handler to "NULL" */
    netsnmp_register_table_data_set(netsnmp_create_handler_registration("messageTable", messageTable_handler,
                                                        messageTable_oid,
                                                        messageTable_oid_len,
                                                        HANDLER_CAN_RWRITE),
                            table_set, NULL);

    FILE *filep;
    char line[256];

    int index = 1;
    int msg_id, msg_sent, msg_err = 0;
    char type[10] = "AMF";		

    filep = fopen("data.txt", "r");

    if(NULL !=  filep) {

		while(fgets(line, sizeof(line), filep) != NULL) {

		    row = netsnmp_create_table_data_row();
	        netsnmp_table_row_add_index(row, ASN_INTEGER, &index , sizeof(index));

	        sprintf(type, "%s", strtok(line, ","));
			netsnmp_set_row_column(row, COLUMN_MESSAGETYPE, ASN_OCTET_STR, type, strlen(type));
			netsnmp_mark_row_column_writable(row, COLUMN_MESSAGETYPE, 1);

		    msg_id = atoi(strtok(NULL, ","));
		    netsnmp_set_row_column(row, COLUMN_CURRENTMSGID, ASN_INTEGER, (char*) &msg_id, sizeof(msg_id));
	        netsnmp_mark_row_column_writable(row, COLUMN_CURRENTMSGID, 1);

		    msg_sent = atoi(strtok(NULL, ","));
	        netsnmp_set_row_column(row, COLUMN_SENTMSGCOUNT, ASN_INTEGER, (char*) &msg_sent, sizeof(msg_sent));
	        netsnmp_mark_row_column_writable(row, COLUMN_SENTMSGCOUNT, 1);

		    msg_err = atoi(strtok(NULL, ","));
	        netsnmp_set_row_column(row, COLUMN_ERRORMSGCOUNT, ASN_INTEGER, (char*) &msg_err, sizeof(msg_err));
	        netsnmp_mark_row_column_writable(row, COLUMN_ERRORMSGCOUNT, 1);

		    netsnmp_table_dataset_add_row(table_set, row);

		    index ++;
	  	}

		netsnmp_register_auto_data_table(table_set, NULL);

		fclose(filep);
    }	


}

void 
updateMessageTable(unsigned int clientreg, void *clientarg)
{
	FILE *filep;
    char line[256];

    int index = 1;
    int msg_id, msg_sent, msg_err = 0;
    char type[10] = "";		

    filep = fopen("data.txt", "r");

    if(NULL !=  filep) {

		while(fgets(line, sizeof(line), filep) != NULL) {
		
			//TODO:How to update the row here by index
			
			index ++;
		}
		fclose(filep);
	}

	printf("MESSAGE_TABLE table updated\n");	
}

/** Initializes the abc module */
void
init_abc(void)
{

  /* here we initialize all the tables we're planning on supporting */
    initialize_table_messageTable();

    snmp_alarm_register(3, SA_REPEAT, updateMessageTable, NULL);
}

/** handles requests for the messageTable table, if anything else needs to be done */
int
messageTable_handler(
    netsnmp_mib_handler               *handler,
    netsnmp_handler_registration      *reginfo,
    netsnmp_agent_request_info        *reqinfo,
    netsnmp_request_info              *requests) {
    /* perform anything here that you need to do.  The requests have
       already been processed by the master table_dataset handler, but
       this gives you chance to act on the request in some other way
       if need be. */
    return SNMP_ERR_NOERROR;
}
