Skip to content

Commit 839a96c

Browse files
committed
routing.go: added CopyRTHost function. this fixes
behaviour of getclosest
1 parent ec9be34 commit 839a96c

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lib/routing.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ func NewRTHostList() *RTHostList {
6363
return r
6464
}
6565

66+
// Copies an RTHost and computes the Distance of rh.Host to newref. If
67+
// newref is "", the old distance is used
68+
func (rh *RTHost) Copy(newref string) (rhc *RTHost) {
69+
rhc = new(RTHost)
70+
rhc.Host = rh.Host
71+
if newref == "" {
72+
newref = rh.Host.Id
73+
}
74+
rhc.Distance = XOR(newref, rhc.Host.Id)
75+
return
76+
}
77+
6678
func (l *RTHostList) Push(h *RTHost) { l.v.Push(h) }
6779

6880
func (l *RTHostList) Len() int {
@@ -348,7 +360,7 @@ func (rt *BRoutingTable) GetClosest(t string, n uint) *RTHostList {
348360
return <-r
349361
}
350362

351-
// TODO: wtf behaviour? gets the closests n nodes to us close to t????
363+
352364
// not goroutine safe. for internal use!
353365
func (rt *BRoutingTable) getClosest(t string, n uint) *RTHostList {
354366
//rt.logger.Logf("looking for %x dist %v<br>\n", t, XOR(rt.id, t))
@@ -369,7 +381,7 @@ func (rt *BRoutingTable) getClosest(t string, n uint) *RTHostList {
369381
var rn uint = 0
370382

371383
for _, el := range buckets[bucketno].hosts {
372-
hl.Push(el)
384+
hl.Push(el.Copy(t))
373385
rn++
374386
}
375387

@@ -389,14 +401,14 @@ func (rt *BRoutingTable) getClosest(t string, n uint) *RTHostList {
389401

390402
if cangoup {
391403
for _, el := range buckets[bucketno+delta].hosts {
392-
hl.Push(el)
404+
hl.Push(el.Copy(t))
393405
rn++
394406
}
395407
}
396408

397409
if cangodown {
398410
for _, el := range buckets[bucketno-delta].hosts {
399-
hl.Push(el)
411+
hl.Push(el.Copy(t))
400412
rn++
401413
}
402414
}

0 commit comments

Comments
 (0)