JAVA_MODULE_HOST_MANAGER_HANDLE JavaModuleHostManager_Create(JAVA_MODULE_HOST_CONFIG* config) { JAVA_MODULE_HOST_MANAGER_HANDLE_DATA* result; if (instance == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_002: [ The function shall allocate memory for the JAVA_MODULE_HOST_MANAGER_HANDLE if the global instance is NULL. ]*/ result = (JAVA_MODULE_HOST_MANAGER_HANDLE_DATA*)malloc(sizeof(JAVA_MODULE_HOST_MANAGER_HANDLE_DATA)); if (result == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_003: [ The function shall return NULL if memory allocation fails. ]*/ LogError("Failed to allocate memory for a JAVA_MODULE_HOST_MANAGER_HANDLE"); } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_004: [ The function shall initialize a new LOCK_HANDLE and set the JAVA_MODULE_HOST_MANAGER_HANDLE structures LOCK_HANDLE member. ]*/ if ((result->lock = Lock_Init()) == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_005: [ The function shall return NULL if lock initialization fails. ]*/ LogError("Lock_Init() failed."); free(result); result = NULL; } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_028: [The function shall do a deep copy of the config parameter and keep a pointer to the copied JAVA_MODULE_HOST_CONFIG.]*/ result->config = config_copy(config); if (result->config == NULL) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_029: [If the deep copy fails, the function shall return NULL.]*/ LogError("Failed to copy the JAVA_MODULE_HOST_CONFIG structure."); Lock_Deinit(result->lock); free(result); result = NULL; } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_006: [ The function shall set the module_count member variable to 0. ]*/ /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_007: [ The function shall shall set the global JAVA_MODULE_HOST_MANAGER_HANDLE instance. ]*/ result->module_count = 0; instance = result; } } } } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_030: [The function shall check against the singleton JAVA_MODULE_HOST_MANAGER_HANDLE instances JAVA_MODULE_HOST_CONFIG structure for equality in each field.]*/ if (config_options_compare(instance->config, config) != 0) { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_031: [The function shall return NULL if the JAVA_MODULE_HOST_CONFIG structures do not match.]*/ LogError("JAVA_MODULE_HOST_CONFIG does not match the JAVA_MODULE_HOST_CONFIG known by this manager."); result = NULL; } else { /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_001: [ The function shall return the global JAVA_MODULE_HOST_MANAGER_HANDLE instance if it is not NULL. ]*/ result = instance; } } /*Codes_SRS_JAVA_MODULE_HOST_MANAGER_14_008: [ The function shall return the global JAVA_MODULE_HOST_MANAGER_HANDLE instance. ]*/ return result; }
int config_load_servers(char *filename, master_server *master_srv) { /* load servers from config file */ config *conf = NULL; FILE *file = NULL; char line[4048]; int len, i; if ((file = fopen(filename, "r")) == NULL) { perror ("ERROR opening config file"); return -1; } int is_key, is_value, next; char key[128]; char value[2048]; int key_len = 0; int value_len = 0; int out_of_scope = 1; int count = 0; char *scope = "none"; while (fgets(line, sizeof(line), file) != NULL) { if ((len = strlen(line)) > 1) { is_key = 1; is_value = 0; next = 0; key_len = 0; value_len = 0; for (i = 0; i < len; ++i) { switch (line[i]) { case '#': /* comment */ next = 1; break; case '=': if (is_key) { key[key_len] = '\0'; is_key = 0; is_value = 1; } break; case '\t': case '\r': case '\n': break; case '{': if (key_len == 6) { if (strcmp(key, "server") == 0) { out_of_scope = 0; next = 1; /* create a new server */ master_srv->servers[count] = malloc(sizeof(server)); server *srv = master_srv->servers[count]; /* copy global config */ srv->config = config_init(); config_copy (srv->config, master_srv->config); conf = srv->config; ++count; scope = "server"; } } else if (key_len == 5) { if (strcmp(key, "proxy") == 0) { out_of_scope = 0; next = 1; /* create a new proxy */ conf->proxies[conf->proxy_count] = proxy_init(); conf->proxy_count++; scope = "proxy"; } } break; case '}': if (!out_of_scope) { out_of_scope = 1; scope = "none"; } break; default: if (is_key) { if (line[i] != ' ') { key[key_len] = line[i]; ++key_len; } } else if (is_value) { if (line[i] != ' ' || value_len != 0) { value[value_len] = line[i]; ++value_len; } } break; } if (next) { break; } } if (!out_of_scope) { value[value_len] = '\0'; if (key_len > 0 && value_len > 0) { if (conf != NULL) { /* save the value */ config_save_value (key, value, conf, scope); if (strcmp(key, "default") == 0) { /* found default server */ if (atoi(value) == 1) { master_srv->server_default = count-1; } } } } } } } fclose (file); /* validate */ if (conf->cache_turn_off_limit == 0 || conf->cache_turn_off_limit > conf->cache_memory_size) { /* invalid cache turn-off limit */ conf->cache_turn_off_limit = conf->cache_memory_size * 0.90; } master_srv->server_count = count; return 0; }
// Max and Min functions will call themselves recursively double prunemin(config* c, int depth, double alpha, double beta) { if(depth > DEPTH) { //Its time to approx at this level return approx(c,0); } config next; double score = 0.0; int i,j,p = 0; if(c->stage == 0) { //Adding Stage p = 0; for(i = 1; i <= 12 ; i = i+1) { if(config_self(c, i) == 1) { //Try this weight at all positions for(j = -15; j <= 15; j++) { if(config_board(c, j) == 0) { //There is no weight at this position config_copy(c, &next); config_selfPlace(&next, i , j); if(!config_tip(&next)) { p = 1; //Some Progress score = prunemax(c, depth+1, alpha, beta); //Get the scores from all your min childrens and take the max one if(score < beta) { //Better score beta = score; } if(alpha >= beta) { // During the entire game for every node alpha should be less than beta. Remember this prunemax will be called prunemin // and beta is best possible min value move that it could make. so if this alpha will be greater than that i will never // make this move. So Cut the search space and just return beta return alpha; } else if(beta == -1.0) { //Found the best possible score; Return it to your opponent; Remember the goal is assume that your opponent will // make the best possible move and if this move makes me the winner I won't search further because my opponent will know // this and thereby never make this move return beta; } } } } } } } else if (c->stage == 1) { // Removing Stage p = 0; for(j = -15; j <=15; j++) { if(config_board(c,j) > 0) { //There exist a weight at this position config_copy(c, &next); config_remove(&next, j); if(!config_tip(&next)) { //Not tipping ; Compute a score for your min childs score = prunemax(&next, depth + 1, alpha, beta); if(score < beta) { beta = score; } if(alpha >= beta) { return alpha; } else if(beta == -1.0) { return beta; } } } } } else { //Game Ended beta = 0.0; } if(p == 0) { //I can't make a move so I loose beta = 1.0; } return beta; }
double prune(config* c, double alpha, double beta) { config next; int weight = INT_MAX, board = INT_MAX; double score; int i,j; if(c->stage == 0) { //Adding Stage for(i = 1; i <= 12; i++) { //Check if you have this weight if(config_self(c, i) == 1) { for(j = -15; j <= 15; j++) { //Try this board position if(config_board(c, j) == 0) { config_copy(c, &next); config_selfPlace(&next, i, j); if(!config_tip(&next)) { //This move didn't cause tipping; Extract the score from each of my min nodes; Choose the child with max score score = prunemin(&next, 1, alpha, beta); if(score > alpha) { alpha = score; weight = i; board = j; } //If the score was 1.0. Bingo found one.This is called "Solve Optimization over minimax" if(alpha == 1.0) { printf("%d,%d\n",weight ,board); return alpha; } } } } } } if(weight == INT_MAX && board == INT_MAX) { //No optimal answer found; i.e every move caused a tipping . I will loose just choose a placement alpha = -1.0; for(i = 1; i <= 12; i++) { if(config_self(c, 1) == 1) { for(j = -15; j <= 15; j++) { //Try this board position if(config_board(c, j) == 0) { weight = i; board = j; break; } } break; } } } } else if (c->stage == 1) { //Removing Stage for(j = -15; j <=15; j++) { if(config_board(c,j) > 0) { //There exist a weight at this position config_copy(c, &next); config_remove(&next, j); if(!config_tip(&next)) { //Not tipping ; Compute a score for your min childs score = prunemin(&next, 1, alpha, beta); if(score > alpha) { alpha = score; weight = config_board(c, j); board = j; } if(alpha == 1.0) { printf("%d,%d\n", weight, board); return alpha; } } } } if(weight == INT_MAX && board == INT_MAX) { //No optimal answer found; i.e every move caused a tipping . I will loose just choose a placement alpha = -1.0; for(j = -15; j <= 15; j++) { //Try this board position if(config_board(c, j) > 0) { weight = config_board(c,j); board = j; break; } } } } else { return 0.0; } printf("%d,%d\n",weight,board); return alpha; }
static int screen_process_form(struct config_screen *screen) { const struct system_info *sysinfo = screen->cui->sysinfo; enum net_conf_type net_conf_type; struct interface_config *iface; bool allow_write, autoboot; char *str, *end; struct config *config; int i, n_boot_opts, rc, idx; unsigned int *order; char mac[20]; config = config_copy(screen, screen->cui->config); talloc_free(config->autoboot_opts); config->n_autoboot_opts = 0; n_boot_opts = widget_subset_get_order(config, &order, screen->widgets.boot_order_f); autoboot = widget_select_get_value(screen->widgets.autoboot_f); config->autoboot_enabled = autoboot && n_boot_opts; config->n_autoboot_opts = n_boot_opts; config->autoboot_opts = talloc_array(config, struct autoboot_option, n_boot_opts); for (i = 0; i < n_boot_opts; i++) { if (order[i] < sysinfo->n_blockdevs) { /* disk uuid */ config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID; config->autoboot_opts[i].uuid = talloc_strdup(config, sysinfo->blockdevs[order[i]]->uuid); } else if(order[i] < (sysinfo->n_blockdevs + sysinfo->n_interfaces)) { /* net uuid */ order[i] -= sysinfo->n_blockdevs; config->autoboot_opts[i].boot_type = BOOT_DEVICE_UUID; mac_str(sysinfo->interfaces[order[i]]->hwaddr, sysinfo->interfaces[order[i]]->hwaddr_size, mac, sizeof(mac)); config->autoboot_opts[i].uuid = talloc_strdup(config, mac); } else { /* device type */ order[i] -= (sysinfo->n_blockdevs + sysinfo->n_interfaces); config->autoboot_opts[i].boot_type = BOOT_DEVICE_TYPE; config->autoboot_opts[i].type = order[i]; } } str = widget_textbox_get_value(screen->widgets.timeout_f); if (str) { unsigned long x; errno = 0; x = strtoul(str, &end, 10); if (!errno && end != str) config->autoboot_timeout_sec = x; } if (screen->ipmi_override) if (widget_checkbox_get_value(screen->widgets.ipmi_clear_cb)) config->ipmi_bootdev = IPMI_BOOTDEV_INVALID; net_conf_type = widget_select_get_value(screen->widgets.network_f); /* if we don't have any network interfaces, prevent per-interface * configuration */ if (sysinfo->n_interfaces == 0) net_conf_type = NET_CONF_TYPE_DHCP_ALL; if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) { config->network.n_interfaces = 0; } else { iface = talloc_zero(config, struct interface_config); config->network.n_interfaces = 1; config->network.interfaces = talloc_array(config, struct interface_config *, 1); config->network.interfaces[0] = iface; /* copy hwaddr (from the sysinfo interface data) to * the configuration */ idx = widget_select_get_value(screen->widgets.iface_f); memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr, sizeof(iface->hwaddr)); } if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) { iface->method = CONFIG_METHOD_DHCP; } if (net_conf_type == NET_CONF_TYPE_STATIC) { char *ip, *mask, *gateway, *url; ip = widget_textbox_get_value(screen->widgets.ip_addr_f); mask = widget_textbox_get_value(screen->widgets.ip_mask_f); gateway = widget_textbox_get_value(screen->widgets.gateway_f); url = widget_textbox_get_value(screen->widgets.url_f); if (!ip || !*ip || !mask || !*mask) { screen->scr.frame.status = _("No IP / mask values are set"); nc_scr_frame_draw(&screen->scr); talloc_free(config); return -1; } iface->method = CONFIG_METHOD_STATIC; iface->static_config.address = talloc_asprintf(iface, "%s/%s", ip, mask); iface->static_config.gateway = talloc_strdup(iface, gateway); iface->static_config.url = talloc_strdup(iface, url); } str = widget_textbox_get_value(screen->widgets.dns_f); talloc_free(config->network.dns_servers); config->network.dns_servers = NULL; config->network.n_dns_servers = 0; if (str && strlen(str)) { char *dns, *tmp; int i; for (;;) { dns = strtok_r(str, " \t", &tmp); if (!dns) break; i = config->network.n_dns_servers++; config->network.dns_servers = talloc_realloc(config, config->network.dns_servers, const char *, config->network.n_dns_servers); config->network.dns_servers[i] = talloc_strdup(config, dns); str = NULL; } }