/*
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          : RoutingInfo.C
Author            : Rene Wilhelm
Date              : 17-OCT-2000
Revised           : 19-JUN-2000, count number of route changes
Description       : Implementation of RoutingInfo class
Language Version  : C++
OSs Tested        : Solaris 2.6

RoutingInfo objects store the route vector information of measurements
between specific source and destination box.  

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

#include "RoutingInfo.h"

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

void RoutingInfo::Update(int vectorid, int nhops) { 

	if (vectorid != lastid) {
		// route changed, increase flap count
		lastid = vectorid;
		flaps++;
	}
		
	// Update: check if vector already seen, if not add it to
	// the list and update max/min hops

	for (int i=0; i<entries; i++) {
		if (vectors[i] == vectorid) {
			// already seen
			return;
		}
	}

	// new vector
	if (entries == size) {
		//have to resize
		if (size > 4000) {
			size += 2048;
		}
		else {
			size += size; // double the size
		}
		vectors = (int *) realloc (vectors, sizeof(int)*size);
	}
	vectors[entries] = vectorid;
	entries++;
	if (nhops < minhops) {
		minhops = nhops;
	}
	if (nhops > maxhops) {
		maxhops = nhops;
	}
};

void RoutingInfo::Reset() { 
	entries = 0;
	flaps   = -1;		// first vector will set this to 0
				// after that count real changes
	lastid  = -1;		// guaranteed to be different from real vector
	minhops =  999;		// first entry will set real values
	maxhops = -999;
};

RoutingInfo::RoutingInfo() { 
	
	size    = 256;
	entries = 0;
	flaps   = -1;
	lastid  = -1;
	minhops =  999;		// first entry will set real values
	maxhops = -999;
	
	// can't use "new" operator, since we want to be able to realloc 
	vectors = (int *) malloc (sizeof(int)*size);
};

