Skip to content

Commit

Permalink
Merge pull request #25 from jbenet/dht
Browse files Browse the repository at this point in the history
DHT PR
  • Loading branch information
jbenet committed Aug 16, 2014
2 parents 8203d2c + 8a1fdbb commit 2a7dafd
Show file tree
Hide file tree
Showing 22 changed files with 2,444 additions and 215 deletions.
20 changes: 20 additions & 0 deletions identify/identify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// The identify package handles how peers identify with eachother upon
// connection to the network
package identify

import (
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
)

// Perform initial communication with this peer to share node ID's and
// initiate communication
func Handshake(self, remote *peer.Peer, in, out chan []byte) error {
// TODO: make this more... secure.
out <- self.ID
resp := <-in
remote.ID = peer.ID(resp)
u.DOut("[%s] identify: Got node id: %s", self.ID.Pretty(), remote.ID.Pretty())

return nil
}
3 changes: 3 additions & 0 deletions identify/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message Identify {
required bytes id = 1;
}
30 changes: 28 additions & 2 deletions peer/peer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package peer

import (
"sync"
"time"

b58 "github.com/jbenet/go-base58"
u "github.com/jbenet/go-ipfs/util"
ma "github.com/jbenet/go-multiaddr"
mh "github.com/jbenet/go-multihash"
Expand All @@ -12,8 +16,12 @@ import (
type ID mh.Multihash

// Utililty function for comparing two peer ID's
func (id *ID) Equal(other *ID) bool {
return bytes.Equal(*id, *other)
func (id ID) Equal(other ID) bool {
return bytes.Equal(id, other)
}

func (id ID) Pretty() string {
return b58.Encode(id)
}

// Map maps Key (string) : *Peer (slices are not comparable).
Expand All @@ -24,6 +32,9 @@ type Map map[u.Key]*Peer
type Peer struct {
ID ID
Addresses []*ma.Multiaddr

latency time.Duration
latenLock sync.RWMutex
}

// Key returns the ID as a Key (string) for maps.
Expand Down Expand Up @@ -52,3 +63,18 @@ func (p *Peer) NetAddress(n string) *ma.Multiaddr {
}
return nil
}

func (p *Peer) GetLatency() (out time.Duration) {
p.latenLock.RLock()
out = p.latency
p.latenLock.RUnlock()
return
}

// TODO: Instead of just keeping a single number,
// keep a running average over the last hour or so
func (p *Peer) SetLatency(laten time.Duration) {
p.latenLock.Lock()
p.latency = laten
p.latenLock.Unlock()
}
49 changes: 49 additions & 0 deletions routing/dht/DHTMessage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dht

import (
peer "github.com/jbenet/go-ipfs/peer"
)

// A helper struct to make working with protbuf types easier
type DHTMessage struct {
Type PBDHTMessage_MessageType
Key string
Value []byte
Response bool
Id uint64
Success bool
Peers []*peer.Peer
}

func peerInfo(p *peer.Peer) *PBDHTMessage_PBPeer {
pbp := new(PBDHTMessage_PBPeer)
addr, err := p.Addresses[0].String()
if err != nil {
//Temp: what situations could cause this?
panic(err)
}
pbp.Addr = &addr
pid := string(p.ID)
pbp.Id = &pid
return pbp
}

// TODO: building the protobuf message this way is a little wasteful
// Unused fields wont be omitted, find a better way to do this
func (m *DHTMessage) ToProtobuf() *PBDHTMessage {
pmes := new(PBDHTMessage)
if m.Value != nil {
pmes.Value = m.Value
}

pmes.Type = &m.Type
pmes.Key = &m.Key
pmes.Response = &m.Response
pmes.Id = &m.Id
pmes.Success = &m.Success
for _, p := range m.Peers {
pmes.Peers = append(pmes.Peers, peerInfo(p))
}

return pmes
}
Loading

0 comments on commit 2a7dafd

Please sign in to comment.