diff --git a/lib/routing.go b/lib/routing.go index d0e3b63..b82d2e1 100644 --- a/lib/routing.go +++ b/lib/routing.go @@ -6,7 +6,7 @@ import ( "log" "bytes" "fmt" -) + ) type RoutingTable interface { SeeHost(h *Host) @@ -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 { @@ -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 @@ -69,20 +70,22 @@ 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) { @@ -90,7 +93,7 @@ func (rt *BRoutingTable) seeHost(h *Host) { 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)*/ @@ -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 { @@ -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)) } @@ -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 } @@ -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 @@ -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) } @@ -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() } @@ -255,13 +258,13 @@ func (rt *BRoutingTable) html() string { for b := 0; b <= rt.maxbucket; b++ { buf.WriteString("") 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("") } buf.WriteString("") - + buf.WriteString("") return buf.String() } @@ -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 } diff --git a/lib/utility.go b/lib/utility.go index 69e77d3..5d6fe74 100644 --- a/lib/utility.go +++ b/lib/utility.go @@ -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) @@ -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] } @@ -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 } @@ -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: