-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage.html
More file actions
432 lines (358 loc) · 14.1 KB
/
Copy pathcoverage.html
File metadata and controls
432 lines (358 loc) · 14.1 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>kademlia: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/arek-e/D7024E-Group-22/app/cmd/kademlia/bucket.go (84.2%)</option>
<option value="file1">github.com/arek-e/D7024E-Group-22/app/cmd/kademlia/contact.go (100.0%)</option>
<option value="file2">github.com/arek-e/D7024E-Group-22/app/cmd/kademlia/kademlia.go (0.0%)</option>
<option value="file3">github.com/arek-e/D7024E-Group-22/app/cmd/kademlia/kademliaid.go (72.7%)</option>
<option value="file4">github.com/arek-e/D7024E-Group-22/app/cmd/kademlia/network.go (0.0%)</option>
<option value="file5">github.com/arek-e/D7024E-Group-22/app/cmd/kademlia/routingtable.go (93.1%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package kademlia
import (
"container/list"
)
// bucket definition
// contains a List
type bucket struct {
list *list.List
}
// newBucket returns a new instance of a bucket
func newBucket() *bucket <span class="cov8" title="1">{
bucket := &bucket{}
bucket.list = list.New()
return bucket
}</span>
// AddContact adds the Contact to the front of the bucket
// or moves it to the front of the bucket if it already existed
func (bucket *bucket) AddContact(contact Contact) <span class="cov8" title="1">{
var element *list.Element
for e := bucket.list.Front(); e != nil; e = e.Next() </span><span class="cov8" title="1">{
nodeID := e.Value.(Contact).ID
if (contact).ID.Equals(nodeID) </span><span class="cov0" title="0">{
element = e
}</span>
}
<span class="cov8" title="1">if element == nil </span><span class="cov8" title="1">{
if bucket.list.Len() < bucketSize </span><span class="cov8" title="1">{
bucket.list.PushFront(contact)
}</span>
} else<span class="cov0" title="0"> {
bucket.list.MoveToFront(element)
}</span>
}
// GetContactAndCalcDistance returns an array of Contacts where
// the distance has already been calculated
func (bucket *bucket) GetContactAndCalcDistance(target *KademliaID) []Contact <span class="cov8" title="1">{
var contacts []Contact
for elt := bucket.list.Front(); elt != nil; elt = elt.Next() </span><span class="cov8" title="1">{
contact := elt.Value.(Contact)
contact.CalcDistance(target)
contacts = append(contacts, contact)
}</span>
<span class="cov8" title="1">return contacts</span>
}
// Len return the size of the bucket
func (bucket *bucket) Len() int <span class="cov0" title="0">{
return bucket.list.Len()
}</span>
</pre>
<pre class="file" id="file1" style="display: none">package kademlia
import (
"fmt"
"sort"
)
// Contact definition
// stores the KademliaID, the ip address and the distance
type Contact struct {
ID *KademliaID
Address string
distance *KademliaID
}
// NewContact returns a new instance of a Contact
func NewContact(id *KademliaID, address string) Contact <span class="cov8" title="1">{
return Contact{id, address, nil}
}</span>
// CalcDistance calculates the distance to the target and
// fills the contacts distance field
func (contact *Contact) CalcDistance(target *KademliaID) <span class="cov8" title="1">{
contact.distance = contact.ID.CalcDistance(target)
}</span>
// Less returns true if contact.distance < otherContact.distance
func (contact *Contact) Less(otherContact *Contact) bool <span class="cov8" title="1">{
return contact.distance.Less(otherContact.distance)
}</span>
// String returns a simple string representation of a Contact
func (contact *Contact) String() string <span class="cov8" title="1">{
return fmt.Sprintf(`contact("%s", "%s")`, contact.ID, contact.Address)
}</span>
// ContactCandidates definition
// stores an array of Contacts
type ContactCandidates struct {
contacts []Contact
}
// Append an array of Contacts to the ContactCandidates
func (candidates *ContactCandidates) Append(contacts []Contact) <span class="cov8" title="1">{
candidates.contacts = append(candidates.contacts, contacts...)
}</span>
// GetContacts returns the first count number of Contacts
func (candidates *ContactCandidates) GetContacts(count int) []Contact <span class="cov8" title="1">{
return candidates.contacts[:count]
}</span>
// Sort the Contacts in ContactCandidates
func (candidates *ContactCandidates) Sort() <span class="cov8" title="1">{
sort.Sort(candidates)
}</span>
// Len returns the length of the ContactCandidates
func (candidates *ContactCandidates) Len() int <span class="cov8" title="1">{
return len(candidates.contacts)
}</span>
// Swap the position of the Contacts at i and j
// WARNING does not check if either i or j is within range
func (candidates *ContactCandidates) Swap(i, j int) <span class="cov8" title="1">{
candidates.contacts[i], candidates.contacts[j] = candidates.contacts[j], candidates.contacts[i]
}</span>
// Less returns true if the Contact at index i is smaller than
// the Contact at index j
func (candidates *ContactCandidates) Less(i, j int) bool <span class="cov8" title="1">{
return candidates.contacts[i].Less(&candidates.contacts[j])
}</span>
</pre>
<pre class="file" id="file2" style="display: none">package kademlia
type Kademlia struct {
}
func (kademlia *Kademlia) LookupContact(target *Contact) {<span class="cov0" title="0">
// TODO
}</span>
func (kademlia *Kademlia) LookupData(hash string) {<span class="cov0" title="0">
// TODO
}</span>
func (kademlia *Kademlia) Store(data []byte) {<span class="cov0" title="0">
// TODO
}</span>
</pre>
<pre class="file" id="file3" style="display: none">package kademlia
import (
"encoding/hex"
"math/rand"
)
// the static number of bytes in a KademliaID
const IDLength = 20
// type definition of a KademliaID
type KademliaID [IDLength]byte
// NewKademliaID returns a new instance of a KademliaID based on the string input
func NewKademliaID(data string) *KademliaID <span class="cov8" title="1">{
decoded, _ := hex.DecodeString(data)
newKademliaID := KademliaID{}
for i := 0; i < IDLength; i++ </span><span class="cov8" title="1">{
newKademliaID[i] = decoded[i]
}</span>
<span class="cov8" title="1">return &newKademliaID</span>
}
// NewRandomKademliaID returns a new instance of a random KademliaID,
// change this to a better version if you like
func NewRandomKademliaID() *KademliaID <span class="cov0" title="0">{
newKademliaID := KademliaID{}
for i := 0; i < IDLength; i++ </span><span class="cov0" title="0">{
newKademliaID[i] = uint8(rand.Intn(256))
}</span>
<span class="cov0" title="0">return &newKademliaID</span>
}
// Less returns true if kademliaID < otherKademliaID (bitwise)
func (kademliaID KademliaID) Less(otherKademliaID *KademliaID) bool <span class="cov8" title="1">{
for i := 0; i < IDLength; i++ </span><span class="cov8" title="1">{
if kademliaID[i] != otherKademliaID[i] </span><span class="cov8" title="1">{
return kademliaID[i] < otherKademliaID[i]
}</span>
}
<span class="cov0" title="0">return false</span>
}
// Equals returns true if kademliaID == otherKademliaID (bitwise)
func (kademliaID KademliaID) Equals(otherKademliaID *KademliaID) bool <span class="cov8" title="1">{
for i := 0; i < IDLength; i++ </span><span class="cov8" title="1">{
if kademliaID[i] != otherKademliaID[i] </span><span class="cov8" title="1">{
return false
}</span>
}
<span class="cov0" title="0">return true</span>
}
// CalcDistance returns a new instance of a KademliaID that is built
// through a bitwise XOR operation betweeen kademliaID and target
func (kademliaID KademliaID) CalcDistance(target *KademliaID) *KademliaID <span class="cov8" title="1">{
result := KademliaID{}
for i := 0; i < IDLength; i++ </span><span class="cov8" title="1">{
result[i] = kademliaID[i] ^ target[i]
}</span>
<span class="cov8" title="1">return &result</span>
}
// String returns a simple string representation of a KademliaID
func (kademliaID *KademliaID) String() string <span class="cov8" title="1">{
return hex.EncodeToString(kademliaID[0:IDLength])
}</span>
</pre>
<pre class="file" id="file4" style="display: none">package kademlia
type Network struct {
}
func Listen(ip string, port int) {<span class="cov0" title="0">
// TODO
}</span>
func (network *Network) SendPingMessage(contact *Contact) {<span class="cov0" title="0">
// TODO
}</span>
func (network *Network) SendFindContactMessage(contact *Contact) {<span class="cov0" title="0">
// TODO
}</span>
func (network *Network) SendFindDataMessage(hash string) {<span class="cov0" title="0">
// TODO
}</span>
func (network *Network) SendStoreMessage(data []byte) {<span class="cov0" title="0">
// TODO
}</span>
</pre>
<pre class="file" id="file5" style="display: none">package kademlia
const bucketSize = 20
// RoutingTable definition
// keeps a refrence contact of me and an array of buckets
type RoutingTable struct {
me Contact
buckets [IDLength * 8]*bucket
}
// NewRoutingTable returns a new instance of a RoutingTable
func NewRoutingTable(me Contact) *RoutingTable <span class="cov8" title="1">{
routingTable := &RoutingTable{}
for i := 0; i < IDLength*8; i++ </span><span class="cov8" title="1">{
routingTable.buckets[i] = newBucket()
}</span>
<span class="cov8" title="1">routingTable.me = me
return routingTable</span>
}
// AddContact add a new contact to the correct Bucket
func (routingTable *RoutingTable) AddContact(contact Contact) <span class="cov8" title="1">{
bucketIndex := routingTable.getBucketIndex(contact.ID)
bucket := routingTable.buckets[bucketIndex]
bucket.AddContact(contact)
}</span>
// FindClosestContacts finds the count closest Contacts to the target in the RoutingTable
func (routingTable *RoutingTable) FindClosestContacts(target *KademliaID, count int) []Contact <span class="cov8" title="1">{
var candidates ContactCandidates
bucketIndex := routingTable.getBucketIndex(target)
bucket := routingTable.buckets[bucketIndex]
candidates.Append(bucket.GetContactAndCalcDistance(target))
for i := 1; (bucketIndex-i >= 0 || bucketIndex+i < IDLength*8) && candidates.Len() < count; i++ </span><span class="cov8" title="1">{
if bucketIndex-i >= 0 </span><span class="cov0" title="0">{
bucket = routingTable.buckets[bucketIndex-i]
candidates.Append(bucket.GetContactAndCalcDistance(target))
}</span>
<span class="cov8" title="1">if bucketIndex+i < IDLength*8 </span><span class="cov8" title="1">{
bucket = routingTable.buckets[bucketIndex+i]
candidates.Append(bucket.GetContactAndCalcDistance(target))
}</span>
}
<span class="cov8" title="1">candidates.Sort()
if count > candidates.Len() </span><span class="cov8" title="1">{
count = candidates.Len()
}</span>
<span class="cov8" title="1">return candidates.GetContacts(count)</span>
}
// getBucketIndex get the correct Bucket index for the KademliaID
func (routingTable *RoutingTable) getBucketIndex(id *KademliaID) int <span class="cov8" title="1">{
distance := id.CalcDistance(routingTable.me.ID)
for i := 0; i < IDLength; i++ </span><span class="cov8" title="1">{
for j := 0; j < 8; j++ </span><span class="cov8" title="1">{
if (distance[i]>>uint8(7-j))&0x1 != 0 </span><span class="cov8" title="1">{
return i*8 + j
}</span>
}
}
<span class="cov8" title="1">return IDLength*8 - 1</span>
}
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>