void wd_msg_handler(MVI_GENERIC_MSG_HEADER_T *Incoming) { logger_detailed("********** Incoming->modname: %s ************", modname[Incoming->moduleID]); switch(Incoming->subType) { case MVI_MSG_BARK: UpdateBarkList(Incoming->moduleID); break; case MVI_MSG_REBOOT: system("reboot"); break; case MVI_MSG_HIBERNATE: hibernate = Incoming->data; if(hibernate == HIBERNATE_OFF) { logger_info("Set Hibernate_mode: %s and reset module timer", hibernate==HIBERNATE_ON?"ON":"OFF"); register_modules(); //Reset all timer } break; default: logger_info("%s: Unknown message type %d",__FUNCTION__, Incoming->subType); break; } }
void wd_action() { int i; // static unsigned long reboot_timer = 0; unsigned long lcur_time; for(i=0; i < MVI_UNKNOWN_MODULE_ID; i++) { lcur_time = get_sys_cur_time(); if((lcur_time - modulelist[i].timer) > 20 && hibernate == HIBERNATE_OFF) logger_info("Module: %s is marginally response...", modname[modulelist[i].module_id]); else if((lcur_time - modulelist[i].timer) > ALLOWANCE_TIMER && hibernate == HIBERNATE_OFF) { // report module in trouble if((lcur_time - modulelist[i].timer) > 2*ALLOWANCE_TIMER && hibernate == HIBERNATE_OFF) { logger_info("WARNING: %s not response in: %ds. timer_start: %d", modname[modulelist[i].module_id], (lcur_time - modulelist[i].timer),modulelist[i].reboot); if(modulelist[i].reboot == 0) modulelist[i].reboot = lcur_time; if(lcur_time - modulelist[i].reboot > ALLOWANCE_TIMER) { logger_info("System about to be reboot"); //TODO system("reboot"); } } } else //reset reboot time for each module modulelist[i].reboot = 0; } }
int main(int argc, char const* argv[]) { int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address, client_address; unlink("server_socket"); server_sockfd = socket(AF_INET, SOCK_STREAM, 0); if(server_sockfd < 0) { raise("Can't create socket!!!"); return -1; } server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(DEFAULT_PORT); server_len = sizeof(server_address); logger_info("Server Ready to Start"); if(bind(server_sockfd, (struct sockaddr *)&server_address, server_len) < 0) { close(server_sockfd); raise("Can't bind socket!!!"); return -1; } listen(server_sockfd, 5); logger_info("Server started"); while(1) { char request_content[BUFFER_SIZE]; char response_content[BUFFER_SIZE]; Request req; logger_info("Server waiting request..."); client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); read(client_sockfd, request_content, BUFFER_SIZE); char verb[64], path[64]; parse_request(request_content, verb, path); req.http_verb = verb; req.path = path; log_request(&req); dispatcher(&req, response_content); write(client_sockfd, response_content, strlen(response_content)); close(client_sockfd); logger_info("Process the request successfully\n"); } return 0; }
void dispatcher(Request *req, char *result) { if ( strcmp(req->http_verb,"GET") != 0 ) { logger_info("Do not implement this operation yet\n"); do_501(result); } else if (not_exist(req->path)) { printf("Can't find request path %s\n", req->path); do_404(req->path, result); } else { printf("Find request path %s\n", req->path); do_cat_file(req->path, result); } }
void send_monitor_cmd(int from, char *cmd_str) { MVI_MONITOR_MSG_T monitor_msg; bzero((char *) &monitor_msg, sizeof(MVI_MONITOR_MSG_T)); int monitor_msgid = open_msg(MVI_MONITOR_MSGQ_KEY); if(monitor_msgid < 0) { logger_info("Error invalid message queue"); return; } monitor_msg.header.moduleID = from; monitor_msg.header.subType = MVI_MSG_SERIAL_CMD; strcpy(monitor_msg.serial_cmd, cmd_str); send_msg(monitor_msgid, (void *) &monitor_msg, sizeof(MVI_MONITOR_MSG_T), 3); }
void send_generic_msg(int from, int msg_id, int data) { MVI_GENERIC_MSG_HEADER_T msg; bzero((char *) &msg, sizeof(MVI_GENERIC_MSG_HEADER_T)); int msgid = open_msg(MVI_WD_MSGQ_KEY); if(msgid < 0) { logger_info("Error invalid message queue"); return; } msg.subType = msg_id; msg.moduleID = from; msg.data = data; send_msg(msgid, (void *) &msg, sizeof(MVI_GENERIC_MSG_HEADER_T), 3); }
void send_dog_bark(int from) { MVI_GENERIC_MSG_HEADER_T bark; // logger_info(" Rough ....rough from: %d", from); bzero((char *) &bark, sizeof(MVI_GENERIC_MSG_HEADER_T)); int msgid = open_msg(MVI_WD_MSGQ_KEY); if(msgid < 0) { logger_info("Error invalid message queue"); return; } bark.subType = MVI_MSG_BARK; bark.moduleID = from; send_msg(msgid, (void *) &bark, sizeof(MVI_GENERIC_MSG_HEADER_T), 3); }
void watchdog_main_task() { MVI_GENERIC_MSG_HEADER_T wd_msg; int msgid = create_msg(MVI_WD_MSGQ_KEY); if(msgid < 0) { logger_info("Failed to open WatchDog message"); exit(0); } register_modules(); while(1) { recv_msg(msgid, (void *) &wd_msg, sizeof(MVI_GENERIC_MSG_HEADER_T), 5); wd_msg_handler((MVI_GENERIC_MSG_HEADER_T *) &wd_msg); usleep(10000); wd_action(); } }
// 12. GET_INFO static void proc_cmd_get_info(struct mg_connection* conn, const struct mg_request_info* request_info) { logger_remotem("GET_INFO from client: %s, version %s, ID: %s", ip2s(request_info->remote_ip), getVersion(), DVR_ID); unsigned long* drive_info = getDriveInfo(DataPath); if (DVR_ID == NULL) { logger_info("No valid ID found wait for next call: %s", DVR_ID); mg_printf(conn, "errno=51010\r\n"); return; } char tb[40]; time_t now = time(NULL); struct tm tmi; strftime(tb, 20, "%Y%m%d_%H%M%S", localtime_r(&now, &tmi)); mg_printf(conn, "HTTP/1.0 200 OK\r\n\n" "serial=%s\r\n" "version=%s\r\n" "dvr_status=%s\r\n" "battery_status=%s\r\n" "product_code=FBBW1\r\n" "filesystem_size=%lu\r\n" "free_blocks=%lu\r\n" "fg_video=%d\r\n" "mac_address=%s\r\n" "mainboard=%s\r\n" "date_time=%s\r\n" "assignable=%s\r\n" "dvr_name=%s\r\n" "errno=0\r\n", DVR_ID, getVersion(), downloading_status, get_battery_level_string(), drive_info[0], drive_info[1], getVideoCount(DataPath), getMAC(1), get_hw_version(), tb, get_assignable(), get_dvr_name()); }
// 8. LOAD_CONFIG_FILE // test: HTTP://192.168.1.155/cgi-bin/Flash2AppREMOTEM_load_config_file.cgi?path=/upload/input.xml static void proc_cmd_load_config_file(struct mg_connection* conn, const struct mg_request_info* request_info) { char configXmlPath[200]; char inputXmlPath[200]; char str_file_path[200]; char *address = 0, *netmask = 0, *gateway = 0; int dhcp = 0; char* broadcast_xml_field; char*rec_pre_xml_field; int found = 0; int isNetUpdate = 0; get_qsvar(request_info, "path", str_file_path, sizeof(str_file_path)); if (strlen(str_file_path) == 0) { mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\nerrno=%d\r\n", INPUT_PARAM_NOT_SPECIFIED); return; } sprintf(inputXmlPath, "%s%s", ODI_HOME, str_file_path); sprintf(configXmlPath, "%s", ODI_CONFIG_XML); logger_remotem("load_config_file: merge XML Config File: from %s to %s", inputXmlPath, ODI_CONFIG_XML); struct my_file xmlfile = MY_STRUCT_FILE_INITIALIZER; if (!my_file_stat(inputXmlPath, &xmlfile)) { logger_remotem("No Input Config file to Load %s", inputXmlPath); send_http_error(conn, 404, "No Input Config file to Load ", "%s not found", inputXmlPath); goto leave; } if (!my_file_stat(ODI_CONFIG_XML, &xmlfile)) { logger_remotem("No destination Config file to Merge %s", ODI_CONFIG_XML); send_http_error(conn, 404, "No original config file to compare for merge ", "%s not found", ODI_CONFIG_XML); goto leave; } // read network file logger_remotem("Start merge XML Config File: %s", inputXmlPath); xmlDocPtr inputXmlDocumentPointer = xmlParseFile(inputXmlPath); if (inputXmlDocumentPointer == 0) { logger_remotem("LOAD_CONFIG_FILE: input XML not well formed %s", inputXmlPath); mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\nInputXML file is not well-formed.\r\n"); return; } xmlDocPtr configXmlDocumentPointer = xmlParseFile(ODI_CONFIG_XML); if (configXmlDocumentPointer == 0) { logger_remotem("LOAD_CONFIG_FILE: config XML not well formed %s", ODI_CONFIG_XML); mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\nConfiguration XML file is not well-formed.\r\n"); return; } // doc check xmlNodePtr cur = xmlDocGetRootElement(inputXmlDocumentPointer); if (cur == NULL) { logger_remotem("LOAD_CONFIG_FILE: input XML is empty"); xmlFreeDoc(configXmlDocumentPointer); xmlFreeDoc(inputXmlDocumentPointer); goto leave; } // config-meta check if (xmlStrcmp(cur->name, (const xmlChar*)"config-metadata")) { logger_remotem("LOAD_CONFIG_FILE: config-metadata tag not found"); xmlFreeDoc(configXmlDocumentPointer); xmlFreeDoc(inputXmlDocumentPointer); goto leave; } xmlNodePtr destParent = xmlDocGetRootElement(configXmlDocumentPointer); //node not found if (destParent == NULL) { logger_remotem("LOAD_CONFIG_FILE: empty doc"); xmlFreeDoc(configXmlDocumentPointer); xmlFreeDoc(inputXmlDocumentPointer); goto leave; } if (xmlStrcmp(destParent->name, (const xmlChar*)"config-metadata")) { logger_remotem("LOAD_CONFIG_FILE: root node != config-metadata"); xmlFreeDoc(configXmlDocumentPointer); xmlFreeDoc(inputXmlDocumentPointer); goto leave; } destParent = destParent->xmlChildrenNode; while (destParent) { if ((!xmlStrcmp(destParent->name, (const xmlChar*)"config"))) { found = 1; //Parse child content for field that need reboot xmlNodePtr info = destParent->xmlChildrenNode; while (info != NULL) { if (!xmlStrcmp(info->name, (const xmlChar*)"remotem_broadcast_ip")) { // logger_info("Tag 1: %s", info->name); // logger_info("Content: %s", (char *)xmlNodeGetContent(info)); broadcast_xml_field = (char*)xmlNodeGetContent(info); } else if (!xmlStrcmp(info->name, (const xmlChar *) "rec_pre")) { // logger_info("Tag 2: %s", info->name); // logger_info("Content: %s", (char *)xmlNodeGetContent(info)); rec_pre_xml_field = (char*)xmlNodeGetContent(info); } info = info->next; } break; } destParent = destParent->next; } if (!found) { logger_remotem("LOAD_CONFIG_FILE: config tag not found in %s", configXmlPath); xmlFreeDoc(configXmlDocumentPointer); xmlFreeDoc(inputXmlDocumentPointer); goto leave; } cur = cur->xmlChildrenNode; xmlNodePtr child = NULL; char *new_date = 0, *new_time = 0, *new_tz = 0; char str_comm[200]; unsigned int config_change_reboot = 0; while (cur != NULL) { if ((!xmlStrcmp(cur->name, (const xmlChar*)"config"))) { child = cur->xmlChildrenNode; while (child != NULL) { if (!(xmlStrcmp(child->name, (const xmlChar*)"text"))) { ; } else if (!(xmlStrcmp(child->name, (const xmlChar*)"dvr_id"))) { // cannot change DVR ID ; } else if (!(xmlStrcmp(child->name, (const xmlChar*)"dts_date"))) { new_date = (char*)xmlNodeGetContent(child); if (strlen(new_date) == 10) // Check for valid date { logger_remotem("LOAD_CONFIG_FILE: setting date %s", new_date); modifytree(&destParent, child); } } else if (!(xmlStrcmp(child->name, (const xmlChar*)"dts_time"))) { new_time = (char*)xmlNodeGetContent(child); if (strlen(new_time) == 8) // Check for valid time { logger_remotem("LOAD_CONFIG_FILE: setting time %s", new_time); modifytree(&destParent, child); } } else if (!(xmlStrcmp(child->name, (const xmlChar*)"dvr_name"))) { char* str = (char*)xmlNodeGetContent(child); if (strlen(str) > 1) { logger_remotem("LOAD_CONFIG_FILE: setting dvr_name %s", str); modifytree(&destParent, child); set_dvr_name(str); } } else if (!(xmlStrcmp(child->name, (const xmlChar*)"ops_carnum"))) { char* str = (char*)xmlNodeGetContent(child); if (strlen(str) > 1) // Check for valid time { logger_remotem("LOAD_CONFIG_FILE: setting ops_carnum %s", str); modifytree(&destParent, child); set_dvr_name(str); } } else if (!(xmlStrcmp(child->name, (const xmlChar*)"usb_login"))) { char* str = (char*)xmlNodeGetContent(child); if (strlen(str) > 1) // Check for valid time { logger_remotem("LOAD_CONFIG_FILE: setting usb_login %s", str); modifytree(&destParent, child); set_assignable(str); } } else if (!(xmlStrcmp(child->name, (const xmlChar*)"dts_tz"))) { new_tz = (char*)xmlNodeGetContent(child); modifytree(&destParent, child); char ptr[strlen(new_tz) + 1]; int i, j = 0; for (i = 0; new_tz[i] != ' '; i++) { // skip till space } i++; // move up from the space for (i; new_tz[i] != '\0'; i++) { ptr[j++] = new_tz[i]; } ptr[j] = '\0'; memset(str_comm, 0, 200); sprintf(str_comm, "export TZ=%s", ptr); system(str_comm); } else if (!(xmlStrcmp(child->name, (const xmlChar*)"eth_dhcp"))) { isNetUpdate = 1; if (!(xmlStrcmp((const xmlChar*)xmlNodeGetContent(child), (const xmlChar*)"true"))) { dhcp = 1; logger_remotem("LOAD_CONFIG_FILE: change to DHCP"); } else { dhcp = 0; logger_remotem("LOAD_CONFIG_FILE: DHCP is static"); } modifytree(&destParent, child); } else if (!(xmlStrcmp(child->name, (const xmlChar*)"eth_addr"))) { address = (char *)xmlNodeGetContent(child); if (address != NULL && strlen(address) > 7) { isNetUpdate = 1; modifytree(&destParent, child); } } else if (!(xmlStrcmp(child->name, (const xmlChar*)"eth_mask"))) { netmask = (char*)xmlNodeGetContent(child); if (netmask != NULL && strlen(netmask) > 7) { isNetUpdate = 1; modifytree(&destParent, child); } } else if (!(xmlStrcmp(child->name, (const xmlChar*)"eth_gate"))) { gateway = (char*)xmlNodeGetContent(child); if (gateway != NULL && strlen(gateway) > 7) { isNetUpdate = 1; modifytree(&destParent, child); } // Set flag for config update item need to reboot to apply change } else if (!(xmlStrcmp(child->name, (const xmlChar*)"remotem_broadcast_ip"))) { logger_info("New: %s Old: %s", xmlNodeGetContent(child), broadcast_xml_field); if (strcmp((char*)xmlNodeGetContent(child), broadcast_xml_field)) { config_change_reboot = 1; } modifytree(&destParent, child); } else if (!(xmlStrcmp(child->name, (const xmlChar*)"rec_pre"))) { if (rec_pre_xml_field == NULL) { config_change_reboot = 1; } else if (strcmp((char*)xmlNodeGetContent(child), rec_pre_xml_field)) { if (strncmp(get_hw_version(), "3", 1)) // Only update when >= v4.0 { config_change_reboot = 1; } } modifytree(&destParent, child); } else { if (modifytree(&destParent, child) == -1) { logger_remotem("LOAD_CONFIG_FILE: %s doesn't contain %s tag", configXmlPath, (char*)child->name); } } child = child->next; } } cur = cur->next; } // end while loop until NULL all elements processed if (new_date == NULL && new_time == NULL) { logger_remotem("%s: date and time are null", __FUNCTION__); } else { set_sys_clock(new_date, new_time); } mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\nerrno=0\r\n"); if (isNetUpdate) { update_network(dhcp, address, netmask, gateway); } logger_remotem("LOAD_CONFIG_FILE: saving new config, size=%d", xmlSaveFileEnc(configXmlPath, configXmlDocumentPointer, "UTF-8")); leave: if (inputXmlDocumentPointer) { xmlFreeDoc(inputXmlDocumentPointer); } if (configXmlDocumentPointer) { xmlFreeDoc(configXmlDocumentPointer); } if (config_change_reboot == 1) { logger_info("Config item change need system reboot"); // write_command_to_serial_port("RST\r\n"); // sleep(1); system("reboot"); } }
clib_package_t * clib_package_new_from_slug(const char *slug, int verbose) { char *author = NULL; char *name = NULL; char *version = NULL; char *url = NULL; char *json_url = NULL; char *repo = NULL; http_get_response_t *res = NULL; clib_package_t *pkg = NULL; // parse chunks if (!slug) goto error; if (!(author = parse_repo_owner(slug, DEFAULT_REPO_OWNER))) goto error; if (!(name = parse_repo_name(slug))) goto error; if (!(version = parse_repo_version(slug, DEFAULT_REPO_VERSION))) goto error; if (!(url = clib_package_url(author, name, version))) goto error; if (!(json_url = clib_package_file_url(url, "package.json"))) goto error; if (!(repo = clib_package_repo(author, name))) goto error; // fetch json if (verbose) logger_info("fetch", json_url); res = http_get(json_url); if (!res || !res->ok) { logger_error("error", "unable to fetch %s", json_url); goto error; } free(json_url); json_url = NULL; free(name); name = NULL; // build package pkg = clib_package_new(res->data, verbose); http_get_free(res); res = NULL; if (!pkg) goto error; // force version number if (pkg->version) { if (0 != strcmp(version, pkg->version)) { free(pkg->version); pkg->version = version; } else { free(version); } } else { pkg->version = version; } // force package author (don't know how this could fail) if (pkg->author) { if (0 != strcmp(author, pkg->author)) { free(pkg->author); pkg->author = author; } else { free(author); } } else { pkg->author = author; } // force package repo if (pkg->repo) { if (0 != strcmp(repo, pkg->repo)) { free(pkg->repo); pkg->repo = repo; } else { free(repo); } } else { pkg->repo = repo; } pkg->url = url; return pkg; error: if (author) free(author); if (name) free(name); if (version) free(version); if (url) free(url); if (json_url) free(json_url); if (repo) free(repo); if (res) http_get_free(res); if (pkg) clib_package_free(pkg); return NULL; }
void logear_path(const char* funcion, const char* path) { logger_info(logger, "\tFuncion:'%s'", funcion); //logger_info(logger, ""); logger_info(logger, "\tpath:'%s'", path); logger_info(logger, "-----------------"); }
void log_request(Request *req) { logger_info(req->http_verb); logger_info(req->path); logger_info(get_time()); }
void ebmq_info (char *message) { logger_info(message); }
int clib_package_install(clib_package_t *pkg, const char *dir, int verbose) { char *pkg_dir = NULL; char *package_json = NULL; list_iterator_t *iterator = NULL; int rc = -1; if (!pkg || !dir) goto cleanup; if (!(pkg_dir = path_join(dir, pkg->name))) goto cleanup; // create directory for pkg if (-1 == mkdirp(pkg_dir, 0777)) goto cleanup; if (NULL == pkg->url) { pkg->url = clib_package_url(pkg->author , pkg->repo_name , pkg->version); if (NULL == pkg->url) goto cleanup; } // if no sources are listed, just install if (NULL == pkg->src) goto install; // write package.json if (!(package_json = path_join(pkg_dir, "package.json"))) goto cleanup; if (-1 == fs_write(package_json, pkg->json)) { logger_error("error", "Failed to write %s", package_json); goto cleanup; } iterator = list_iterator_new(pkg->src, LIST_HEAD); list_node_t *source; while ((source = list_iterator_next(iterator))) { char *filename = NULL; char *file_url = NULL; char *file_path = NULL; int error = 0; filename = source->val; // get file URL to fetch if (!(file_url = clib_package_file_url(pkg->url, filename))) { error = 1; goto loop_cleanup; } // get file path to save if (!(file_path = path_join(pkg_dir, basename(filename)))) { error = 1; goto loop_cleanup; } // TODO the whole URL is overkill and floods my terminal if (verbose) logger_info("fetch", file_url); // fetch source file and save to disk if (-1 == http_get_file(file_url, file_path)) { logger_error("error", "unable to fetch %s", file_url); error = 1; goto loop_cleanup; } if (verbose) logger_info("save", file_path); loop_cleanup: if (file_url) free(file_url); if (file_path) free(file_path); if (error) { list_iterator_destroy(iterator); rc = -1; goto cleanup; } } install: rc = clib_package_install_dependencies(pkg, dir, verbose); cleanup: if (pkg_dir) free(pkg_dir); if (package_json) free(package_json); if (iterator) list_iterator_destroy(iterator); return rc; }
t_personaje* personaje_crear(char* config_path) { //creamos una instancia de personaje alloc(self, t_personaje); //creamos una instancia del lector de archivos de config t_config* config = config_create(config_path); t_config* global_config = config_create("global.cfg"); //datos basicos del personaje self->nombre = string_duplicate(config_get_string_value(config, "Nombre")); self->simbolo = *config_get_string_value(config, "Simbolo"); int vidas = config_get_int_value(config, "Vidas"); self->vidas_iniciales = vidas; self->vidas = vidas; //misc if(config_has_property(global_config, "AutoContinue")) self->auto_continue = config_get_int_value(global_config, "AutoContinue"); else self->auto_continue = 0; //datos del logger char* log_file; if(config_has_property(config,"LogFile")) log_file = string_duplicate(config_get_string_value(config, "LogFile")); else log_file = string_from_format("%s.log", self->nombre); char* log_level; if(config_has_property(global_config, "LogLevel")) log_level = config_get_string_value(global_config, "LogLevel"); else if(config_has_property(config, "LogLevel")) log_level = config_get_string_value(config, "LogLevel"); else log_level = "INFO"; //inicializamos el logger logger_initialize(log_file, "personaje", log_level, 1); self->logger = logger_new_instance(); var(logger, self->logger); //datos de plataforma char* plataforma; if(config_has_property(global_config, "Plataforma")) plataforma = string_duplicate(config_get_string_value(global_config, "Plataforma")); else plataforma = string_duplicate(config_get_string_value(config, "Plataforma")); self->ippuerto_orquestador = plataforma; //datos de niveles y objetivos char** nombres_niveles = config_get_array_value(config, "Niveles"); t_list* niveles = list_create(); int i = 0; char* nombre_nivel = nombres_niveles[i]; while(nombre_nivel != null) { alloc(nivel, t_nivel); nivel->nombre = string_duplicate(nombre_nivel); nivel->objetivos = list_create(); char* key_objetivos = string_from_format("Obj[%s]", nombre_nivel); char* objetivos = config_get_string_value(config, key_objetivos); logger_info(logger, "Nivel: %s", nivel->nombre); logger_info(logger, "Objetivos: %s", objetivos); int ii = 0; while(objetivos != null && objetivos[ii] != '\0') { char* objetivo = string_from_format("%c" , objetivos[ii]); list_add(nivel->objetivos, objetivo); ii++; } free(key_objetivos); list_add(niveles, nivel); i++; nombre_nivel = nombres_niveles[i]; } self->niveles = niveles; logger_info(logger, "Log File:%s", log_file); free(log_file); logger_info(logger, "Log Level:%s", log_level); //liberamos recursos config_destroy(config); config_destroy(global_config); liberar_niveles(nombres_niveles); return self; }
int main(int argc, char** argv) { int response; igraph_t graph; igraph_vector_ptr_t complist; iclust_collection * collection = NULL; time_t time_start, time_end; /* turn on attribute handling */ igraph_i_set_attribute_table(&igraph_cattribute_table); double minimal_weight; unsigned int maximal_steps_delimieter; char graphncol[1024], logconfig[1024]; Config *cfg = ConfigNew(); const char * configuration = getopt_configfile(argc, argv, "./graphtocluster.conf"); massert((ConfigReadFile(configuration, &cfg) == CONFIG_OK), "Configuration file is not readable"); ConfigReadString(cfg, "sources", "graphncol", graphncol, sizeof(graphncol), 0); ConfigReadString(cfg, "sources", "logconfig", logconfig, sizeof(logconfig), 0); ConfigReadDouble(cfg, "limits", "minimal_weight", &minimal_weight, 0); ConfigReadUnsignedInt(cfg, "limits", "maximal_steps_delimieter", &maximal_steps_delimieter, 1); massert((maximal_steps_delimieter > 0), "Delimiter can not be equal to zero"); ConfigFree(cfg); logger_init(logconfig, "graphtocluster"); logger_info("File:\t configuration %s", configuration); logger_info("File:\t configuration logger %s", logconfig); logger_info("File:\t ncol graph source %s", graphncol); logger_info("Min:\t edge weight %f", minimal_weight); logger_info("Max:\t step delimeter %u", maximal_steps_delimieter); FILE *graph_source = fopen(graphncol, "r"); response = igraph_read_graph_ncol(&graph, graph_source, NULL, true, IGRAPH_ADD_WEIGHTS_YES, 0); massert((response == IGRAPH_SUCCESS), "Can not read a graph"); logger_info("Count:\t edges at start: %d", igraph_ecount(&graph)); fclose(graph_source); time(&time_start); igraph_edges_remove_by(&graph, "weight", minimal_weight, double_lt); time(&time_end); logger_info("Time:\t remove edges: %f", difftime(time_end, time_start)); logger_info("Count:\t edges after remove: %d", igraph_ecount(&graph)); response = igraph_vector_ptr_init(&complist, 0); massert((response == IGRAPH_SUCCESS), "Can not initialize vector pointer"); response = igraph_decompose(&graph, &complist, IGRAPH_WEAK, -1, 0); massert((response == IGRAPH_SUCCESS), "Can not decompose graph"); unsigned int n = igraph_vector_ptr_size(&complist); collection = iclust_collection_new(); massert((collection != NULL), "Cluster collection object can not be empty"); time(&time_start); for (unsigned int i = 0; i < n; i++) { igraph_t *subgraph = VECTOR(complist)[i]; massert((subgraph != NULL), "Subgraph object can not be empty"); iclust_collection_fill_leading_eigenvector(collection, subgraph, (i + 1), maximal_steps_delimieter); igraph_destroy(subgraph); } time(&time_end); logger_info("Time:\t cluster: %f", difftime(time_end, time_start)); /* Sort collection by cluster id to be * able to build a second column correct*/ time(&time_start); iclust_collection_sort(collection); time(&time_end); logger_info("Time:\t cluster sorting: %f", difftime(time_end, time_start)); unsigned long cluster_index = 1, cluster = 0; for (unsigned long i = 0; i < collection->length; i++) { iclust_collection_element * element = collection->collection[i]; massert((element != NULL), "Cluster collection element object can not be empty"); if (element->cluster != cluster) { cluster = element->cluster; cluster_index = 0; } printf("%lu\t%lu\t%s\n", cluster, ++cluster_index, element->name); } iclust_collection_destroy(collection); igraph_vector_ptr_destroy(&complist); igraph_destroy(&graph); logger_destroy(); return EXIT_SUCCESS; }