]> arthur.barton.de Git - netdata.git/blob - src/registry.c
Merge remote-tracking branch 'upstream/master' into registry
[netdata.git] / src / registry.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
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
6
7 #include <uuid/uuid.h>
8 #include <inttypes.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "log.h"
13 #include "common.h"
14 #include "dictionary.h"
15
16 struct machine {
17         char guid[36 + 1];
18
19         time_t first_t;
20         time_t last_t;
21         size_t usages;
22         
23         DICTIONARY *urls;
24 };
25 typedef struct machine MACHINE;
26
27
28 struct person {
29         char guid[36 + 1];
30
31         time_t first_t;
32         time_t last_t;
33         size_t usages;
34
35         DICTIONARY *urls;
36 };
37 typedef struct person PERSON;
38
39
40 struct url {
41         MACHINE *machine;
42         PERSON *person;
43
44         time_t first_t;
45         time_t last_t;
46         size_t usages;
47
48         size_t url_length;
49         char url[];
50 };
51 typedef struct url URL;
52
53
54 struct registry {
55         DICTIONARY *persons;
56         DICTIONARY *machines;
57 } registry;
58
59
60 // ----------------------------------------------------------------------------
61 // MACHINE
62
63 MACHINE *registry_machine_load(const char *machine_guid) {
64         (void)machine_guid;
65
66         return NULL;
67 }
68
69 int registry_machine_save(MACHINE *m) {
70         (void)m;
71
72         return -1;
73 }
74
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);
78         return m;
79 }
80
81
82 MACHINE *registry_machine_get(const char *machine_guid) {
83         MACHINE *m = registry_machine_find(machine_guid);
84         if(!m) {
85                 debug(D_REGISTRY, "Registry: creating new machine '%s'", machine_guid);
86
87                 m = calloc(1, sizeof(MACHINE));
88                 if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
89
90                 strncpy(m->guid, machine_guid, 36);
91
92                 dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
93                 
94                 m->first_t = time(NULL);
95                 m->usages = 0;
96         }
97
98         return m;
99 }
100
101
102 // ----------------------------------------------------------------------------
103 // PERSON
104
105 PERSON *registry_person_load(const char *person_guid) {
106         (void)person_guid;
107
108         return NULL;
109 }
110
111 int registry_person_save(PERSON *p) {
112         (void)p;
113
114         return -1;
115 }
116
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);
120         return p;
121 }
122
123 PERSON *registry_person_get(const char *person_guid) {
124         PERSON *p = NULL;
125
126         if(person_guid && *person_guid)
127                 p = registry_person_find(person_guid);
128
129         if(!p) {
130                 if(person_guid && *person_guid)
131                         error("Registry: discarding unknown person guid '%s'. Will give a new PERSONID.", person_guid);
132
133                 debug(D_REGISTRY, "Registry: creating new person");
134
135                 p = calloc(1, sizeof(PERSON));
136                 if(!p) fatal("Registry: cannot allocate memory for new person.");
137
138                 uuid_t uuid;
139                 if(uuid_generate_time_safe(uuid) == -1)
140                         info("Registry: uuid_generate_time_safe() reports UUID generation is not safe for uniqueness.");
141
142                 uuid_unparse_lower(uuid, p->guid);
143
144                 dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
145
146                 p->first_t = time(NULL);
147                 p->usages = 0;
148         }
149
150         return p;
151 }
152
153
154 // ----------------------------------------------------------------------------
155 // URL
156
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);
160         URL *u = NULL;
161
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);
164         else
165                 u = pu;
166
167         if(!u) {
168                 size_t len = strlen(url);
169
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);
172
173                 strcpy(u->url, url);
174                 u->url_length = len;
175                 u->person = p;
176                 u->machine = m;
177                 u->first_t = time(NULL);
178                 u->usages = 1;
179
180                 dictionary_set(p->urls, url, u, sizeof(URL) + u->url_length);
181                 dictionary_set(m->urls, url, u, sizeof(URL) + u->url_length);
182                 if(pu) free(pu);
183                 if(mu) free(mu);
184         }
185         else
186                 u->usages++;
187
188         p->usages++;
189         m->usages++;
190         p->last_t = m->last_t = u->last_t = time(NULL);
191
192         return u;
193 }
194
195
196 // ----------------------------------------------------------------------------
197 // REGISTRY
198
199 int registry_save(void) {
200         return -1;
201 }
202
203 char *registry_request(const char *person_guid, const char *machine_guid, const char *url) {
204         PERSON *p = NULL;
205         MACHINE *m = NULL;
206
207         // --- PERSON ---
208         p = registry_person_get(person_guid);
209         person_guid = p->guid;
210
211         // --- MACHINE ---
212         m = registry_machine_get(machine_guid);
213         machine_guid = m->guid;
214
215         // --- URL ---
216         registry_url_update(p, m, url);
217
218         registry_person_save(p);
219         registry_machine_save(m);
220         registry_save();
221
222         return NULL;
223 }
224
225 void registry_init(void) {
226         registry.persons = dictionary_create(DICTIONARY_FLAG_DEFAULT);
227         if(!registry.persons)
228                 fatal("Registry: cannot create persons registry");
229
230         registry.machines = dictionary_create(DICTIONARY_FLAG_DEFAULT);
231         if(!registry.machines)
232                 fatal("Registry: cannot create machines registry");
233 }
234
235 int main(int argc, char **argv) {
236         (void)argc;
237         (void)argv;
238
239         registry_init();
240
241         return 0;
242 }