/*
Copyright (c) 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	  : TestBox.C
Author	    	  : Rene Wilhelm
Date	      	  : 13-AUG-2001
Description       : TestBox class implemenation
Language Version  : C++
OSs Tested        : Solaris 2.6, Debian Linux 2.2

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

#include "TestBox.h"

#include <stdlib.h>
#include <string.h>

#ifdef DEBUG 
#include <iostream.h>
#endif

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


/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	   	:     TestBox constructor 
Side Effects    :     initialize private data members
Comments	:
-------------------------------------------------------------------------------
*/

TestBox::TestBox() {

	id = 0 ;
	name = NULL;
	shortname = NULL;
	nTargets = 0;
	targetList = NULL;

}

/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	   	  :   Initializing constructor
Input 		  :   character string 
-------------------------------------------------------------------------------
*/

TestBox::TestBox(int num, char* label) {

	id = num;
	this->SetName(label);

	nTargets = 0;
	listSize = 0;
	targetList = NULL;
}

/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	  	:   TestBox destructor
Side Effects    :   deletes ROOT TChain + dynamically created datapath string
Comments	:
-------------------------------------------------------------------------------
*/

TestBox::~TestBox() {

#ifdef DEBUG
	cout << "TestBox: " << name << " destructed\n";
	cout << "targetlist: " << targetList << "\n";
#endif
	free(targetList);	// allocated with malloc() / realloc() 

}


/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	   	  :   Set name of this box
Input 		  :   character string 
-------------------------------------------------------------------------------
*/

void TestBox::SetName(char* label) {

	char *cp;

	name = new char[strlen(label)+1];
	strcpy(name, label);

	// determine short, non-qualified hostname for use in
	// autogenerated filenames.  For uniform presentation,
	// two exceptions are hardcoded, in all other cases shortname
	// is just the first part of the fully qualified domain name

	if (strcmp(name, "osdorp.ripe.net") == 0) {
		shortname = "tt02";
	}
	else if (strcmp(name, "jordaan.ripe.net") == 0) {
		shortname = "tt04";
	}
	else {
		// shortname is first part of full hostname
		if ((cp = strchr (name, '.')) != NULL) {
			*cp = '\0';
			shortname = new char[strlen(name)+1];
			strcpy(shortname, name);
			*cp = '.';
		}
		else {
			// no '.' in hostname ?
			shortname = name;
		}
	}
}


/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	   	  :   Add a box to the list of targets for test-traffic
Input parameters  :   target - pointer to TestBox object
Comments	  :   No checks for duplicates, we assume caller knows
		      what he is doing ...
-------------------------------------------------------------------------------
*/

void TestBox::AddTarget(TestBox *target) {
	
	if (nTargets == listSize) {
		listSize += 32;
		targetList = (TestBox **) realloc(targetList,
						sizeof(TestBox *) * listSize);
	}
	
	nTargets++;
	targetList[nTargets-1] = target;

}
	
/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	   	  :   Get list of targets for this box
Output 		  :   return value is pointer to targetList 
		      number of targets is returned via reference
-------------------------------------------------------------------------------
*/

TestBox **TestBox::GetTargets(int& numtargets) {
	numtargets = nTargets;
	return(targetList);
}

/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	   	  :   Find box with specified targetId in targetList
Output 		  :   return value is pointer to relevant TestBox object
		      (or NULL if not found)
-------------------------------------------------------------------------------
*/

TestBox *TestBox::FindTargetById(uint targetid) {


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

	for (int i=0; i<nTargets; i++) {
		if (targetList[i]->GetId() == targetid) {
			return(targetList[i]);
                        
		}
	}
	return(NULL); // not found ??
}

/*
-------------------------------------------------------------------------------
Subroutine Header
Purpose	   	  :   Set list of targets to be processed for this box 
Note		  :   This list can be a subset of the list of boxes 
		      which the box was sending traffic to. 
-------------------------------------------------------------------------------
*/

void TestBox::SetTargets(int numtargets, TestBox **targets) {

	if (numtargets > listSize) {
		listSize = numtargets;
		targetList = (TestBox **) realloc(targetList,
						sizeof(TestBox *) * listSize);
	}

	nTargets = numtargets ;
	for (int i=0; i < nTargets; i++) {
		targetList[i] = targets[i];
	}
}
