5 // gcc -O3 -Wall -Wextra -I ../src/ -I ../ -o registry ../src/registry.c ../src/dictionary.o ../src/log.o ../src/avl.o ../src/common.o -pthread -luuid -DHAVE_CONFIG_H
14 #include "dictionary.h"
25 typedef struct machine MACHINE;
37 typedef struct person PERSON;
51 typedef struct url URL;
60 // ----------------------------------------------------------------------------
63 MACHINE *registry_machine_load(const char *machine_guid) {
69 int registry_machine_save(MACHINE *m) {
75 MACHINE *registry_machine_find(const char *machine_guid) {
76 MACHINE *m = dictionary_get(registry.machines, machine_guid);
77 if(!m) m = registry_machine_load(machine_guid);
82 MACHINE *registry_machine_get(const char *machine_guid) {
83 MACHINE *m = registry_machine_find(machine_guid);
85 debug(D_REGISTRY, "Registry: creating new machine '%s'", machine_guid);
87 m = calloc(1, sizeof(MACHINE));
88 if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
90 strncpy(m->guid, machine_guid, 36);
92 dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
94 m->first_t = time(NULL);
102 // ----------------------------------------------------------------------------
105 PERSON *registry_person_load(const char *person_guid) {
111 int registry_person_save(PERSON *p) {
117 PERSON *registry_person_find(const char *person_guid) {
118 PERSON *p = dictionary_get(registry.persons, person_guid);
119 if(!p) p = registry_person_load(person_guid);
123 PERSON *registry_person_get(const char *person_guid) {
126 if(person_guid && *person_guid)
127 p = registry_person_find(person_guid);
130 if(person_guid && *person_guid)
131 error("Registry: discarding unknown person guid '%s'. Will give a new PERSONID.", person_guid);
133 debug(D_REGISTRY, "Registry: creating new person");
135 p = calloc(1, sizeof(PERSON));
136 if(!p) fatal("Registry: cannot allocate memory for new person.");
139 if(uuid_generate_time_safe(uuid) == -1)
140 info("Registry: uuid_generate_time_safe() reports UUID generation is not safe for uniqueness.");
142 uuid_unparse_lower(uuid, p->guid);
144 dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
146 p->first_t = time(NULL);
154 // ----------------------------------------------------------------------------
157 URL *registry_url_update(PERSON *p, MACHINE *m, const char *url) {
158 URL *pu = dictionary_get(p->urls, url);
159 URL *mu = dictionary_get(m->urls, url);
162 if(pu != mu || pu == NULL || mu == NULL)
163 error("Registry: person/machine discrepancy on url '%s', for person '%s' and machine '%s'", url, p->guid, m->guid);
168 size_t len = strlen(url);
170 URL *u = calloc(1, sizeof(URL) + len);
171 if(!u) fatal("Registry: cannot allocate memory for person '%s', machine '%s', url '%s'.", p->guid, m->guid, url);
177 u->first_t = time(NULL);
180 dictionary_set(p->urls, url, u, sizeof(URL) + u->url_length);
181 dictionary_set(m->urls, url, u, sizeof(URL) + u->url_length);
190 p->last_t = m->last_t = u->last_t = time(NULL);
196 // ----------------------------------------------------------------------------
199 int registry_save(void) {
203 char *registry_request(const char *person_guid, const char *machine_guid, const char *url) {
208 p = registry_person_get(person_guid);
209 person_guid = p->guid;
212 m = registry_machine_get(machine_guid);
213 machine_guid = m->guid;
216 registry_url_update(p, m, url);
218 registry_person_save(p);
219 registry_machine_save(m);
225 void registry_init(void) {
226 registry.persons = dictionary_create(DICTIONARY_FLAG_DEFAULT);
227 if(!registry.persons)
228 fatal("Registry: cannot create persons registry");
230 registry.machines = dictionary_create(DICTIONARY_FLAG_DEFAULT);
231 if(!registry.machines)
232 fatal("Registry: cannot create machines registry");
235 int main(int argc, char **argv) {