/*  $Id: netcomputer.cpp,v 1.11 2003/11/27 20:31:42 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library 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
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#include "API/Core/System/error.h"
#include "API/Network/NetSession/netcomputer.h"
#include "API/Network/NetSession/netsession.h"
#include "netcomputer_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_NetComputer construction:

CL_NetComputer::CL_NetComputer()
: impl(0)
{
}

CL_NetComputer::CL_NetComputer(const CL_NetComputer &copy)
: impl(copy.impl)
{
	if (impl) impl->add_ref();
}

CL_NetComputer::~CL_NetComputer()
{
	if (impl) impl->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetComputer attributes:

CL_IPAddress CL_NetComputer::get_address() const
{
	if (impl) return impl->address;
	return CL_IPAddress(0);
}

CL_NetSession CL_NetComputer::get_session()
{
	if (impl == 0)
		throw CL_Error("CL_NetComputer::get_session() not allowed on an unattached netcomputer object");

	return CL_NetSession(impl->netsession);
}

bool CL_NetComputer::is_disconnected() const
{
	if(impl == 0)
		return true;
	else
		return impl->disconnected;
}

const std::string &CL_NetComputer::get_disconnect_reason() const
{
	static std::string null_msg("Unattached netcomputer object");

	if (impl == 0)
		return null_msg;
	else
		return impl->disconnect_reason;
}

bool CL_NetComputer::operator == (const CL_NetComputer &other_instance) const
{
	return (impl == other_instance.impl);
}

bool CL_NetComputer::operator < (const CL_NetComputer &other_instance) const
{
	return (impl < other_instance.impl);
}

bool CL_NetComputer::operator > (const CL_NetComputer &other_instance) const
{
	return (impl > other_instance.impl);
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetComputer operations:

void CL_NetComputer::disconnect(std::string reason)
{
	impl->disconnect(reason);
}

void CL_NetComputer::send(const std::string &packet_channel, const CL_NetPacket &packet, bool reliable)
{
	if (impl == NULL)
		throw CL_Error("CL_NetComputer::send not allowed on an unattached netcomputer object");

	if (reliable)
	{
		impl->send_packet(packet_channel, packet.impl);
	}
	else
	{
		impl->send_packet_udp(packet_channel, packet.impl);
	}
}

CL_NetComputer &CL_NetComputer::operator = (const CL_NetComputer &other_instance)
{
	if (impl) impl->release_ref();
	impl = other_instance.impl;
	if (impl) impl->add_ref();
	return *this;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetComputer implementation:

CL_NetComputer::CL_NetComputer(class CL_NetComputer_Generic *impl)
: impl(impl)
{
	if (impl) impl->add_ref();
}
