-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
rtpp_command_rcache.c
223 lines (199 loc) · 6.35 KB
/
rtpp_command_rcache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (c) 2015 Sippy Software, Inc., http://www.sippysoft.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#if defined(HAVE_CONFIG_H)
#include "config_pp.h"
#endif
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "rtpp_types.h"
#include "rtpp_command_rcache.h"
#include "rtpp_command_rcache_fin.h"
#include "rtpp_hash_table.h"
#include "rtpp_mallocs.h"
#include "rtpp_refcnt.h"
#include "rtpp_timed.h"
#include "rtpp_timed_task.h"
#include "rtpp_util.h"
#define RTPP_RCACHE_CPERD 3.0
struct rtpp_cmd_rcache_pvt {
struct rtpp_cmd_rcache pub;
double min_ttl;
struct rtpp_hash_table *ht;
struct rtpp_timed_task *timeout;
int timeout_rval;
};
struct rtpp_cmd_rcache_entry {
char *reply;
double etime;
struct {
struct rtpp_refcnt *rcnt;
} pub;
};
static enum rtpp_timed_cb_rvals rtpp_cmd_rcache_cleanup(double, void *);
static void rtpp_cmd_rcache_insert(struct rtpp_cmd_rcache *, const rtpp_str_t *,
const char *, double);
int rtpp_cmd_rcache_lookup(struct rtpp_cmd_rcache *, const rtpp_str_t *,
char *, int);
static void rtpp_cmd_rcache_dtor(struct rtpp_cmd_rcache_pvt *);
static void rtpp_cmd_rcache_shutdown(struct rtpp_cmd_rcache *);
struct rtpp_cmd_rcache *
rtpp_cmd_rcache_ctor(struct rtpp_timed *rtpp_timed_cf, double min_ttl)
{
struct rtpp_cmd_rcache_pvt *pvt;
pvt = rtpp_rzmalloc(sizeof(struct rtpp_cmd_rcache_pvt), PVT_RCOFFS(pvt));
if (pvt == NULL) {
return (NULL);
}
#if !defined(RTPP_DEBUG)
pvt->ht = rtpp_hash_table_ctor(rtpp_ht_key_str_t, RTPP_HT_NODUPS);
#else
pvt->ht = rtpp_hash_table_ctor(rtpp_ht_key_str_t, RTPP_HT_NODUPS |
RTPP_HT_DUP_ABRT);
#endif
if (pvt->ht == NULL) {
goto e0;
}
pvt->timeout = CALL_SMETHOD(rtpp_timed_cf, schedule_rc, RTPP_RCACHE_CPERD,
pvt->pub.rcnt, rtpp_cmd_rcache_cleanup, NULL, pvt);
if (pvt->timeout == NULL) {
goto e2;
}
pvt->min_ttl = min_ttl;
pvt->timeout_rval = CB_MORE;
pvt->pub.insert = &rtpp_cmd_rcache_insert;
pvt->pub.lookup = &rtpp_cmd_rcache_lookup;
pvt->pub.shutdown = &rtpp_cmd_rcache_shutdown;
CALL_SMETHOD(pvt->pub.rcnt, attach, (rtpp_refcnt_dtor_t)&rtpp_cmd_rcache_dtor,
pvt);
return (&pvt->pub);
e2:
RTPP_OBJ_DECREF(pvt->ht);
e0:
RTPP_OBJ_DECREF(&(pvt->pub));
free(pvt);
return (NULL);
}
static void
rtpp_cmd_rcache_entry_free(void *p)
{
struct rtpp_cmd_rcache_entry *rep;
rep = (struct rtpp_cmd_rcache_entry *)p;
free(rep->reply);
free(rep);
}
static void
rtpp_cmd_rcache_insert(struct rtpp_cmd_rcache *pub, const rtpp_str_t *cookie,
const char *reply, double ctime)
{
struct rtpp_cmd_rcache_pvt *pvt;
struct rtpp_cmd_rcache_entry *rep;
pvt = (struct rtpp_cmd_rcache_pvt *)pub;
rep = rtpp_rzmalloc(sizeof(struct rtpp_cmd_rcache_entry), PVT_RCOFFS(rep));
if (rep == NULL) {
return;
}
rep->reply = strdup(reply);
if (rep->reply == NULL) {
goto e1;
}
rep->etime = ctime + pvt->min_ttl;
CALL_SMETHOD(rep->pub.rcnt, attach, rtpp_cmd_rcache_entry_free, rep);
CALL_SMETHOD(pvt->ht, append_str_refcnt, cookie, rep->pub.rcnt, NULL);
/*
* append_refcnt() either takes ownership in which case it incs refcount
* or it drops the ball in which it does not, so we release rco and set
* it free.
*/
RTPP_OBJ_DECREF(&(rep->pub));
return;
e1:
RTPP_OBJ_DECREF(&(rep->pub));
free(rep);
}
int
rtpp_cmd_rcache_lookup(struct rtpp_cmd_rcache *pub, const rtpp_str_t *cookie,
char *rbuf, int rblen)
{
struct rtpp_cmd_rcache_pvt *pvt;
struct rtpp_cmd_rcache_entry *rep;
struct rtpp_refcnt *rco;
pvt = (struct rtpp_cmd_rcache_pvt *)pub;
rco = CALL_SMETHOD(pvt->ht, find_str, cookie);
if (rco == NULL) {
return (0);
}
/*
* "find" method returns object that has been incref'ed, so make sure
* to decref when we've done with it.
*/
rep = CALL_SMETHOD(rco, getdata);
strlcpy(rbuf, rep->reply, rblen);
RC_DECREF(rco);
return (1);
}
static void
rtpp_cmd_rcache_shutdown(struct rtpp_cmd_rcache *pub)
{
struct rtpp_cmd_rcache_pvt *pvt;
pvt = (struct rtpp_cmd_rcache_pvt *)pub;
pvt->timeout_rval = CB_LAST;
CALL_METHOD(pvt->timeout, cancel);
RTPP_OBJ_DECREF(pvt->timeout);
pvt->timeout = NULL;
}
void
rtpp_cmd_rcache_dtor(struct rtpp_cmd_rcache_pvt *pvt)
{
rtpp_cmd_rcache_fin(&pvt->pub);
RTPP_OBJ_DECREF(pvt->ht);
free(pvt);
}
static int
rtpp_cmd_rcache_ematch(void *dp, void *ap)
{
struct rtpp_cmd_rcache_entry *rep;
double *ctimep;
/*
* This method does not need us to bump ref, since we are in the
* context of the rtpp_hash_table, which hold its own ref.
*/
ctimep = (double *)ap;
rep = (struct rtpp_cmd_rcache_entry *)dp;
if (rep->etime < *ctimep) {
return (RTPP_HT_MATCH_DEL);
}
return (RTPP_HT_MATCH_CONT);
}
static enum rtpp_timed_cb_rvals
rtpp_cmd_rcache_cleanup(double ctime, void *p)
{
struct rtpp_cmd_rcache_pvt *pvt;
pvt = (struct rtpp_cmd_rcache_pvt *)p;
CALL_SMETHOD(pvt->ht, foreach, rtpp_cmd_rcache_ematch, &ctime, NULL);
return (pvt->timeout_rval);
}