void* _send_buffer_monitor(void* arg) { int buf_idx = *(int*)arg; Message_t msg; Client_t* c; for (;;) { sem_wait(&g_executor.send_buffer_num_[buf_idx]); if (Queue_Pop(g_executor.send_buffer_[buf_idx], &msg) == QUEUE_OK) { if ((c = Map_GetPtr(g_executor.clients, &msg.sockfd)) == NULL) { //Clients already gone Log(LOG_NOTICE,"[SEND FAIL]Client Already Gone"); ylfree(msg.package); continue; } if (msg.trysend_cnt) { usleep(100000); } int nsend = netWrite(NULL, msg.sockfd, (char*) msg.package, msg.pkg_len); if (nsend == ERR_NETWORK_TRY_AGAIN&& ++msg.trysend_cnt < MAX_SEND_TRY) { Queue_Push(g_executor.send_buffer_[buf_idx] , &msg); sem_post(&g_executor.send_buffer_num_[buf_idx]); } else { ylfree(msg.package); } } } return NULL; }
void* ylutfile_fread(unsigned int* outsz, void* f, int btext) { unsigned int osz = YLErr_io; unsigned char* buf = NULL; FILE* fh = (FILE*)f; unsigned int sz; /* do not check error.. very rare to fail!! */ if (0 > fseek(fh, 0, SEEK_END)) { osz = YLErr_io; goto bail; } sz = ftell(fh); if (0 > sz) { osz = YLErr_io; goto bail; } if (0 > fseek(fh, 0, SEEK_SET)) { osz = YLErr_io; goto bail; } /* handle special case - empty file */ if (0 == sz) buf = (btext)? (unsigned char*)ylmalloc(1): NULL; else { /* +1 for trailing 0 */ buf = ylmalloc((unsigned int)sz + ((btext)? 1: 0)); if (!buf) { osz = YLErr_out_of_memory; goto bail; } if (1 != fread(buf, sz, 1, fh)) { osz = YLErr_io; goto bail; } } if (btext) { osz = sz+1; buf[sz] = 0; /* add trailing 0 */ } else osz = sz; /* check case (reading empty file) */ if (!buf) osz = YLOk; if (outsz) *outsz = osz; return buf; bail: if (outsz) *outsz = osz; if (buf) ylfree(buf); return NULL; }
void* _recv_buffer_monitor(void* arg) { int buf_idx = *(int*)arg; Message_t msg; for(;;) { sem_wait(&g_executor.recv_buffer_num_[buf_idx]); if (Queue_Pop(g_executor.recv_buffer_[buf_idx] , &msg) == QUEUE_OK) { DistributeMsg(&msg); ylfree(msg.package); } } return NULL; }
static void _release_client(int fd , int type) { static char* type_map[] = { "By Client", "By Pluse", "By Error" }; if (type > 2 || type < 0) { type = RELEASE_BY_ERROR; } Client_t* stored_client = Map_GetAndDel(g_executor.clients , &fd); if (stored_client == NULL) { return; } StdMutex_Lock(stored_client->oplock); Log(LOG_NOTICE,"[RELEASE_CLIENT][%s:%d] type:[%s]" , stored_client->addr , stored_client->port , type_map[type]); Event_DeleteFileEvent(g_executor.evloop , stored_client->fd , EV_READABLE); close(stored_client->fd); StdMutex_Unlock(stored_client->oplock); StdMutex_Destroy(stored_client->oplock); ylfree(stored_client); }
int ExecutorLoadConfig(const char* filename) { g_executor.port_ = DEFAULT_EXECUTOR_RUN_PORT; g_executor.pluse_interval_ = DEFAULT_EXECUTOR_PLUSE_INTVAL; g_executor.max_concurrent_task_ = DEFAULT_EXECUTOR_CONCURRENT; g_executor.log_level = LOG_DEBUG; int tmp_log_path_len = strlen(DEFAULT_EXECUTOR_LOG_PATH) + strlen(DEFAULT_EXECUTOR_LOG_FILE) + 1; g_executor.log_path_ = ylmalloc(tmp_log_path_len); snprintf(g_executor.log_path_, tmp_log_path_len, "%s%s", DEFAULT_EXECUTOR_LOG_PATH, DEFAULT_EXECUTOR_LOG_FILE); char* tmp_server = ylstrdup(DEFAULT_EXECUTOR_STORAGE_SERVER); char* str_pos = strstr(tmp_server, ":"); if (!str_pos || !is_numeric(str_pos + 1)) { /*this should not be happened, unless someone had written some incorrect code*/ ylfree(g_executor.log_path_); ylfree(tmp_server); return ERR_EXECUTOR_CONFIG_INCORRECT; } *str_pos++ = '\0'; snprintf(g_executor.store_server_addr_, IP_ADDR_LEN, "%s", tmp_server); g_executor.store_server_port_ = atoi(str_pos); ylfree(tmp_server); g_executor.save_result_to = ylstrdup(DEFAULT_EXECUTOR_SAVE_RESULT_TO); //Read Configure File FILE* fp; if (!filename) { return 0; } if ((fp = fopen(filename , "r")) == NULL) { return ERR_EXECUTOR_CONFIG_FILE_NOT_EXIST; } char line_buffer[MAX_CONFIG_LINE]; char filter_line[MAX_CONFIG_LINE]; int c; int pos = 0; while( (c = fgetc(fp)) != EOF ) { if ('\n' == c || pos == MAX_CONFIG_LINE_LEN) { line_buffer[pos] = '\0'; //cancel the notation str_pos = strstr(line_buffer , "#"); if (str_pos) { *str_pos = '\0'; } //trim the input buffer pos = 0; str_pos = line_buffer; while(*str_pos != '\0') { if (*str_pos != ' ' && *str_pos != '\t') { filter_line[pos++] = *str_pos; } ++str_pos; } filter_line[pos] = '\0'; if (pos) { // printf("[%s]\n",filter_line); str_pos = strstr(filter_line , "="); if (!str_pos || !*(str_pos + 1)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } *str_pos++ = '\0'; if (!strncmp("port" , filter_line , pos)) { if (!is_numeric(str_pos)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.port_ = atoi(str_pos); } else if (!strncmp("concurrent_task" , filter_line , pos)) { if (!is_numeric(str_pos)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.max_concurrent_task_ = atoi(str_pos); } else if (!strncmp("loglevel" , filter_line , pos)) { int loglevel; if (!strncmp("ERROR", str_pos, 5)) { loglevel = LOG_ERROR; } else if (!strncmp("WARNING", str_pos, 7)) { loglevel = LOG_WARNING; } else if (!strncmp("NOTICE", str_pos, 6)) { loglevel = LOG_NOTICE; } else if (!strncmp("DEBUG", str_pos, 5)) { loglevel = LOG_DEBUG; } else { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.log_level = loglevel; } else if (!strncmp("pluse_interval" , filter_line , pos)) { if (!is_numeric(str_pos)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } g_executor.pluse_interval_ = atoi(str_pos); if (g_executor.pluse_interval_ < DEFAULT_EXECUTOR_PLUSE_INTVAL) { g_executor.pluse_interval_ = DEFAULT_EXECUTOR_PLUSE_INTVAL; } } else if (!strncmp("log_path" , filter_line , pos)) { int tmp_len = pos - (int)(str_pos - filter_line); if (tmp_len >= strlen(g_executor.log_path_)) { g_executor.log_path_ = ylrealloc(g_executor.log_path_ , tmp_len + 1); } snprintf(g_executor.log_path_ , tmp_len + 1, "%s" , str_pos); } else if (!strncmp("storage_server" , filter_line , pos)) { char* tmp = strstr(str_pos , ":"); if (!tmp || !is_numeric(tmp + 1)) { return ERR_EXECUTOR_CONFIG_INCORRECT; } *tmp++ = '\0'; snprintf(g_executor.store_server_addr_ , IP_ADDR_LEN , "%s" , str_pos); g_executor.store_server_port_ = atoi(tmp); } else if (!strncmp("save_result_to" , filter_line , pos)) { int tmp_len = pos - (int)(str_pos - filter_line); if (tmp_len >= strlen(g_executor.save_result_to)) { g_executor.save_result_to = ylrealloc(g_executor.save_result_to , tmp_len + 1); } snprintf(g_executor.save_result_to , tmp_len + 1, "%s" , str_pos); } else if (!strncmp("daemonize" , filter_line , pos)) { if (!strncmp(str_pos , "yes" , 3)) { g_executor.daemonize = true; } else { g_executor.daemonize = false; } } else { printf("%s\n" , filter_line); return ERR_EXECUTOR_CONFIG_UNRECOGNIZED; } } pos = 0; } else { line_buffer[pos++] = (char)c; } } return 0; }
static void _pb_free(void* alloc_data , void* ptr) { AVOID_NOUSED(alloc_data); if (!ptr) return; ylfree(ptr); }
void ExecutorFreeResult(char* ptr) { ylfree(ptr); }