forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASynchronizer.c
122 lines (107 loc) · 3.87 KB
/
ASynchronizer.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
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "interface/Iface.h"
#include "interface/ASynchronizer.h"
#include "memory/Allocator.h"
#include "util/Identity.h"
#include "util/events/Timeout.h"
#include "util/log/Log.h"
#include "util/Hex.h"
#define MAX_DRY_CYCLES 16
#define ArrayList_TYPE struct Message
#define ArrayList_NAME Messages
#include "util/ArrayList.h"
struct ASynchronizer_pvt
{
struct ASynchronizer pub;
struct Allocator* alloc;
struct EventBase* base;
struct Log* log;
struct Allocator* cycleAlloc;
struct ArrayList_Messages* msgsToA;
struct ArrayList_Messages* msgsToB;
struct Allocator* timeoutAlloc;
int dryCycles;
Identity
};
static void timeoutTrigger(void* vASynchronizer)
{
struct ASynchronizer_pvt* as = Identity_check((struct ASynchronizer_pvt*) vASynchronizer);
if (!as->cycleAlloc) {
if (as->dryCycles++ < MAX_DRY_CYCLES || !as->timeoutAlloc) { return; }
Allocator_free(as->timeoutAlloc);
as->timeoutAlloc = NULL;
as->dryCycles = 0;
return;
}
struct ArrayList_Messages* msgsToA = as->msgsToA;
struct ArrayList_Messages* msgsToB = as->msgsToB;
struct Allocator* cycleAlloc = as->cycleAlloc;
as->msgsToA = NULL;
as->msgsToB = NULL;
as->cycleAlloc = NULL;
if (msgsToA) {
for (int i = 0; i < msgsToA->length; i++) {
struct Message* msg = ArrayList_Messages_get(msgsToA, i);
Iface_send(&as->pub.ifA, msg);
}
}
if (msgsToB) {
for (int i = 0; i < msgsToB->length; i++) {
struct Message* msg = ArrayList_Messages_get(msgsToB, i);
Iface_send(&as->pub.ifB, msg);
}
}
Allocator_free(cycleAlloc);
}
static void checkTimeout(struct ASynchronizer_pvt* as)
{
if (as->timeoutAlloc) { return; }
as->timeoutAlloc = Allocator_child(as->alloc);
Timeout_setInterval(timeoutTrigger, as, 1, as->base, as->timeoutAlloc);
}
static Iface_DEFUN fromA(struct Message* msg, struct Iface* ifA)
{
struct ASynchronizer_pvt* as = Identity_containerOf(ifA, struct ASynchronizer_pvt, pub.ifA);
if (!as->cycleAlloc) { as->cycleAlloc = Allocator_child(as->alloc); }
if (!as->msgsToB) { as->msgsToB = ArrayList_Messages_new(as->cycleAlloc); }
Allocator_adopt(as->cycleAlloc, msg->alloc);
ArrayList_Messages_add(as->msgsToB, msg);
checkTimeout(as);
return NULL;
}
static Iface_DEFUN fromB(struct Message* msg, struct Iface* ifB)
{
struct ASynchronizer_pvt* as = Identity_containerOf(ifB, struct ASynchronizer_pvt, pub.ifB);
if (!as->cycleAlloc) { as->cycleAlloc = Allocator_child(as->alloc); }
if (!as->msgsToA) { as->msgsToA = ArrayList_Messages_new(as->cycleAlloc); }
Allocator_adopt(as->cycleAlloc, msg->alloc);
ArrayList_Messages_add(as->msgsToA, msg);
checkTimeout(as);
return NULL;
}
struct ASynchronizer* ASynchronizer_new(struct Allocator* alloc,
struct EventBase* base,
struct Log* log)
{
struct ASynchronizer_pvt* ctx = Allocator_calloc(alloc, sizeof(struct ASynchronizer_pvt), 1);
Identity_set(ctx);
ctx->alloc = alloc;
ctx->base = base;
ctx->log = log;
ctx->pub.ifA.send = fromA;
ctx->pub.ifB.send = fromB;
return &ctx->pub;
}