mud++0.35/etc/
mud++0.35/etc/guilds/
mud++0.35/help/propert/
mud++0.35/mudC/
mud++0.35/player/
mud++0.35/src/interface/
mud++0.35/src/os/cygwin32/
mud++0.35/src/os/win32/
mud++0.35/src/os/win32/bcppbuilder/
mud++0.35/src/osaddon/
mud++0.35/src/util/
/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project.  All 
................................................ contributions are welcome. 
....Copyright(C).1995.Melvin.Smith.............. Enjoy. 
------------------------------------------------------------------------------
Melvin Smith (aka Fusion)         msmith@hom.net 
MUD++ development mailing list    mudpp@van.ml.org
------------------------------------------------------------------------------
cluster.cc
*/

#include "config.h"
#include "mmp.h"
#include "cluster.h"
#include "io.h"



LList< Host > cluster;
Host local;
Host master;


int Host::connect()
{
	mm_packet packet;
	socket = new Socket( SOCK_DGRAM, (const char*)host, port1 );
	if( socket->error() )
	{
		delete socket;
		socket = 0;
		tries++;
		gettimeofday( &last, 0 );
		return -1;
	}

	strcpy( packet.cmd, CMD_HELLO );
	socket->send( (char*)&packet, sizeof( packet ) );
	return 0;
}

int loadHostsFile()
{
	bool localFound = false;
	char type[ 256 ];
	char host[ 256 ];
	char name[ 256 ];
	char pass[ 256 ];
	int port1;
	int port2;
	int count = 0;

	Host * node;
	InputFile inf( "../etc/HOSTS" );

	if( !inf )
		return -1;

	while( !inf.eof() )
	{
		// Skip comment lines
		if( *inf.getword( type ) == '#' )
		{
			inf.skipline();
			continue;
		}

		if( inf.eof() )
		{
			Cout << "Registered " << count << " hosts.\n";
			if( !localFound )
			{
				Cout << "'local' entry not found in HOSTS file.\n";
				exit(0);
			}
			return 0;
		}

		inf.getword( host );
		inf.getword( name );
		inf.getword( pass );
		port1 = inf.getnum();
		port2 = inf.getnum();

		if( !strcmp( type, "local" ) )
		{
			localFound = true;
			local.setName( name );
			local.setPasswd( pass );
			local.setHost( host );
			local.setPlayerPort( port1 );
			local.setCommPort( port2 );
			continue;
		}

		node = new Host( name, pass, host, port1, port2, 0, 0 );
		cluster.addTop( node );
		Cout << "Registered node " << name << " at " << host << endl;
		count++;
		inf.skipwhite();
	}

	Cout << "Registered " << count << " hosts.\n";
	if( !localFound )
	{
		Cout << "'local' entry not found in HOSTS file.\n";
		exit(0);
	}
	return 0;
}


Host * getHostCluster( const String & id )
{
	cluster.reset();
	while( cluster.peek() )
	{
		if( cluster.peek()->getName() == id )
			return cluster.peek();
		cluster.next();
	}
	return 0;
}