Skip to content

Commit

Permalink
lots of improvement in find
Browse files Browse the repository at this point in the history
  • Loading branch information
distributed committed Jan 26, 2010
1 parent d40afd4 commit 55708a2
Showing 1 changed file with 120 additions and 9 deletions.
129 changes: 120 additions & 9 deletions lib/find.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package malus


import (
"fmt"
"net"
)

type robotReturn struct {
rthost *RTHost
Expand All @@ -9,13 +13,90 @@ type robotReturn struct {
}


func robot(rh *RTHost, retchan chan *robotReturn) {
func robotParse(oid string, t string, retis []interface{}) (closest *RTHostList, retvals []interface{}) {

closest = nil
retvals = nil

if len(retis) != 1 {
fmt.Printf("find: len(retis) != 1 ?\n")
return
}


// TODO: disambiguate between direct findnode return values
// and findvalue style mapped retval/closest return values
wireclosest, ok := retis[0].([]interface{})
if !ok {
fmt.Printf("wire closest is not []interface{}")
}

closest = NewRTHostList()

for _, wci := range wireclosest {
wci, ok := wci.([]interface{})
if !ok {
continue
}

if len(wci) != 3 {
continue
}

host, ok := wci[0].(string)
if !ok {
continue
}

port64, ok := wci[0].(int64)
if !ok {
continue
}
var port int = int(uint16(port64))

id, ok := wci[0].(string)
if !ok {
continue
}
if len(id) != HASHLEN {
continue
}

addrstring := fmt.Sprintf("%s:%d", host, port)
addr, err := net.ResolveUDPAddr(addrstring)
if err != nil {
continue
}

rth := new(RTHost)
rth.Host.Addr = addr
rth.Host.Id = id
rth.Distance = XOR(oid, id)
closest.Push(rth)
}

return
}


func robot(oid string, t string, rh *RTHost, retchan chan *robotReturn, cm *CallManager) {
ret := new(robotReturn)

args := []interface{}{t}
retis, err := cm.Call(rh.Host.Addr, "findnode", args)

ret.rthost = rh
ret.closest = nil
ret.retvals = nil
if err != nil {
ret.closest = nil
ret.retvals = nil
} else {
fmt.Printf("robot parsing\n")
ret.closest, ret.retvals = robotParse(oid, t, retis)
fmt.Printf("robot parsing done\n")
}

fmt.Printf("robot done <=\n")

retchan <- ret
}

Expand All @@ -34,30 +115,53 @@ func find(t string, cm *CallManager, rt RoutingTable, bootstrap *RTHostList) {
}

kclosest.Sort()
visited := make(map[string]*RTHost)

closestd := kclosest.At(0).Distance //MaxDistance
alpha := Alpha

retchan := make(chan *robotReturn) // dummy
retchan := make(chan *robotReturn)
nrunning := 0
nqueried := 0
converging := true

for converging {
fmt.Printf("find: convering round\n")
for (nrunning < alpha) && (kclosest.Len() > 0) {
fmt.Printf("=> robot w/ %d left\n", kclosest.Len())
rh := kclosest.PopFront()
go robot(rh, retchan)
straddr := rh.Host.Addr.String()
if _, ok := visited[straddr]; ok {
// host already visited
continue
}
go robot(cm.Id, t, rh, retchan, cm)
visited[straddr] = rh
nrunning++
}

if nrunning == 0 {
break
}

ret := <-retchan
nrunning--
nqueried++

if ret.closest == nil {
panicln("handle this more intelligently...")
//panicln("handle this more intelligently...")
continue
}
kclosest.Append(ret.closest)
//fmt.Printf("find: append\n")
//kclosest.Append(ret.closest)
for i := 0; i < ret.closest.Len(); i++ {
el := ret.closest.At(i)
addrstr := el.Host.Addr.String()
if _, ok := visited[addrstr]; !ok {
kclosest.Push(el)
}
}
//fmt.Printf("find: sort\n")
kclosest.Sort()
l := kclosest.Len()
if l >= K {
Expand All @@ -76,12 +180,19 @@ func find(t string, cm *CallManager, rt RoutingTable, bootstrap *RTHostList) {
}
}

// TODO: drain channel

fmt.Printf("find: not converging any more\n")

// the search is not converging any more. now make sure all k
// nodes are queried
for (nqueried < K) || (nrunning > 0) {
/*for (nqueried < K) || (nrunning > 0) {
// if we can spawn more goroutines => spawn them
// read results similar to code above
}
}*/

// TODO: as above, either drain channel or make channel
// buffered, so robots actually terminate and channel gets
// garbitsch collected
}

0 comments on commit 55708a2

Please sign in to comment.