Skip to content

Commit

Permalink
Moved booting to kademlia library
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsnew committed Mar 20, 2013
1 parent 2a5b60d commit 4d60402
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 54 deletions.
60 changes: 49 additions & 11 deletions kademlia/kademlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package kademlia
import (
"container/list"
"log"
"math/rand"
"net"
"net/http"
"net/rpc"
"time"
)

// Contains the core kademlia type. In addition to core state, this type serves
Expand All @@ -20,21 +24,24 @@ type Kademlia struct {
Table map[ID][]byte // TODO: republishes
}

func NewKademlia(address string) *Kademlia {
ip, port, err := parseAddress(address)
func NewKademlia(address string, firstPeerAddr string) (k *Kademlia, err error) {
k, err = NewUnBootedKademlia(address, firstPeerAddr)
if err != nil {
log.Fatal("bad address")
return
}
return newKademliaSplitAddress(ip, port)
}
err = k.bootUp(address, firstPeerAddr)

func LocalLookup(k *Kademlia, key ID) ([]byte, bool) {
val, ok := k.Table[key]
return val, ok
return
}

func newKademliaSplitAddress(ip net.IP, port uint16) *Kademlia {
k := new(Kademlia)
func NewUnBootedKademlia(listenAddr, peerAddr string) (k *Kademlia, err error) {
ip, port, err := parseAddress(listenAddr)
if err != nil {
return
}
rand.Seed(time.Now().UnixNano())

k = new(Kademlia)
k.NodeID = NewRandomID()
k.Self = Contact{k.NodeID, ip, port}
for i, _ := range k.Buckets {
Expand All @@ -45,7 +52,38 @@ func newKademliaSplitAddress(ip net.IP, port uint16) *Kademlia {
}
k.updateContact(k.Self)
k.Table = make(map[ID][]byte)
return k
return
}

func LocalLookup(k *Kademlia, key ID) ([]byte, bool) {
val, ok := k.Table[key]
return val, ok
}

func (k *Kademlia) bootUp(listenAddr string, peerAddr string) (err error) {
rpc.Register(k)
rpc.HandleHTTP()

l, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Fatal("Listen: ", err)
return
}

// Serve forever.
go http.Serve(l, nil)

_, err = SendPing(k, peerAddr)
if err != nil {
log.Fatal("Initial ping error: ", err)
return
}

_, err = IterativeFindNode(k, k.NodeID)
if err != nil {
log.Fatal("Bootstrap find_node error: ", err)
}
return
}

func (k *Kademlia) removeContact(id ID) {
Expand Down
20 changes: 12 additions & 8 deletions kademlia/kademlia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package kademlia
import (
"fmt"
"log"
"math/rand"
"net"
"net/http"
"net/rpc"
"testing"
"time"
)

func ExampleIndex() {
k := NewKademlia("127.0.0.1:8890")
k, err := NewUnBootedKademlia("127.0.0.1:8890", "127.0.0.1:8890")
if err != nil {
log.Fatal(err)
return
}
k.NodeID = HalfHalfID()
other := OnesID()
index := k.index(other)
Expand Down Expand Up @@ -115,7 +114,11 @@ func TestOnesIndices(t *testing.T) {
}

func TestDoInSearchOrder(t *testing.T) {
k := NewKademlia("127.0.0.1:8890")
k, err := NewUnBootedKademlia("127.0.0.1:8890", "127.0.0.1:8890")
if err != nil {
t.Log(err)
t.Fail()
}
k.NodeID = HalfHalfID()

other := OnesID()
Expand All @@ -134,6 +137,7 @@ func TestDoInSearchOrder(t *testing.T) {

}

/*
func bootupKademlia(listenStr string, firstPeerStr string) *Kademlia {
// By default, Go seeds its RNG with 1. This would cause every program to
// generate the same sequence of IDs.
Expand Down Expand Up @@ -179,7 +183,7 @@ func bootupKademlia(listenStr string, firstPeerStr string) *Kademlia {
}
return kadem
}

*/
// func TestDoStore(t *testing.T) {
// bootupKademlia("127.0.0.1:8765", "127.0.0.1:8765")
// // val := []byte("foobar")
Expand Down
71 changes: 36 additions & 35 deletions tfh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ import (
"flag"
"fmt"
"log"
"math/rand"
"net"
"net/http"
"net/rpc"
"os"
"time"
)

import (
Expand All @@ -29,7 +24,7 @@ import (
func main() {
// By default, Go seeds its RNG with 1. This would cause every program to
// generate the same sequence of IDs.
rand.Seed(time.Now().UnixNano())
// rand.Seed(time.Now().UnixNano())

// Get the bind and connect connection strings from command-line arguments.
flag.Parse()
Expand All @@ -41,45 +36,51 @@ func main() {
firstPeerStr := args[1]

fmt.Printf("kademlia starting up!\n")
kadem := kademlia.NewKademlia(listenStr)
tfh := NewTFH(kadem)
rpc.Register(kadem)
rpc.HandleHTTP()
l, err := net.Listen("tcp", listenStr)
kadem, err := kademlia.NewKademlia(listenStr, firstPeerStr)
if err != nil {
log.Fatal("Listen: ", err)
log.Fatal(err)
return
}
tfh := NewTFH(kadem)

// Serve forever.
go http.Serve(l, nil)
/*
rpc.Register(kadem)
rpc.HandleHTTP()
l, err := net.Listen("tcp", listenStr)
if err != nil {
log.Fatal("Listen: ", err)
}
// Confirm our server is up with a PING request and then exit.
// Your code should loop forever, reading instructions from stdin and
// printing their results to stdout. See README.txt for more details.
pong, err := kademlia.SendPing(kadem, firstPeerStr)
if err != nil {
log.Fatal("Initial ping error: ", err)
}
// Serve forever.
go http.Serve(l, nil)
fmt.Printf("pong msgID: %s\n", pong.MsgID.AsString())
// Confirm our server is up with a PING request and then exit.
// Your code should loop forever, reading instructions from stdin and
// printing their results to stdout. See README.txt for more details.
pong, err := kademlia.SendPing(kadem, firstPeerStr)
if err != nil {
log.Fatal("Initial ping error: ", err)
}
// THis was the workaround, Ted.
// _, err = kademlia.SendFindNodeAddr(kadem, kadem.NodeID, firstPeerStr)
fmt.Printf("pong msgID: %s\n", pong.MsgID.AsString())
// var foundNodes []kademlia.Contact
// if err == nil {
foundNodes, err := kademlia.IterativeFindNode(kadem, kadem.NodeID)
// }
// THis was the workaround, Ted.
// _, err = kademlia.SendFindNodeAddr(kadem, kadem.NodeID, firstPeerStr)
if err != nil {
log.Fatal("Bootstrap find_node error: ", err)
}
// var foundNodes []kademlia.Contact
// if err == nil {
foundNodes, err := kademlia.IterativeFindNode(kadem, kadem.NodeID)
// }
fmt.Println("Received", len(foundNodes), "nodes")
for i, node := range foundNodes {
fmt.Println("Node ", i, ": ", node.NodeID.AsString())
}
if err != nil {
log.Fatal("Bootstrap find_node error: ", err)
}
fmt.Println("Received", len(foundNodes), "nodes")
for i, node := range foundNodes {
fmt.Println("Node ", i, ": ", node.NodeID.AsString())
}
*/
r := bufio.NewReader(os.Stdin)
for {
line, err := r.ReadString('\n')
Expand Down

0 comments on commit 4d60402

Please sign in to comment.