/*************************************************************
/*
/* Program commtime
/*
/* Author: E. Pasqualucci
/*
/* This program contains an example of how the user can
/* measure the time needed to send a command to a DAQ process.
/* Supposing the destination process (local or remote)
/* is in a RESUME state, it sends n times the two commands
/* pause and resume, each time getting the two acknowledgements.
/* At the end, the time spent is printed out.
/*
/* Usage:
/*
/*       commtime -p process [-n node] [-l nloop]
/*
/* Modified for daqctl v2
/* E. Pasqualucci 23-5-1997
/*
/*************************************************************/

/* standard include files */

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

/* Process template include files */

#include "template.h"
#include "dummy.h"

/* global variables */

extern char ownpname[];

int nloop = 10000;

char c_pause[] = "pause ";
char c_resume[] = "resume ";
char destination[256];

char *remote_node = (char *) NULL;
char *process = (char *) NULL;

time_t t0, t1;

void init_options (int argc, char **argv)
{
    int arg;
    int i;

    for(arg = 1; arg < argc; arg++)
        if (argv[arg][0] == '-')
            switch(argv[arg][1])
            {
                case 'p':
                    arg++;
                    process = argv[arg];
                    break;
                case 'n':
                    arg++;
                    remote_node = argv[arg];
                    break;
                case 'l':
                    arg++;
                    nloop = atoi(argv[arg]);
                    break;
                default:
                    printf("invalid option: -%c\n", argv[arg][1]);
                    break;
            }

    if (process == (char *) NULL)
    {
        fprintf (stderr, 
                 "Usage: commtime -p process [-n node] [-l nloop]\n");
        exit (0);
    }
}

void init_commtime (void)
{
    strcpy (destination, process);

    if (remote_node)
    {
        ksnmp_init();
        init_mib();
        strcat (destination, "@");
        strcat (destination, remote_node);
    }
    else
        if (Attach_Table() == -1)
        {
            printf("Error in Attach_Table()\n");
            exit(0);
        }

    strcpy (ownpname, "commtime");

    time (&t0);
}

void do_commtime (void)
{
    register int i;

    for (i=0 ; i < nloop ; ++i)
    {
        Send_Message (destination, c_resume);
        Get_Ack (destination, c_resume);
        Send_Message (destination, c_pause);
        Get_Ack (destination, c_pause);
    }
}

void end_commtime (void)
{
    time (&t1);
    printf (" nloop %d time needed %d sec\n", nloop, (int) (t1-t0));
    ksnmp_end();
}

main (int argc, char **argv)
{
    init_options (argc, argv);
    init_commtime();
    do_commtime();
    end_commtime();
}