/***************************************************************************
*   Copyright (C) 2006 by Ian Reinhart Geiser   *
*   geiseri@sourcextreme.com   *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   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.             *
***************************************************************************/
#include "nodeid.h"
#include "kcodecs.h"
#include <QByteArray>
#include <QBitArray>
#include <QDataStream>
#include <qdebug.h>
#include <qglobal.h>

KRPC::NodeID::NodeID()
{
	clear();
}


KRPC::NodeID::~NodeID()
{
}

bool KRPC::NodeID::operator <( const NodeID & str ) const
{
	int size = m_id.size();
	for( int index = 0; index < size; ++index )
	{
		if( m_id[index] < str.m_id[index])
		{
			return true;
		}
		if( m_id[index] > str.m_id[index])
		{
			return false;
		}

	}

	return false;
}

bool KRPC::NodeID::operator ==( const NodeID & str ) const
{
	return m_id == str.m_id;
}

const QByteArray KRPC::NodeID::toString( ) const
{
	int base = 16;
	QByteArray result;
	int size = m_id.size();
	for( int index = 0; index < size; ++index )
	{
		result += QByteArray::number(m_id[index], base).rightJustified(2,'0');
	}
	return result;
}

void KRPC::NodeID::clear( )
{
	m_id.fill(0,16);
}

void KRPC::NodeID::generateId( const QByteArray & source )
{
	clear();
	KMD5 context (source);
	const unsigned char* digest = context.rawDigest();

	for( int index = 0; index < 16; ++index )
		m_id[index] = digest[index];
}

KRPC::NodeID::NodeID( const QByteArray & source )
{
	generateId(source);
}

KRPC::NodeID::NodeID( const NodeID & other )
{
	m_id = other.m_id;
}

KRPC::NodeID KRPC::NodeID::operator =( const NodeID & other )
{
	m_id = other.m_id;
	return *this;
}

bool KRPC::NodeID::operator >( const NodeID & str ) const
{
	if( !(*this == str) && !(*this < str ) )
		return true;
	else
		return false;
}

QDebug operator <<( QDebug dbg, KRPC::NodeID node )
{
	dbg << node.toString().toUpper();
	return dbg;
}

QDataStream & KRPC::operator <<( QDataStream & out, const KRPC::NodeID & nodeid )
{
	out << nodeid.m_id;
	return out;
}

#include <qdebug.h>

QDataStream & KRPC::operator >>( QDataStream & in, KRPC::NodeID & nodeid )
{
	in >> nodeid.m_id;
	return in;
}

void KRPC::NodeID::registerNodeID( )
{
	qRegisterMetaType<NodeID>("KRPC::NodeID");
	qRegisterMetaTypeStreamOperators<NodeID>("KRPC::NodeID");
}







