]> git.meshlink.io Git - catta/blob - src/rrlist.c
rename everything avahi to catta
[catta] / src / rrlist.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <assert.h>
26
27 #include <catta/llist.h>
28 #include <catta/malloc.h>
29
30 #include "rrlist.h"
31 #include <catta/log.h>
32
33 typedef struct CattaRecordListItem CattaRecordListItem;
34
35 struct CattaRecordListItem {
36     int read;
37     CattaRecord *record;
38     int unicast_response;
39     int flush_cache;
40     int auxiliary;
41     CATTA_LLIST_FIELDS(CattaRecordListItem, items);
42 };
43
44 struct CattaRecordList {
45     CATTA_LLIST_HEAD(CattaRecordListItem, read);
46     CATTA_LLIST_HEAD(CattaRecordListItem, unread);
47
48     int all_flush_cache;
49 };
50
51 CattaRecordList *catta_record_list_new(void) {
52     CattaRecordList *l;
53
54     if (!(l = catta_new(CattaRecordList, 1))) {
55         catta_log_error("catta_new() failed.");
56         return NULL;
57     }
58
59     CATTA_LLIST_HEAD_INIT(CattaRecordListItem, l->read);
60     CATTA_LLIST_HEAD_INIT(CattaRecordListItem, l->unread);
61
62     l->all_flush_cache = 1;
63     return l;
64 }
65
66 void catta_record_list_free(CattaRecordList *l) {
67     assert(l);
68
69     catta_record_list_flush(l);
70     catta_free(l);
71 }
72
73 static void item_free(CattaRecordList *l, CattaRecordListItem *i) {
74     assert(l);
75     assert(i);
76
77     if (i->read)
78         CATTA_LLIST_REMOVE(CattaRecordListItem, items, l->read, i);
79     else
80         CATTA_LLIST_REMOVE(CattaRecordListItem, items, l->unread, i);
81
82     catta_record_unref(i->record);
83     catta_free(i);
84 }
85
86 void catta_record_list_flush(CattaRecordList *l) {
87     assert(l);
88
89     while (l->read)
90         item_free(l, l->read);
91     while (l->unread)
92         item_free(l, l->unread);
93
94     l->all_flush_cache = 1;
95 }
96
97 CattaRecord* catta_record_list_next(CattaRecordList *l, int *ret_flush_cache, int *ret_unicast_response, int *ret_auxiliary) {
98     CattaRecord *r;
99     CattaRecordListItem *i;
100
101     if (!(i = l->unread))
102         return NULL;
103
104     assert(!i->read);
105
106     r = catta_record_ref(i->record);
107     if (ret_unicast_response)
108         *ret_unicast_response = i->unicast_response;
109     if (ret_flush_cache)
110         *ret_flush_cache = i->flush_cache;
111     if (ret_auxiliary)
112         *ret_auxiliary = i->auxiliary;
113
114     CATTA_LLIST_REMOVE(CattaRecordListItem, items, l->unread, i);
115     CATTA_LLIST_PREPEND(CattaRecordListItem, items, l->read, i);
116
117     i->read = 1;
118
119     return r;
120 }
121
122 static CattaRecordListItem *get(CattaRecordList *l, CattaRecord *r) {
123     CattaRecordListItem *i;
124
125     assert(l);
126     assert(r);
127
128     for (i = l->read; i; i = i->items_next)
129         if (catta_record_equal_no_ttl(i->record, r))
130             return i;
131
132     for (i = l->unread; i; i = i->items_next)
133         if (catta_record_equal_no_ttl(i->record, r))
134             return i;
135
136     return NULL;
137 }
138
139 void catta_record_list_push(CattaRecordList *l, CattaRecord *r, int flush_cache, int unicast_response, int auxiliary) {
140     CattaRecordListItem *i;
141
142     assert(l);
143     assert(r);
144
145     if (get(l, r))
146         return;
147
148     if (!(i = catta_new(CattaRecordListItem, 1))) {
149         catta_log_error("catta_new() failed.");
150         return;
151     }
152
153     i->unicast_response = unicast_response;
154     i->flush_cache = flush_cache;
155     i->auxiliary = auxiliary;
156     i->record = catta_record_ref(r);
157     i->read = 0;
158
159     l->all_flush_cache = l->all_flush_cache && flush_cache;
160
161     CATTA_LLIST_PREPEND(CattaRecordListItem, items, l->unread, i);
162 }
163
164 void catta_record_list_drop(CattaRecordList *l, CattaRecord *r) {
165     CattaRecordListItem *i;
166
167     assert(l);
168     assert(r);
169
170     if (!(i = get(l, r)))
171         return;
172
173     item_free(l, i);
174 }
175
176 int catta_record_list_is_empty(CattaRecordList *l) {
177     assert(l);
178
179     return !l->unread && !l->read;
180 }
181
182 int catta_record_list_all_flush_cache(CattaRecordList *l) {
183     assert(l);
184
185     /* Return TRUE if all entries in this list have flush_cache set */
186
187     return l->all_flush_cache;
188 }