/***************************************************************************
 *   Copyright (C) 2005 by xavier helluy   *
 *   xavier.helluy@web.de   *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include <kdebug.h> 
 
#include "client.h"
#include "serverclient.h"
#include <kresolver.h> 
#include <sys/socket.h>
#include <unistd.h>    //02.06.05 Jol  sleep or QThread::sleep (protected) 

Client::Client(const QCString & socketPath)
	: ServerClient() , m_socketPath(socketPath)
{
}

int Client::init()
{
	kdDebug() << "Client::init() started " << endl;

	KNetwork::KUnixSocketAddress  AddrSocket(m_socketPath );
	const KNetwork::KResolverEntry   mySocket(AddrSocket ,SOCK_STREAM , PF_UNIX  , "unknown" ,(QCString) "unknown" ); 
// 	m_socket = m_device.socketDevice();
	m_socket = new KNetwork::KStreamSocket(); 
// 	m_socket->create(SOCK_STREAM , PF_UNIX , 0);
// 	m_socket->setSocketOptions(KNetwork::KSocketBase::Blocking);
	m_socket->setBlocking(TRUE);
	
	if ( ! m_socket->connect(mySocket) )
	{
		kdDebug() << "Client::init() Bad connection ? " << endl;
		sleep(1);
		
		if ( ! m_socket->connect(mySocket) )
		{
			sleep(4);
			if ( ! m_socket->connect(mySocket) )
			{
				kdWarning() << "Client::init() Again, bad connection ! " << endl;
				return -1;			
			}
		}
		
	}
		
	//Wait and Read Invitation "Are you Ready ? from server"
	if ( ! getSignal( (QString) "Are you ready ?"))
	{
		kdWarning() << "Client::init()   Client got Bad Invitation. I stop! " << endl;
		return -2;
	}
	else 
	{
		kdDebug() << "Client::init()   Client got GOOD Invitation. " << endl;
		sendSignal( (QString) "I am ready.");
	}
	if ( ! getSignal( (QString) "OK"))
	{
		kdWarning() << "Client::init()   Client did not send OK message. I stop " << endl;
		return -3;
	}
	kdDebug() << "Client::init()   Client send OK signal." << endl;
	return 1;
}

Client::~Client()
{
	m_socket->close();
	delete m_socket;
}




