📄 ga_server.c
字号:
/* ga_server.c *//* Copyright 2004-2007 Oswaldo Morizaki Hirakata*//* This file is part of ga-nn-ag. ga-nn-ag 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. ga-nn-ag 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 ga-nn-ag; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include "my_header.h"#include "aux_prot.h"#include "sig_usr1.h"#include "sig_alarm.h"#include "sig_chld.h"int main(void){int k,l,m,n;int poblation;int counter;int max_fd;int ready_fd;int generations;int max_neuron_mut;int gen;int exit_flag;int agr;int lifespan;int local;int max_local;int num_hosts;int connfd_remote;int connfd_niche;int num_niche;int niche_flag;int test_flag;int local_server_port;int remote_server_port;pid_t pid;pid_t pid_temp;socklen_t clilen;int *listenfd;int *connfd;int *connected;int *num_neuron;int *new_num_neuron;int *inner_elem1;int *inner_elem2;float base;float boost;float mut_neuron_prob;float mut_net_prob;float level;float mut_new_prob;float max_local_f;float poblation_f;float inm_prob;float *fitness;double *error;int **parents;float **vertice;fd_set read_set,write_set,ex_set;struct sigaction sig_usr_action;struct timespec timeout;struct sockaddr_un cli_un_addr, serv_un_addr;struct sockaddr_in cli_in_addr, serv_in_addr;struct sockaddr_in serv_in_addr_remote;struct ga_server_config ga_conf;struct ga_server_config ga_conf_temp;struct individual ** net_array;struct neuron ** parent1;struct neuron ** parent2;char char_temp[BUFFSIZE];char ** char_vector;/* Change directory */getcwd(char_buffer, BUFFSIZE);chdir(char_buffer);if( (pid = fork()) <0 ){ printf("Error fork: %s\n",strerror(errno)); }else if (pid != 0){ printf("ga_server started pid=%d\n",getpid()); exit(0);}setsid();openlog("ga_server",LOG_PID,LOG_LOCAL0);syslog(LOG_INFO,"Daemon started");load_ga_server_config(&ga_conf, "ga_server_config");poblation = ga_conf.poblation;generations = ga_conf.generations;boost = ga_conf.boost;base = ga_conf.base;max_neuron_mut = ga_conf.max_neuron_mut;mut_new_prob = ga_conf.mut_new_prob;mut_neuron_prob = ga_conf.mut_neuron_prob;mut_net_prob = ga_conf.mut_net_prob;level = ga_conf.level;agr = ga_conf.agr;lifespan = ga_conf.lifespan;local_server_port = ga_conf.local_server_port;remote_server_port = ga_conf.remote_server_port;max_local = ga_conf.max_local;num_hosts = ga_conf.num_hosts;inm_prob = ga_conf.inm_prob;poblation_f = poblation;max_local_f = max_local;num_niche = ga_conf.num_niche;if (!strncmp(ga_conf.ip,"127.0.0.1",9)){ local = 1;}else{ local = 0;}if ((poblation / max_local) > num_hosts + 1){ syslog(LOG_CRIT,"Not enough hosts"); return(1);}if (ga_conf.num_niche > 0){ if ( mkfifo("niche_fifo",FILE_MODE) <0) { syslog(LOG_CRIT,"Error creating fifo for local NICHE SERVER:%s",strerror(errno));// return(1); } }exit_flag=0;niche_flag=0;/*** Start of LOCAL NICHE Server for remote connections ***//*** Fork NICHE Server (num_niche > 0) ***/if (ga_conf.num_niche > 0){ if ((pid = fork()) < 0) { syslog(LOG_CRIT,"Error in NICHE SERVER, going without it"); } else if (pid == 0) { syslog(LOG_INFO,"pid NICHE SERVER: %d",getpid()); if( !(connfd = (int *)calloc(1,sizeof(int)) )) { syslog(LOG_CRIT,"Error calloc connfd for local NICHE SERVER: %s",strerror(errno)); return(1); } if ( !(listenfd = (int *)calloc(1,sizeof(int)) )) { syslog(LOG_CRIT,"Error calloc listenfd local NICHE SERVER: %s",strerror(errno)); return(1); } if ( !(connected = (int *)calloc(1,sizeof(int)) )) { syslog(LOG_CRIT,"Error calloc connected local NICHE SERVER: %s",strerror(errno)); return(1); } if ( !(num_neuron = (int *)calloc(poblation,sizeof(float)) )) { syslog(LOG_CRIT,"Error calloc num_neuron: %s",strerror(errno)); return(1); } if ( !(new_num_neuron = (int *)calloc(poblation,sizeof(float)) )) { syslog(LOG_CRIT,"Error calloc num_neuron: %s",strerror(errno)); return(1); } if ( !(net_array = (struct individual **)calloc(poblation,sizeof(struct individual *)) )) { syslog(LOG_CRIT,"Error calloc net_array: %s",strerror(errno)); return(1); } for (k=0; k< poblation; k++) { if ( !(net_array[k] = (struct individual *)calloc(1,sizeof(struct individual)) )) { syslog(LOG_CRIT,"Error calloc net_array[%d] in SLAVE: %s",k,strerror(errno)); return(1); } } /*** Listening socket for remote niche connections ***/ if ( (listenfd[0] = socket(PF_INET, SOCK_STREAM, 0)) < 0) { syslog(LOG_CRIT,"Error creating listen socket in NICHE SERVER"); return(1); } bzero(&serv_in_addr,sizeof(serv_in_addr)); serv_in_addr.sin_family = PF_INET; if (inet_pton(AF_INET,ga_conf.ip,&serv_in_addr.sin_addr) < 1) { syslog(LOG_CRIT,"Error inet_pton() in NICHE SERVER %s",strerror(errno)); return(1); } serv_in_addr.sin_port = htons((uint16_t)(local_server_port)); if ( (bind(listenfd[0], (struct sockaddr *) &serv_in_addr, sizeof(serv_in_addr)) ) < 0) { syslog(LOG_CRIT,"Error Binding socket in NICHE SERVER: %s",strerror(errno)); return(1); } if ( (listen(listenfd[0],LISTENQ)) <0 ) { syslog(LOG_CRIT,"Error listenning in NICHE SERVER %s",strerror(errno)); return(1); } /*** Open niche_fifo in read_only mode ***/ if( (connfd_niche=open("niche_fifo",O_RDONLY, 0)) <0) { syslog(LOG_CRIT,"Error openning niche_fifo: %s",strerror(errno)); return(1); } FD_ZERO(&read_set); FD_ZERO(&write_set); FD_ZERO(&ex_set); FD_SET(connfd_niche, &read_set); max_fd = connfd_niche + 1; if ( (ready_fd=pselect(max_fd, &read_set, &write_set, &ex_set, NULL, NULL)) <0 ) { syslog(LOG_CRIT,"Error in pselect in local NICHE SERVER: %s",strerror(errno)); return(1); } if (FD_ISSET(connfd_niche, &read_set) ) { /*** Setting sigaction to handle SIGUSR1 ***/ syslog(LOG_INFO,"Setting SIGUSR1 handler"); sigemptyset(&sig_usr_action.sa_mask); sig_usr_action.sa_handler = sig_usr1; sig_usr_action.sa_flags = 0; sig_usr_action.sa_flags |= SA_RESTART; sig_usr_action.sa_flags |= SA_NOMASK; if (sigaction(SIGUSR1, &sig_usr_action, NULL) < 0) { syslog(LOG_CRIT,"Couldn't set SIGUSR1 handler"); return(1); } /*** Jump point for data reload ***/ if (sigsetjmp(jump_niche,0) != 0) { syslog(LOG_INFO,"Cleaning net_array"); for (k=0; k< poblation; k++) { if ( (net_array[k]->neuron_array = (struct neuron **)free_neuron_array( num_neuron[k],net_array[k]->neuron_array) )) { syslog(LOG_CRIT,"Error in free_neuron_array net_array[%d]->neuron_array",k); return(1); } } } /*** Wait niche_fifo to be ready for reading ***/ syslog(LOG_INFO,"NICHE SERVER Data Loading"); for (k=0; k< poblation; k++) { if( (readn(connfd_niche,char_buffer,BUFFSIZE)) < 0) { syslog(LOG_CRIT,"Error reading niche_fifo"); return(1); } if ( !(strncmp("neuron_array",char_buffer,12) ) ) { if (readn(connfd_niche,char_buffer,BUFFSIZE) <0) { syslog(LOG_CRIT,"Error reading niche_fifo"); return(1); } num_neuron[k] = atoi(char_buffer+11); syslog(LOG_INFO,"client=%d num_neuron=%d",k,num_neuron[k]); //required for bug if ( !(net_array[k]->neuron_array = (struct neuron **)calloc( num_neuron[k],sizeof(struct neuron *)) )) { syslog(LOG_CRIT,"Error calloc net_array[%d]->neuron_array: %s",k, strerror(errno)); return(1); } for (m=0; m< num_neuron[k]; m++) { if ( !(net_array[k]->neuron_array[m] = (struct neuron *)calloc( 1,sizeof(struct neuron)) )) { syslog(LOG_CRIT,"Error calloc net_array[%d]->neuron_array[%d]: %s",k,m, strerror(errno)); return(1); } } /*** read from FIFO net_array ***/ read_neuron_array(num_neuron[k], net_array[k]->neuron_array, connfd_niche); } } } /*** Setting sigaction to handle SIGCHLD ***/ syslog(LOG_INFO,"Setting SIGCHLD handler"); sigemptyset(&sig_usr_action.sa_mask); sig_usr_action.sa_handler = sig_chld; sig_usr_action.sa_flags = 0; sig_usr_action.sa_flags |= SA_RESTART; sig_usr_action.sa_flags |= SA_NOMASK; if (sigaction(SIGCHLD, &sig_usr_action, NULL) < 0) { syslog(LOG_CRIT,"Couldn't set SIGCHLD handler"); return(1); } /*** Niche server ready to accept remote connections ***/ syslog(LOG_INFO,"NICHE SERVER accepting connections"); while (1) { clilen = sizeof(cli_in_addr); if ( (connfd[0] = accept(listenfd[0], (struct sockaddr *) &cli_in_addr, &clilen)) <0 ) { if (errno == EINTR) { continue; } else { syslog(LOG_CRIT,"Accept error Inmigration Server: %s",strerror(errno)); return (1); } } if ((pid = fork())< 0) { syslog(LOG_CRIT,"Fork error Inmigration Server"); return(1); } else if(pid == 0) { close(connfd_niche); close(listenfd[0]); /*** INMIGRATION CODE HERE ***/ l = dice_toss(pid,poblation); dwrite_neuron_array(num_neuron[l], net_array[l]->neuron_array, connfd[0]); close(connfd[0]); exit(0); } /*** Thread Code ***/ /* */ } } pid_temp = pid; // pid of local NICHE SERVER}if (ga_conf.num_niche >0){ syslog(LOG_INFO,"pid NICHE SERVER %d (for reload)",pid_temp);}/*** Setup connections hooks for remote clients ***//**************************************************/if( !(connfd = (int *)calloc(poblation,sizeof(int)) )){ syslog(LOG_CRIT,"Error calloc connfd: %s",strerror(errno)); return(1);}if ( !(listenfd = (int *)calloc(poblation,sizeof(int)) )){ syslog(LOG_CRIT,"Error calloc listenfd: %s",strerror(errno)); return(1);}if ( !(connected = (int *)calloc(poblation,sizeof(int)) )){ syslog(LOG_CRIT,"Error calloc connected: %s",strerror(errno)); return(1);}for (k=0; (k < max_local) && (k< poblation); k++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -