1 #include "registry_internals.h"
3 // ----------------------------------------------------------------------------
6 REGISTRY_MACHINE *registry_machine_find(const char *machine_guid) {
7 debug(D_REGISTRY, "Registry: registry_machine_find('%s')", machine_guid);
8 return dictionary_get(registry.machines, machine_guid);
11 REGISTRY_MACHINE_URL *registry_machine_url_allocate(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
12 debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): allocating %zu bytes", m->guid, u->url, sizeof(REGISTRY_MACHINE_URL));
14 REGISTRY_MACHINE_URL *mu = mallocz(sizeof(REGISTRY_MACHINE_URL));
16 mu->first_t = mu->last_t = (uint32_t)when;
19 mu->flags = REGISTRY_URL_FLAGS_DEFAULT;
21 registry.machines_urls_memory += sizeof(REGISTRY_MACHINE_URL);
23 debug(D_REGISTRY, "registry_machine_url_allocate('%s', '%s'): indexing URL in machine", m->guid, u->url);
24 dictionary_set(m->machine_urls, u->url, mu, sizeof(REGISTRY_MACHINE_URL));
31 REGISTRY_MACHINE *registry_machine_allocate(const char *machine_guid, time_t when) {
32 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating new machine, sizeof(MACHINE)=%zu", machine_guid, sizeof(REGISTRY_MACHINE));
34 REGISTRY_MACHINE *m = mallocz(sizeof(REGISTRY_MACHINE));
36 strncpyz(m->guid, machine_guid, GUID_LEN);
38 debug(D_REGISTRY, "Registry: registry_machine_allocate('%s'): creating dictionary of urls", machine_guid);
39 m->machine_urls = dictionary_create(DICTIONARY_FLAGS);
41 m->first_t = m->last_t = (uint32_t)when;
44 registry.machines_memory += sizeof(REGISTRY_MACHINE);
46 registry.machines_count++;
47 dictionary_set(registry.machines, m->guid, m, sizeof(REGISTRY_MACHINE));
52 // 1. validate machine GUID
53 // 2. if it is valid, find it or create it and return it
54 // 3. if it is not valid, return NULL
55 REGISTRY_MACHINE *registry_machine_get(const char *machine_guid, time_t when) {
56 REGISTRY_MACHINE *m = NULL;
58 if(likely(machine_guid && *machine_guid)) {
59 // validate it is a GUID
60 char buf[GUID_LEN + 1];
61 if(unlikely(regenerate_guid(machine_guid, buf) == -1))
62 info("Registry: machine guid '%s' is not a valid guid. Ignoring it.", machine_guid);
65 m = registry_machine_find(machine_guid);
66 if(!m) m = registry_machine_allocate(machine_guid, when);
74 // ----------------------------------------------------------------------------
77 REGISTRY_MACHINE_URL *registry_machine_link_to_url(REGISTRY_MACHINE *m, REGISTRY_URL *u, time_t when) {
78 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): searching for URL in machine", m->guid, u->url);
80 REGISTRY_MACHINE_URL *mu = dictionary_get(m->machine_urls, u->url);
82 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): not found", m->guid, u->url);
83 mu = registry_machine_url_allocate(m, u, when);
84 registry.machines_urls_count++;
87 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): found", m->guid, u->url);
89 if(likely(mu->last_t < (uint32_t)when)) mu->last_t = (uint32_t)when;
93 if(likely(m->last_t < (uint32_t)when)) m->last_t = (uint32_t)when;
95 if(mu->flags & REGISTRY_URL_FLAGS_EXPIRED) {
96 debug(D_REGISTRY, "registry_machine_link_to_url('%s', '%s'): accessing an expired URL.", m->guid, u->url);
97 mu->flags &= ~REGISTRY_URL_FLAGS_EXPIRED;