/*! \brief Dumps the current Resources to the observer. \details The dmz::ResourcesObserver::update_resource() Mode parameter will be set to dmz::ResourcesDumped. */ void dmz::ResourcesObserver::dump_current_resources () { if (__state.rc) { if (__state.mask & ResourcesPathMask) { HashTableStringIterator it; StringContainer *ptr (0); while (__state.rc->pathTable.get_next (it, ptr)) { update_resources_path (it.get_hash_key (), ResourcesDumped); } } if (__state.mask & ResourcesResourceMask) { HashTableStringIterator it; Config *ptr (0); while (__state.rc->rcTable.get_next (it, ptr)) { update_resource (it.get_hash_key (), ResourcesDumped); } } } }
void add_resource(region * r, int level, int base, int divisor, const resource_type * rtype) { struct rawmaterial *rm = calloc(sizeof(struct rawmaterial), 1); rm->next = r->resources; r->resources = rm; rm->level = level; rm->startlevel = level; rm->base = base; rm->divisor = divisor; rm->flags = 0; rm->type = rmt_get(rtype); update_resource(rm, 1.0); rm->type->terraform(rm, r); }
static void use_default(rawmaterial * res, const region * r, int amount) { assert(res->amount > 0 && amount >= 0 && amount <= res->amount); res->amount -= amount; while (res->amount == 0) { double modifier = 1.0 + ((rng_int() % (SHIFT * 2 + 1)) - SHIFT) * ((rng_int() % (SHIFT * 2 + 1)) - SHIFT) / 10000.0; int i; for (i = 0; r->terrain->production[i].type; ++i) { if (res->type->rtype == r->terrain->production[i].type) break; } ++res->level; update_resource(res, modifier); } }
/*! \brief Activates dmz::ResourcesObserver::update_resource() callback. \param[in] Mode Specifies if all currently defined resources should be dumped to observer upon activation. \param[in] TheMask A mask specifying which callbacks to activate. \return Returns a mask of all callbacks that were activated. */ dmz::UInt32 dmz::ResourcesObserver::set_resources_observer_callback_mask ( const ResourcesActivateModeEnum Mode, const UInt32 TheMask) { if (__state.rc) { const Boolean Dump = (Mode == ResourcesDumpAll); const Handle ObsHandle = __state.ObsHandle; if (ResourcesPathMask & TheMask) { if ((ResourcesPathMask & __state.mask) == 0) { if (__state.rc->pathObsTable.store (ObsHandle, this)) { __state.mask |= ResourcesPathMask; if (Dump) { HashTableStringIterator it; StringContainer *ptr (0); while (__state.rc->pathTable.get_next (it, ptr)) { update_resources_path (it.get_hash_key (), ResourcesCreated); } } } } } else if (ResourcesPathMask & __state.mask) { __state.mask &= ~ResourcesPathMask; __state.rc->pathObsTable.remove (ObsHandle); } if (ResourcesResourceMask & TheMask) { if ((ResourcesResourceMask & __state.mask) == 0) { if (__state.rc->rcObsTable.store (ObsHandle, this)) { __state.mask |= ResourcesResourceMask; if (Dump) { HashTableStringIterator it; Config *ptr (0); while (__state.rc->rcTable.get_next (it, ptr)) { update_resource (it.get_hash_key (), ResourcesCreated); } } } } } else if (ResourcesResourceMask & __state.mask) { __state.mask &= ~ResourcesResourceMask; __state.rc->rcObsTable.remove (ObsHandle); } } return __state.mask; }
int process_resource_event(auparse_state_t *au) { uid_t uid; time_t time; const char *res_type, *uuid, *name; char field[64]; const char *reason; int success; /* Just skip this record if it failed to get some of the fields */ if (extract_virt_fields(au, &uuid, &uid, &time, &name, &success)) return 0; /* Get the resource type */ auparse_first_record(au); res_type = auparse_find_field(au, "resrc"); reason = auparse_find_field(au, "reason"); if (res_type == NULL) { if (debug) fprintf(stderr, "Invalid resrc field.\n"); return 0; } /* Resource records with these types have old and new values. New * values indicate resources assignments and are added to the event * list. Old values are used to update the end time of a resource * assignment. */ int rc = 0; if (strcmp("disk", res_type) == 0 || strcmp("vcpu", res_type) == 0 || strcmp("mem", res_type) == 0 || strcmp("rng", res_type) == 0 || strcmp("net", res_type) == 0) { const char *res = NULL; /* Resource removed */ snprintf(field, sizeof(field), "old-%s", res_type); if(auparse_find_field(au, field)) res = auparse_interpret_field(au); if (res == NULL && debug) { fprintf(stderr, "Failed to get %s field.\n", field); } else { rc += update_resource(au, uuid, uid, time, name, success, reason, res_type, res); } /* Resource added */ res = NULL; snprintf(field, sizeof(field), "new-%s", res_type); if (auparse_find_field(au, field)) res = auparse_interpret_field(au); if (res == NULL && debug) { fprintf(stderr, "Failed to get %s field.\n", field); } else { rc += add_resource(au, uuid, uid, time, name, success, reason, res_type, res); } } else if (strcmp("cgroup", res_type) == 0) { auparse_first_record(au); const char *cgroup = NULL; if (auparse_find_field(au, "cgroup")) cgroup = auparse_interpret_field(au); rc += add_resource(au, uuid, uid, time, name, success, reason, res_type, cgroup); } else if (debug) { fprintf(stderr, "Found an unknown resource: %s.\n", res_type); } return rc; }