
/*
 * gen.cc
 * Copyright (C) 2002 by John Heidemann <johnh@isi.edu>
 * $Id: gen.cc,v 1.7 2002/04/05 21:36:56 johnh Exp $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 */

/*
 * gen.cc implements generic data structures
 */

#include "gen.hh"
#include "main.hh"

AddrPrefix::AddrPrefix(int a, int b, int c, int d, int mask)
{
	addr_ = 0;
	addr_ = (addr_ << 8) + (a & 0xff);
	addr_ = (addr_ << 8) + (b & 0xff);
	addr_ = (addr_ << 8) + (c & 0xff);
	addr_ = (addr_ << 8) + (d & 0xff);
	prefix_ = mask;
}

ostream&
operator<< (ostream& os, AddrPrefix& ap)
{
	return os << ((ap.addr_>>24) & 0xff) << "."
		  << ((ap.addr_>>16) & 0xff) << "."
		  << ((ap.addr_>>8) & 0xff) << "."
		  << ((ap.addr_>>0) & 0xff) << "/"
		  << ap.prefix_;
}



ostream&
operator<< (ostream& os, RouterStats& s)
{
	static char *stat_names[] = { "sent", "received", "dropped", NULL };
	for (int i = 0; i < RouterStats::LAST_STAT; i++) {
		os << "messages-" << stat_names[i] << " " << s.stats_[i] << endl;
	};
	return os;
}



bool
Route::operator<(Route&b)
{
	if (med_ != b.med_)
		return med_ < b.med_;  // note: LOWER med is better!
	if (hops_ != b.hops_)
		return hops_ < b.hops_;
	if (next_hop_ != b.next_hop_)
		return next_hop_ < b.next_hop_;
	return false;
}

ostream&
operator<< (ostream& os, Route& rte)
{
	// output is id prefix/size next-hop distance
	// xxx: id is not currently there
	//os << rte.origin_id_ << " " << *rte.ap_ << " " << rte.next_hop_ << " " << rte.hops_;
	// next line includes (med) for debugging
	 os << rte.origin_id_ << " " << *rte.ap_ << " " << rte.next_hop_ << " " << rte.hops_ << " (med=" << rte.med_ << ")";
	return os;
}
