/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  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.
 *
 *
 *                      ********************
 *
 *
 *  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 Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  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 <semaphore.h>
#include <sys/types.h>
#include <unistd.h>
#include "Semaphore.h"
#include "Info.h"
#include "Com/Commun.h"
#include "Com/Initialisation_Com.h"
#include "Debug.h"
#include "Thread.h"
#include "Macro.h"

void Info_Agent (void)
{
	struct struct_Info_Agent Message;
	gint      Envoye, Recu, Tout;

#ifdef __LOG_ENABLE
	Create_Log(CATEG_INFO_AGENT,NULL,NULL,TRUE);
#endif
	while (1) {
		sleep (1);
		PROTECT_CODE(Semaphore_Info);
		Envoye = Nb_Octet_Envoye;
		Recu = Nb_Octet_Recu;
		Tout = Nb_Machine_Dans_La_Liste;
		Nb_Octet_Envoye = 0;
		Nb_Octet_Recu = 0;
		DEPROTECT_CODE(Semaphore_Info); 
		Message.ID_Reseau = Info_Demarrage.ID_Reseau;
		Message.Octet_Envoyer = Envoye;
		Message.Octet_Recu = Recu;
		Message.Nb_Machine_Dans_La_Liste = Tout;
		Message.Nb_Machine_En_Cours_De_Scan = Info_Demarrage.Nb_Thread_Max - Valeur_Semaphore (&Semaphore_Thread_Scan);
		Message.Nb_Machine_En_Attente_De_Scan = Valeur_Semaphore (&Semaphore_Nouveau_Equipement);
		Communication_Envoie_Donnee (INFO_AGENT, (gpointer *) & Message, sizeof (Message));
	}
}

void Nouveau_Octet_Envoye (int Nb_Octet)
{
	PROTECT_CODE(Semaphore_Info);
	Nb_Octet_Envoye = Nb_Octet_Envoye + Nb_Octet;
	if (Nb_Octet < 0) {
		debug_printf (D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Nouveau_Octet_Envoye %d   +%d", Nb_Octet_Envoye, Nb_Octet);
	}
	DEPROTECT_CODE(Semaphore_Info); 
}

void Nouveau_Octet_Recu (int Nb_Octet)
{
	PROTECT_CODE(Semaphore_Info);
	Nb_Octet_Recu = Nb_Octet_Recu + Nb_Octet;
	if (Nb_Octet < 0) {
		debug_printf (D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Nouveau_Octet_Recu %d   +%d", Nb_Octet_Recu, Nb_Octet);
	}
	DEPROTECT_CODE(Semaphore_Info);
}


void Initialisation_Info (void)
{
	Nb_Octet_Envoye = 0;
	Nb_Octet_Recu = 0;
	Nb_Machine_Dans_La_Liste = 0;
	Nb_Machine_En_Cours_De_Scan = 0;
	Nb_Machine_En_Attente_De_Scan = 0;
}
