/* ***********************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 <errno.h>
#include <stdlib.h>
#include "Commun.h"
#include "../Debug.h"
#include "Initialisation_Com.h"
#include "../Quit.h"
#include "Communication.h"
#include "../Semaphore.h"
#include "../Macro.h"

extern struct Struct_Modele_Communication Liste_Communication[];

#ifdef __DEBUG_COM
void Log_Envoie_Transmission(struct struct_Cle_Privee *Donnee_Cle,gchar *Texte_Log){
	if (Donnee_Cle!=NULL){
		Log(Donnee_Cle,Texte_Log);
		MYFREE(Texte_Log);
	}
}
#endif

gint Calculer_Ckecksum (gint * Donnee, gint Taille_Donnee)
{			
	gint      i, Ckecksum;

	ERROR_if (Donnee == NULL);
	Ckecksum = 0;
	for (i = 0; i < (Taille_Donnee / sizeof (gint)); i++)
		Ckecksum = Donnee[i] + Ckecksum;
	return Ckecksum;
}

void Communication_Envoie_Donnee (gint ID_Info, gpointer * Donnee, gint Taille_Donnee)
{
	ERROR_if (Donnee == NULL);
	PROTECT_CODE(Semaphore_Communication);
	struct struct_Info_TCP Info_TCP;
	int       NbOctet;

	Info_TCP.ID_Info = ID_Info;
	Info_TCP.Taille_Donnee = Taille_Donnee;
	Info_TCP.Ckecksum = Calculer_Ckecksum (Donnee, Taille_Donnee);
	memcpy (Info_TCP.Donnee, Donnee, Taille_Donnee);
#ifdef __DEBUG_COM

	if (Liste_Communication[ID_Info].ID_Communication!=INFO_AGENT)
		Log_Envoie_Transmission(Log_Send,g_strdup_printf("[%s] %d\n[%s] Ckecksum %08X\n",Liste_Communication[ID_Info].Debug,Taille_Donnee,Liste_Communication[ID_Info].Debug,Info_TCP.Ckecksum));
#endif
	
	
	NbOctet = send (client_id, &Info_TCP, sizeof (struct struct_Info_Entete) + Taille_Donnee, 0);
	if (NbOctet < 0) {
#ifdef __DEBUG_COM
		Log_Envoie_Transmission(Log_Send,g_strdup_printf("[%s] %d\n[%s] Ckecksum %08X   #### send NbOctet < 0  (%d)[%s]\n",Liste_Communication[ID_Info].Debug,Taille_Donnee,Liste_Communication[ID_Info].Debug,Info_TCP.Ckecksum,errno, strerror (errno)));
#endif
#ifdef __LOG_ENABLE
		Log ( Donnee_Cle_Main, "[ERROR] Commun.h   NbOctet < 0");
		Log (Donnee_Cle_Main, strerror (errno));
#endif
		DEPROTECT_CODE(Semaphore_Communication);
		Arret_Demande (NULL);
	}
	if (NbOctet != sizeof (struct struct_Info_Entete) + Taille_Donnee) {
#ifdef __DEBUG_COM
		Log_Envoie_Transmission(Log_Send,g_strdup_printf("[%s] %d\n[%s] Ckecksum %08X   #### send NbOctet(%d) != (%d)(sizeof (struct struct_Info_Entete)+Taille_Donnee) (%d)[%s]\n",Liste_Communication[ID_Info].Debug,Taille_Donnee,Liste_Communication[ID_Info].Debug,Info_TCP.Ckecksum,NbOctet,sizeof (struct struct_Info_Entete) + Taille_Donnee, errno, strerror (errno)));
#endif	
#ifdef __LOG_ENABLE
		Log ( Donnee_Cle_Main, "[ERROR] Commun.h   NbOctet != sizeof (struct struct_Info_Entete)+Taille_Donnee");
		Log (Donnee_Cle_Main, strerror (errno));
#endif
		DEPROTECT_CODE(Semaphore_Communication);
		Arret_Demande (NULL);
	}
	DEPROTECT_CODE(Semaphore_Communication);
}
