/*
Copyright (c) 2000                      RIPE NCC


All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of the author not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
-------------------------------------------------------------------------------
Module Header
Filename          : TestBoxConfig.C
Author            : Rene Wilhelm
Date              : 17-OCT-2000
Revised for Linux : 15-JUN-2001
Description       : Implementation of TestBoxConfig class
Language Version  : C++
OSs Tested        : Solaris 2.6 , Debian Linux 2.2 
Todo		  : implement GetBoxByName method

This class provides an interface between analysis software and the
configuration of the TTM network (which boxes are on-line, what are
their names and ids, what are the targets for measurements from a
given box etc.). It is still in the early stages of development and will
evolve as needs arise.

$Id: TestBoxConfig.C,v 1.8 2002/08/09 14:32:41 ttraffic Exp $
-------------------------------------------------------------------------------
*/

#include <errno.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include "TestBoxConfig.h"

static char const rcsid[] = "$Id: TestBoxConfig.C,v 1.8 2002/08/09 14:32:41 ttraffic Exp $";


// ------------------------------------
// GetTargets	- find active targets for given test box id
//
// Input:	reference to source TestBox struct
// Output:	targetlist (array of pointers to TestBox structs);
//		as updated in source TestBox struct.
//	        
//
// Currently we have a full mesh, every active box sending to and
// receiving from every other active box. However, scaling problems
// could prevent us from continuing this in future.
//
// In anticipation of such a change, all analysis software should 
// be flexible and get the 'target' information via this method
// instead of implicitly assuming a full measurement mesh.

TestBox **TestBoxConfig::GetTargets(TestBox *source) {

	if (source->targetlist == NULL) {
		source->number_of_targets=number_of_boxes-1;
		source->targetlist=new TestBox* [source->number_of_targets];

		int target=0;
		for (int i=0; i<number_of_boxes; i++) {
			if (tblist[i].id != source->id) {
				source->targetlist[target] = &tblist[i];
				target++;
			}
		}
	}
	return(source->targetlist);
}
			
// ------------------------------------
//
// GetBoxById	-  Get TestBox struct associated with given numeric id

TestBox *TestBoxConfig::GetBoxById(uint id) { 

	// simple linear search; replace by hash in future?	

	for (int i=0; i<number_of_boxes; i++) {
		if (tblist[i].id == id) {
			return(&tblist[i]);
			
		}
	}
	return(NULL); // not found ??
}

// ------------------------------------
// TestBoxConfig constructor: 
//
// Input: 
//	command   -  command to run to get list of active testboxes
// 
// as a minimum, lines should have box id in first and box name
// in second field. ohter fields as well as commented lines (#)
// are ignored.

#define MAXWORDS 20
#define MAXCHARS 200

TestBoxConfig::TestBoxConfig(const char *command) { 

	char    line[MAXCHARS];
	char   *point = &line[0];
	char  **ap;
	char   *arg[MAXWORDS];
	FILE   *fp;
	
	number_of_boxes = 0;
	
	// allocate some memory
	list_size = 25;
	tblist = (TestBox *) malloc(sizeof(TestBox) * list_size);

	/* Read configuration data from pipe */

	if ((fp = popen (command, "r")) == NULL) {
		cerr << "ERROR: TestBoxConfig: Cannot open pipe " << command << endl;	 
		exit(1);
	}


// entry point for retried fgets()
tryagain:  

	/* Read data */
	while ( (point=fgets(line, MAXCHARS, fp)) != NULL ) {

#ifdef DEBUG
		cout << line ;
#endif
		/* # sign indicates comment */
		if (line[0] == '#') { continue; }

		/* Split line into pieces */
		ap = arg;
		*ap = strtok (point, " \t");
		while (*ap != NULL) {
			if (**ap != '\0') {
				++ap;
			}
			*ap = strtok (NULL, " \t");
		}
	
		uint id = atoi(arg[0]);
		
		// check if we've seen this one before
		// yes, skip it: first name found is real name
		// and we do not yet care about IP addresses

		int found = 0;
		for (int i=0; i< number_of_boxes; i++) {
			if (tblist[i].id == id) {
				found = 1;
				break;
			}
		}

		if (found) continue;

		int length = strlen(arg[1]);
		char *last = arg[1] + length - 1;

		if (*last == '\n') {
			*last = '\0';
			length--;
		}

		// new entry
		tblist[number_of_boxes].number_of_targets = 0;
		tblist[number_of_boxes].targetlist = NULL;
		tblist[number_of_boxes].id = id;
		tblist[number_of_boxes].name = new char[length+1];
		strcpy (tblist[number_of_boxes].name, arg[1]);
		number_of_boxes++;

		if (number_of_boxes == list_size) {
			list_size+=25;
			tblist = (TestBox *) realloc(tblist, sizeof(TestBox) * list_size);
		}
			
	}

	if (number_of_boxes == 0) {
#ifdef DEBUG
		perror("fgets");
#endif
		if (ferror(fp)) {       // A system error occurred 
	        	 clearerr(fp);
	        	 if (errno == EINTR) {
#ifdef DEBUG
				// Retry
				cout << "fgets intterupted! retrying ... \n"; 
#endif
				goto tryagain;
		         }
		}
	}

	pclose (fp);

}
