/*
....[@@@..[@@@..............[@.................. 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@falcon.mercer.peachnet.edu 
MUD++ development mailing list    mudpp-list@spice.com
------------------------------------------------------------------------------
node.h
*/

#include "socket.h"

class Node
{
	protected:
		char name[ 64 ];
		char passwd[ 64 ];
		char host[ 128 ];
		int port;
		int tries;
		int maxtries; // how many times should we try connection
		time_t last; // last connect attempt
		time_t timeout;	// time in between tries
		Socket * socket; // set if connected otherwise null

	public:
		Node()
		:	port(0), tries(0), maxtries(0), timeout(0), socket(0)
		{
			*name = *passwd = *host = '\0';
		}

		Node( char *n, char *p, char *h, int p, int t, int secs )
		:	port(p), tries(t), socket(0)
		{
			strcpy( name, n );
			strcpy( passwd, p );
			strcpy( host, h );
			timeout.tv_sec = secs;
			timeout.tv_usec = 0;
		}

		char *getName();
		char *getPasswd();
		char *getHost();
		int getPort();

		int connect();
		Socket * connected() { return socket; }
		int write( char * str );
};

inline int Node::connect()
{
	socket = new Socket( host, port );

	if( socket->error() )
	{
		delete socket;
		socket = 0;
		tries++;
		last = time( 0 );
		return;
	}

	socket->connect();

	if( socket->error() )
	{
		delete socket;
		socket = 0;
		tries++;
		last = time( 0 );
		return;
	}


	socket->write( name );
	socket->write( "\n\r" );
	socket->write( passwd );
	socket->write( "\n\r" );
}

inline int Node::write( const char * str )
{
	if( socket )
		return socket->write( str, strlen( str ) );
	return -1;
}