/*
Copyright (c) 2000,2001                  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
Revised		  : 10-AUG-2001, TestBox target lists initialised from constructor
Revised		  : 31-OCT-2001, Create config based on program options
Description       : Implementation of TestBoxConfig class
Language Version  : C++
OSs Tested        : Solaris 2.6, Solaris 8, 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.). 

When requested via program options, the list of boxes in a
TestBoxConfig object will be limited to a selected source or
subset of sources.

$Id: TestBoxConfig.C,v 1.3 2003/05/16 13:57:41 ttraffic Exp $
-------------------------------------------------------------------------------
*/

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

static char const rcsid[] = "$Id: TestBoxConfig.C,v 1.3 2003/05/16 13:57:41 ttraffic Exp $";
static char const *rcsid_p = rcsid;   // Prevent g++ from optimizing out rcsid

// ------------------------------------
// TestBoxConfig constructor: 
//
// Input: 
//	options   -  TTMOptions: defaults or explicitly passed on command line  
// 

#define MAXWORDS 20
#define MAXCHARS 200

TestBoxConfig::TestBoxConfig(TTMOptions &options) { 

	char    line[MAXCHARS];
	char   *point = &line[0];
	char  **ap;
	char   *arg[MAXWORDS];
	FILE   *fp;
	
	maxBoxId = 0;
	list_size = 0;
	tblist = NULL;
	number_of_boxes = 0;

	// the command which will return TestBox Configuration
	// as a minimum, lines should have box id in first and box name
        // in second field; other fields as well as commented lines (#)
	// are ignored.

	char *command = options.ConfigCommand();

	
	/* Read configuration data from pipe */

	if (options.Verbose()) {
		cout << "TestBox Config command: " << command << endl;
	}

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


tryagain:

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

		/* # 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]->GetId() == 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

		if (number_of_boxes == list_size) {
			// allocate more memory
			list_size+=32;
			tblist = (TestBox **) realloc(tblist, sizeof(TestBox *) * list_size);
		}

		number_of_boxes++;
		tblist[number_of_boxes-1] = new TestBox(id, arg[1]);


		if (id > maxBoxId) {
			maxBoxId = id;
		}
			
	}

        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);

	// now initialize the target lists
	SetTargets();

	// We have the full config, now restrict to specific src/tgt
        // if requested so by program options.

	if (options.SourceId() != 0) {
		TestBox *src = this->GetBoxById(options.SourceId());
		if (src != NULL) {
			tblist[0] = src;
			number_of_boxes = 1;
		}
		else {
			cerr << options.ProgramName() << ": ABORT: Box " <<
			options.SourceId() << " not found in testbox config" <<
			endl;
			exit (1);
		}
			
		if (options.TargetId() != 0) {
			TestBox *tgt = src->FindTargetById(options.TargetId());
			if (tgt != NULL) {
				src->SetTargets(1, &tgt);
				maxBoxId = MAX(options.SourceId(), options.TargetId());
			}
			else {
				cerr << options.ProgramName() <<
				   ": ABORT: Box " << options.TargetId() <<
				   " not configured as target of box "<<
				   options.SourceId() << endl;
				exit (1);
			}
		}
	}

	
}


// ------------------------------------
// SetTargets	- find active targets for all boxes in this Config
//
//
// 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 from TestBox.targetlist[]
// instead of implicitly assuming a full measurement mesh.

void TestBoxConfig::SetTargets() {

	for (int j=0; j < number_of_boxes; j++) {

		TestBox *source = tblist[j];
		for (int i=0; i<number_of_boxes; i++) {
			if (tblist[i]->GetId() != source->GetId()) {
				source->AddTarget(tblist[i]);
			}
		}
	}
}
			
// ------------------------------------
//
// 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]->GetId() == id) {
			return(tblist[i]);
			
		}
	}
	return(NULL); // not found ??
}
