欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

用NET-SNMP软件包开发简单客户端代理(5)

程序员文章站 2022-05-01 21:46:44
...

6.3.3 ExampleTable_access.c /*ExampleTable_access.c*/ /* * Note: this file originally auto-generated by mib2c using * :mib2c.access_functions.conf,v 1.9 2004/10/14 12:57:33 dts12 Exp $ */ #include net-snmp/net-snmp-config.h #includenet-snm

6.3.3 ExampleTable_access.c

/*ExampleTable_access.c*/

/*

* Note: this file originally auto-generated by mib2c using

* : mib2c.access_functions.conf,v 1.9 2004/10/14 12:57:33 dts12 Exp $

*/

#include

#include

#include

#include "ExampleTable_access.h"

#include "ExampleTable_enums.h"

#include

struct ExampleTable_entry {

long MachineNumber;

char MachineStatus[10];

u_long CheckTime;

long MonSet;

struct ExampleTable_entry *next;

};

struct ExampleTable_entry *ExampleTable_head=NULL;

/* create a new row in the (unsorted) table */

struct ExampleTable_entry *

ExampleTable_createEntry(long MachineNumber,

char *MachineStatus,

u_long CheckTime,

long MonSet

)

{

struct ExampleTable_entry *entry;

entry = SNMP_MALLOC_TYPEDEF(struct ExampleTable_entry);

if (!entry)

return NULL;

entry->MachineNumber = MachineNumber;

strcpy(entry->MachineStatus ,MachineStatus);

entry->CheckTime = CheckTime;

entry->MonSet =MonSet;

entry->next = ExampleTable_head;

ExampleTable_head = entry;

return entry;

}

/* remove a row from the table */

void

ExampleTable_removeEntry( struct ExampleTable_entry *entry )

{

struct ExampleTable_entry *ptr, *prev;

if (!entry)

return; /* Nothing to remove */

for ( ptr = ExampleTable_head, prev = NULL;

ptr != NULL;

prev = ptr, ptr = ptr->next ) {

if ( ptr == entry )

break;

}

if ( !ptr )

return; /* Can't find it */

if ( prev == NULL )

ExampleTable_head = ptr->next;

else

prev->next = ptr->next;

SNMP_FREE( entry ); /* XXX - release any other internal resources */

}

void data_read(void)

{

FILE *fp;

int i;

struct ExampleTable_entry *temp;

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

while (ExampleTable_head)/*clean link list begin*/

{

temp=ExampleTable_head->next;

ExampleTable_removeEntry(ExampleTable_head);

ExampleTable_head=temp;

}/*clean link list end*/

temp=SNMP_MALLOC_TYPEDEF(struct ExampleTable_entry);

temp->next=NULL;

/*set up a link list begin*/

if(fp)

{

i=fscanf(fp,"%d %s %d %d",&temp->MachineNumber, temp->MachineStatus, &temp->CheckTime,&temp->MonSet);

/*fscanf return reading var numbers .if EOF return -1.feof() dosen't work well*/

while(i>0)

{

ExampleTable_createEntry(temp->MachineNumber,temp->MachineStatus,temp->CheckTime,temp->MonSet);

i=fscanf(fp,"%d %s %d %d",&temp->MachineNumber, temp->MachineStatus, &temp->CheckTime,&temp->MonSet);

}

SNMP_FREE(temp);

fclose(fp);/*set up a link list end*/

}

else

{

printf("Error:Can't open data.txt!\n");

/*exit(1);*/

}

}

static u_long long_ret;

/** returns the first data point within the ExampleTable table data.

Set the my_loop_context variable to the first data point structure

of your choice (from which you can find the next one). This could

be anything from the first node in a linked list, to an integer

pointer containing the beginning of an array variable.

Set the my_data_context variable to something to be returned to

you later that will provide you with the data to return in a given

row. This could be the same pointer as what my_loop_context is

set to, or something different.

The put_index_data variable contains a list of snmp variable

bindings, one for each index in your table. Set the values of

each appropriately according to the data matching the first row

and return the put_index_data variable at the end of the function.

*/

netsnmp_variable_list *

ExampleTable_get_first_data_point(void **my_loop_context, void **my_data_context,

netsnmp_variable_list *put_index_data,

netsnmp_iterator_info *mydata)

{

data_read();

*my_loop_context = ExampleTable_head;

*my_data_context = ExampleTable_head;

return ExampleTable_get_next_data_point(my_loop_context, my_data_context,

put_index_data, mydata );

}

/** functionally the same as ExampleTable_get_first_data_point, but

my_loop_context has already been set to a previous value and should

be updated to the next in the list. For example, if it was a

linked list, you might want to cast it to your local data type and

then return my_loop_context->next. The my_data_context pointer

should be set to something you need later and the indexes in

put_index_data updated again. */

netsnmp_variable_list *

ExampleTable_get_next_data_point(void **my_loop_context, void **my_data_context,

netsnmp_variable_list *put_index_data,

netsnmp_iterator_info *mydata)

{

struct ExampleTable_entry *entry = (struct ExampleTable_entry *)*my_loop_context;

netsnmp_variable_list *idx = put_index_data;

if ( entry )

{

snmp_set_var_value( idx, (u_char *)&entry->MachineNumber, sizeof(entry->MachineNumber) );

idx = idx->next_variable;

*my_data_context = (void *)entry;

*my_loop_context = (struct ExampleTable_entry *)entry->next;

}

else

{

return NULL;

}

return put_index_data;

}

/** Create a data_context for non-existent rows that SETs are performed on.

* return a void * pointer which will be passed to subsequent get_XXX

* and set_XXX functions for data retrival and modification during

* this SET request.

*

* The indexes are encoded (in order) into the index_data pointer,

* and the column object which triggered the row creation is available

* via the column parameter, if it would be helpful to use that information.

*/

/*

void *

ExampleTable_create_data_context(netsnmp_variable_list *index_data) {

return entry; /* XXX: you likely want to return a real pointer */

/*

}

*/

/** If the implemented set_* functions don't operate directly on the

real-live data (which is actually recommended), then this function

can be used to take a given my_data_context pointer and "commit" it

to whereever the modified data needs to be put back to. For

example, if this was a routing table you could publish the modified

routes back into the kernel at this point.

new_or_del will be set to 1 if new, or -1 if it should be deleted

or 0 if it is just a modification of an existing row.

If you free the data yourself, make sure to *my_data_context = NULL */

int

ExampleTable_commit_row(void **my_data_context, int new_or_del)

{

/** Add any necessary commit code here */

/* */

/* return no errors. And there shouldn't be any!!! Ever!!! You

should have checked the values long before this. */

return SNMP_ERR_NOERROR;

}

/* User-defined data access functions (per column) for data in table ExampleTable */

/*

* NOTE:

* - these get_ routines MUST return data that will not be freed (ie,

* use static variables or persistent data). It will be copied, if

* needed, immediately after the get_ routine has been called.

* - these SET routines must copy the incoming data and can not take

* ownership of the memory passed in by the val pointer.

*/

/** XXX: return a data pointer to the data for the MachineNumber column and set

ret_len to its proper size in bytes. */

long *get_MachineNumber(void *data_context, size_t *ret_len) {

struct ExampleTable_entry *entry=(struct ExampleTable_entry *)data_context;

long_ret=entry->MachineNumber;

*ret_len=sizeof(long_ret);

return &long_ret; /** XXX: replace this with a pointer to a real value */

}

/** XXX: return a data pointer to the data for the MachineStatus column and set

ret_len to its proper size in bytes. */

char *get_MachineStatus(void *data_context, size_t *ret_len) {

struct ExampleTable_entry *entry=(struct ExampleTable_entry *)data_context;

*ret_len=strlen(entry->MachineStatus);

return entry->MachineStatus; /** XXX: replace this with a pointer to a real value */

}

/** XXX: return a data pointer to the data for the CheckTime column and set

ret_len to its proper size in bytes. */

u_long *get_CheckTime(void *data_context, size_t *ret_len) {

struct ExampleTable_entry *entry=(struct ExampleTable_entry *)data_context;

long_ret=entry->CheckTime;

*ret_len=sizeof(long_ret);

return &long_ret; /** XXX: replace this with a pointer to a real value */

}

/** XXX: return a data pointer to the data for the MonSet column and set

ret_len to its proper size in bytes. */

long *get_MonSet(void *data_context, size_t *ret_len) {

struct ExampleTable_entry *entry=(struct ExampleTable_entry *)data_context;

long_ret=entry->MonSet;

*ret_len=sizeof(long_ret);

return &long_ret; /** XXX: replace this with a pointer to a real value */

}

/** XXX: Set the value of the MonSet column and return

SNMP_ERR_NOERROR on success

SNMP_ERR_XXX for SNMP deterministic error codes

SNMP_ERR_GENERR on generic failures (a last result response). */

int set_MonSet(void *data_context, long *val, size_t val_len) {

FILE *fp;

struct ExampleTable_entry *temp=ExampleTable_head,*entry=data_context;

memcpy(&entry->MonSet, val, val_len);

fp=fopen("data.txt","w+");

if(!fp)

{DEBUGMSGTL(("set_MonSet","Open file failure\n"));

return SNMP_ERR_NOACCESS;

}

else

while(temp)

{

fprintf(fp,"\n%d %s %d %d",temp->MachineNumber,

temp->MachineStatus,

temp->CheckTime,

temp->MonSet);

temp=temp->next;

}

fclose(fp);

return SNMP_ERR_NOERROR; /** XXX: change if an error occurs */

}