Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
Latest commit
259 lines (234 loc) · 6.12 KB
/
Copy pathinit.lua
File metadata and controls
259 lines (234 loc) · 6.12 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
K=
(function()
locallocal_k= {}
localcallbacks= {}
localk_client_id=bitstring.hexstream(Crypto.Digest("sha256", Crypto.GetRandomBytes(32)))
localstartup_time=Timer.Now()
localpeers= {}
localfunctionbest_peer()
-- Remove peers from peers table that are no longer connected
-- Peers is a table of last-seen timestamps, indexed by peer ID
forpeer_id, last_seeninpairs(peers) do
ifTimer.Now() -last_seen>15then
peers[peer_id] =nil
end
end
-- Otherwise, return the peer with the most recent timestamp
localbest_peer_id=k_client_id
forpeer_idinpairs(peers) do
ifpeer_id>best_peer_idthen
best_peer_id=peer_id
end
end
returnbest_peer_id
end
localfunctionpublish(diff, type)
Notifications.Publish("com.locimation.K", {k=diff, s=k_client_id, t=type})
end
_G._locimation_k_timer=Timer.New()
localsource_data=nil
localfunctionsource_clear(data, t)
ifnottthen
t=source_data
end
fork, vinpairs(data) do
iftype(v) =="table" andtype(t[k]) =="table" then
t[k] =source_clear(v, t[k])
else
t[k] =nil
end
end
end
_G._locimation_k_timer.EventHandler=function()
ifsource_datathen
publish(source_data, "refresh")
end
ifTimer.Now() -startup_time>15andbest_peer() ==k_client_idthen
Timer.CallAfter(
function()
publish(local_k, "broadcast")
end,
math.random()
)
end
end
_G._locimation_k_timer:Start(10)
localfunctionextract(t, k)
ifnotkork=="" then
returnt
end
localvalue=t
forpartink:gmatch("([^.]+)") do
value=value[part]
ifvalue==nilthen
returnnil
end
end
returnvalue
end
localfunctiondeepcopy(t)
iftype(t) ~="table" then
returnt
end
localnew_t= {}
fork, vinpairs(t) do
iftype(v) =="table" then
new_t[k] =deepcopy(v)
else
new_t[k] =v
end
end
returnnew_t
end
localfunctiondeepcompare(t1, t2)
iftype(t1) ~="table" ortype(t2) ~="table" then
returnt1==t2
end
fork, vinpairs(t1) do
ifnotdeepcompare(v, t2[k]) then
returnfalse
end
end
fork, vinpairs(t2) do
ifnotdeepcompare(v, t1[k]) then
returnfalse
end
end
returntrue
end
localfunctionpush_k(new_k)
iftype(new_k) ~="table" then
error("Invalid argument to push_k(): expected table")
end
localchanges= {}
forkeyinpairs(callbacks) do
localold_value=extract(local_k, key)
localnew_value=extract(new_k, key)
ifnotdeepcompare(old_value, new_value) then
changes[key] =deepcopy(new_value)
end
end
local_k=new_k
forkey, new_valueinpairs(changes) do
for_, fninipairs(callbacks[key]) do
fn(new_value)
end
end
end
localfunctionmerge(t1, t2)
iftype(t1) ~="table" ortype(t2) ~="table" then
returnt2
end
localnew_t=deepcopy(t1)
forkinpairs(t2) do
iftype(t1[k]) =="table" andtype(t2[k]) =="table" then
new_t[k] =merge(t1[k], t2[k])
else
new_t[k] =t2[k]
end
end
returnnew_t
end
localfunctioninit()
Notifications.Subscribe(
"com.locimation.K",
function(_, message)
Timer.CallAfter(
function()
ifmessage.s==k_client_idthen
return
end
peers[message.s] =Timer.Now()
ifmessage.t~="broadcast" andsource_datathen
source_clear(message.k)
end
localnew_k=merge(local_k, message.k)
ifmessage.t=="broadcast" andsource_datathen
new_k=merge(new_k, source_data)
end
push_k(new_k)
end,
0
)
end
)
end
localfunctionnow(diff)
source_data=merge(source_dataor {}, diff)
push_k(merge(local_k, diff))
publish(diff, "new")
end
localfunctionon(key, fn)
iftype(key) ~="string" ortype(fn) ~="function" then
error("Invalid arguments to on(): expected (string, function)")
end
ifnotcallbacks[key] then
callbacks[key] = {}
end
for_, existing_fninipairs(callbacks[key]) do-- prevent duplicate callbacks
ifexisting_fn==fnthen
return
end
end
table.insert(callbacks[key], fn)
end
localfunctionoff(key, fn)
ifnotkeythen
callbacks= {}
return
end
ifnotcallbacks[key] then
return
end
localcallbacks_filtered= {}
iffnthen
for_, finpairs(callbacks[key]) do
iff~=fnthen
table.insert(callbacks_filtered, f)
end
end
end
callbacks[key] =callbacks_filtered
end
localfunctionset(str_key, value)
localparts= {}
forpartinstr_key:gmatch("([^.]+)") do
table.insert(parts, 1, part)
end
for_, partinipairs(parts) do
value= {[part] =value}
end
now(value)
end
localfunctionlink(key, control, typ, options)
iftype(key) ~="string" ortype(control) ~="userdata" ortype(typ) ~="string" then
error("Invalid arguments to link(): expected (string, control, string)")
end
localfunctionset_control_value()
set(key, control[typ])
end
localoptions=optionsor {}
control.EventHandler=set_control_value
on(
key,
function(value)
control[typ] =value
end
)
ifnotoptions.no_initthen
Timer.CallAfter(set_control_value, math.random() +0.3)
end
end
localfunctionget(key)
returnextract(local_k, key)
end
return {
init=init,
now=now,
set=set,
on=on,
off=off,
link=link,
get=get
}
end)()