

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_com.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"
#include<file_comp/file_comp.h>

extern global_data_t gd;

extern pthread_mutex_t fd_mutex;




// LIFEを送り、その返事から各設定ファイルをとってくるアクションを得る関数
int life_put(void)
{
int stat, interval;

stat = 0;

char * life_data = (char *)calloc(IP_LEN, sizeof(char));
char * rec = (char *)calloc(IP_LEN, sizeof(char));

interval = (gd.bf.life_int + gd.bf.life_los + INT_PLUS);

life_data = safe_sprintf(life_data, BUF_MAX, "%s :: COM: LIFE DATA: --\n", gd.bf.c_name);

//pthread_mutex_lock(&fd_mutex);
safe_pthread_mutex_lock(&fd_mutex);
if((stat = socket_send(gd.ip.sk, life_data, interval)) > 0){
   rec = select_fdcat(gd.ip.sk, rec, &stat, BUF_MAX, interval);
   #ifdef LIFE_DEBUG
   printf("LIFE: life_put(): LIFE SEND> %s\n", life_data);
   printf("LIFE: life_put(): LIFE GET> %s\n", rec);
   #endif
   }
if(stat < 0){
   safe_exit("life_put(1): socket err exit \n");
   }
pthread_mutex_unlock(&fd_mutex);


if(0 == memcmp(rec, "CHECK_", 6)){

   stat = check_alget(rec);

   free(life_data);
   free(rec);
   return stat;
   }
else {
   free(life_data);
   free(rec);
   return stat;
   }


// Not reachable (ここには到達しません)

#ifdef FREE_BUG
printf("FREE: single life_put(17): life_data %s\n", life_data);
printf("FREE: single life_put(17): rec %s\n", rec);
#endif
free(life_data);
free(rec);

return stat;
}





