Skip to content

Commit

Permalink
made fields of RTHost public
Browse files Browse the repository at this point in the history
  • Loading branch information
distributed committed Jan 22, 2010
1 parent b6fb4bd commit 9f7d95f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
67 changes: 35 additions & 32 deletions lib/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"bytes"
"fmt"
)
)

type RoutingTable interface {
SeeHost(h *Host)
Expand All @@ -18,23 +18,24 @@ type RoutingTable interface {


type BRoutingTable struct {
id string
buckets [](*Bucket)
id string
buckets [](*Bucket)
maxbucket int

seehost chan *Host
quitchan chan bool
seehost chan *Host
quitchan chan bool
stringchan chan chan string
htmlchan chan chan string
htmlchan chan chan string

Log bool
Log bool
logger *log.Logger
}


type RTHost struct {
host *Host
distance Distance
Host *Host
Distance Distance
}
}

type Bucket struct {
Expand All @@ -47,9 +48,9 @@ func NewBRoutingTable(id string) (rt *BRoutingTable) {

rt.id = id

rt.buckets = make([](*Bucket), HASHLEN*8)
rt.buckets = make([](*Bucket), HASHLEN * 8)
firstbucket := new(Bucket)
firstbucket.hosts = make([](*RTHost), K+1)[0:0]
firstbucket.hosts = make([](*RTHost), K + 1)[0:0]

rt.buckets[0] = firstbucket
rt.maxbucket = 0
Expand All @@ -69,28 +70,30 @@ func NewBRoutingTable(id string) (rt *BRoutingTable) {
func (rt *BRoutingTable) main() {
for {
select {
case h := <-rt.seehost:
case h := <- rt.seehost:
rt.seeHost(h)
case <-rt.quitchan:
case <- rt.quitchan:
return
case r := <-rt.stringchan:
case r := <- rt.stringchan:
r <- rt.string()
case r := <-rt.htmlchan:
case r := <- rt.htmlchan:
r <- rt.html()
}
}
}


func (rt *BRoutingTable) SeeHost(h *Host) { rt.seehost <- h }
func (rt *BRoutingTable) SeeHost(h *Host) {
rt.seehost <- h
}


func (rt *BRoutingTable) seeHost(h *Host) {
if rt.Log {
rt.logger.Logf("see host: %v dist %v\n", h, XOR(h.Id, rt.id)[0:5])
}
bucketno, pos, maxbucketno := rt.findHost(h)
/*rt.logger.Logf("is in %d/%d maxbucketno %d\n", bucketno, pos, maxbucketno)
/*rt.logger.Logf("is in %d/%d maxbucketno %d\n", bucketno, pos, maxbucketno)
rt.logger.Logf("dist %v to %v\n", XOR(h.Id, rt.id), h)
rt.logger.Logf("own id is %x\n", rt.id)
rt.logger.Logf("h.Id is %x", h.Id)*/
Expand All @@ -109,7 +112,7 @@ func (rt *BRoutingTable) seeHost(h *Host) {
if rt.Log {
rt.logger.Logf("bucket %d not full yet -> inserting\n", bucketno)
}
bucket.hosts = bucket.hosts[0 : hl+1]
bucket.hosts = bucket.hosts[0:hl+1]
bucket.hosts[hl] = rthost
} else {
if maxbucketno == bucketno {
Expand All @@ -118,7 +121,7 @@ func (rt *BRoutingTable) seeHost(h *Host) {
}
} else {
rt.newBucket()
bucket.hosts = bucket.hosts[0 : hl+1]
bucket.hosts = bucket.hosts[0:hl+1]
bucket.hosts[hl] = rthost
rt.balanceleftright(uint(bucketno))
}
Expand All @@ -135,16 +138,16 @@ func (rt *BRoutingTable) seeHost(h *Host) {


func (rt *BRoutingTable) newBucket() *Bucket {
if rt.maxbucket == (HASHLEN*8 - 1) {
if rt.maxbucket == (HASHLEN * 8 - 1) {
return nil
}

b := new(Bucket)
b.hosts = make([](*RTHost), K+1)[0:0]
b.hosts = make([](*RTHost), K + 1)[0:0]

rt.maxbucket++
rt.buckets[rt.maxbucket] = b

return b
}

Expand Down Expand Up @@ -186,8 +189,8 @@ func (rt *BRoutingTable) balanceleftright(lefti uint) {
left := rt.buckets[lefti]
right := rt.buckets[righti]

newleft := make([](*RTHost), K+1)
newright := make([](*RTHost), K+1)
newleft := make([](*RTHost), K + 1)
newright := make([](*RTHost), K + 1)

nleft := 0
nright := 0
Expand All @@ -213,13 +216,13 @@ func (rt *BRoutingTable) balanceleftright(lefti uint) {
}

rt.logger.Logf("rebalanced to %d/%d\n", nleft, nright)

newleft = newleft[0:nleft]
newright = newright[0:nright]

left.hosts = newleft
right.hosts = newright

fmt.Printf("%v", rt)
}

Expand All @@ -232,10 +235,10 @@ func (rt *BRoutingTable) string() string {
for b := 0; b <= rt.maxbucket; b++ {
buf.WriteString(fmt.Sprintf("bucket %d\n", b))
for _, rth := range rt.buckets[b].hosts {
buf.WriteString(fmt.Sprintf("\t%x | %v @ %v\n", rth.host.Id, XOR(rth.host.Id, rt.id)[0:5], rth.host.Addr))
buf.WriteString(fmt.Sprintf("\t%x | %v @ %v\n", rth.Host.Id, XOR(rth.Host.Id, rt.id)[0:5], rth.Host.Addr))
}
}

buf.WriteString("<===\n")
return buf.String()
}
Expand All @@ -255,13 +258,13 @@ func (rt *BRoutingTable) html() string {
for b := 0; b <= rt.maxbucket; b++ {
buf.WriteString("<td>")
for _, rth := range rt.buckets[b].hosts {
buf.WriteString(fmt.Sprintf("\t%x | %v @ %v<br>\n", rth.host.Id, XOR(rth.host.Id, rt.id)[0:5], rth.host.Addr))
buf.WriteString(fmt.Sprintf("\t%x | %v @ %v<br>\n", rth.Host.Id, XOR(rth.Host.Id, rt.id)[0:5], rth.Host.Addr))
}
buf.WriteString("</td>")
}

buf.WriteString("</tr>")

buf.WriteString("</table>")
return buf.String()
}
Expand All @@ -271,12 +274,12 @@ func (rt *BRoutingTable) html() string {
func (rt *BRoutingTable) GetString() string {
r := make(chan string)
rt.stringchan <- r
return <-r
return <- r
}

// goroutine safe. not for internal use!
func (rt *BRoutingTable) GetHTML() string {
r := make(chan string)
rt.htmlchan <- r
return <-r
return <- r
}
18 changes: 9 additions & 9 deletions lib/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)


type Distance []byte
type Distance []byte;


func (d Distance) String() string { return hex.EncodeToString(d) }

func (d Distance) String() string {
return hex.EncodeToString(d)
}
const K = 20

func SHA1Bytes(b []byte) string {
h := sha1.New()
h.Write(b)
Expand All @@ -32,10 +32,10 @@ func XOR(a, b string) Distance {
if l != len(b) {
return nil
}

d := make(Distance, l)

for i := 0; i < l; i++ {
for i := 0; i < l; i ++ {
d[i] = a[i] ^ b[i]
}

Expand All @@ -56,14 +56,14 @@ func BucketNo(d Distance) uint {
continue
}
var bitnr uint = 0
for i := 0; i < 8; i++ {
for i := 0; i < 8; i ++ {
if (b & (0x80 >> bitnr)) != 0 {
return basebitnr + bitnr
}
bitnr++
}
}

return basebitnr
}

Expand All @@ -75,7 +75,7 @@ func (a Distance) Less(b Distance) bool {

//fmt.Printf("distance Less called: %v < %v\n", a, b)

for i, ea := range a {
for i, ea := range(a) {
eb := b[i]
switch {
case ea < eb:
Expand Down

0 comments on commit 9f7d95f

Please sign in to comment.