/*************************************************************
/*
/* Program timeval
/*
/* This program contains an example of how the user can call
/* the routines contained in the ksnmp library.
/* It was used to perform an evaluation of the mean time
/* needed to send a run-control command and receive a first-level
/* acknowledgement to a
/* KLOE node.
/*
/* E. Pasqualucci 13-12-1995
/*
/* Modified for daqctl v2
/* E. Pasqualucci 23-5-1997
/*
/*************************************************************

/* standard include files */

#include <stdio.h>
#include <time.h>

/* SNMP include files */

#include "snmp.h"
#include "asn1.h"
#include "snmp_impl.h"
#include "snmp_api.h"
#include "snmp_client.h"
#include "party.h"
#include "context.h"
#include "view.h"
#include "acl.h"

/* specific include files */

#include "netmap.h"

/* DAQ library prototypes */

#include "ksnmplib_protos.h"

/******************************************************************/

#define MAX_NAMES      64
#define MAX_NAME_LEN  128

#define PRIVATE  "private"

main()
{
    int flag;

    struct network_map *map;

    struct port_descr *theport;
    struct node_descr *thenode;

    struct snmp_pdu     *pdu;
    struct snmp_session *ss;

/*  SNMP and other initializations */

    ksnmp_init();
    init_mib();

/*  Mapping the network and printing the map */

    ksnmp_map_network();
    map = ksnmp_map_retrieve_network_map();
    ksnmp_map_print_network_map (stdout, map);

/*  Selects the first "KLOE node" in the list */

    flag = FALSE;
    theport = map->portlist;

    while (theport)
    {
        if (theport->nodelist)
        {
            thenode = theport->nodelist;
            while (thenode)
            {
                if (thenode->snmp_sess)
                {
                    ss = thenode->snmp_sess;
                    if (!(strncmp((char *)ss->community,
                          PRIVATE,strlen(PRIVATE))))
                    {
                        flag = TRUE;
                        break;
                    }
                }
                thenode = thenode->next_node;
            }
        }
        if (flag) break;
        theport = theport->next_port;
    }

    if (!flag)
    {
        printf(" Sorry, no KLOE nodes active\n");
        exit(1);
    }

/*  If this node is not the arp_server itself or the      */
/*  gigaswitch,                                           */
/*  sets and retrieve the variable system.sysContact.0    */
/*  10000 times, calculating the time needed.             */

    if (ss != ksnmp_map_get_arp_ss() && ss != ksnmp_map_get_switch_ss())
    {
        int i, n_names;
        int *vartype;
        char *val[MAX_NAMES];
        char *names[MAX_NAMES];
        double tdiff;
        time_t t1, t2;
        time_t pt1, pt2;
        FILE *fp;

        vartype = (int *) calloc((size_t) 1, sizeof(int));
        for (i=0 ; i<4 ; i++)
        {
            names[i] = (char *) calloc((size_t) MAX_NAME_LEN, (size_t) 8);
            val[i] = (char *) calloc((size_t) MAX_NAME_LEN, (size_t) 8);
        }

        t1 = time (&pt1);

        for (i=0 ; i<10000 ; i++)
        {
            n_names = 1;
            *vartype = STRING;
            strcpy(val[0],"Pinco Pallino");
            sprintf(names[0], "system.sysContact.0");

            ksnmp_set (thenode->snmp_sess, names, n_names, vartype,
                       val);
            pdu = ksnmp_get (thenode->snmp_sess, names, n_names);
        }

        t2 = time (&pt2);
        tdiff = difftime (t2, t1);
        fp = fopen ("pippo.out","w");
        fprintf(fp," %f\n",tdiff);
        fclose (fp);
    }

/*  Closes all the SNMP sessions, frees the map, etc. */

    ksnmp_end();
}