dht.go 6 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package dht

3
import (
4
	"sync"
5
	"time"
6

Jeromy's avatar
Jeromy committed
7 8 9
	peer	"github.com/jbenet/go-ipfs/peer"
	swarm	"github.com/jbenet/go-ipfs/swarm"
	u		"github.com/jbenet/go-ipfs/util"
10 11 12
	identify "github.com/jbenet/go-ipfs/identify"

	ma "github.com/jbenet/go-multiaddr"
Jeromy's avatar
Jeromy committed
13 14 15

	ds "github.com/jbenet/datastore.go"

16
	"code.google.com/p/goprotobuf/proto"
17 18
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20 21 22 23
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js

// IpfsDHT is an implementation of Kademlia with Coral and S/Kademlia modifications.
// It is used to implement the base IpfsRouting module.
type IpfsDHT struct {
24
	routes *RoutingTable
25

26 27
	network *swarm.Swarm

Jeromy's avatar
Jeromy committed
28 29 30 31 32
	// Local peer (yourself)
	self *peer.Peer

	// Local data
	datastore ds.Datastore
33

34 35
	// map of channels waiting for reply messages
	listeners  map[uint64]chan *swarm.Message
36
	listenLock sync.RWMutex
37 38 39

	// Signal to shutdown dht
	shutdown chan struct{}
40 41
}

42 43
// Create a new DHT object with the given peer as the 'local' host
func NewDHT(p *peer.Peer) (*IpfsDHT, error) {
44 45 46 47 48
	network := swarm.NewSwarm(p)
	err := network.Listen()
	if err != nil {
		return nil,err
	}
49

50 51
	dht := new(IpfsDHT)
	dht.network = network
52 53
	dht.datastore = ds.NewMapDatastore()
	dht.self = p
Jeromy's avatar
Jeromy committed
54 55
	dht.listeners = make(map[uint64]chan *swarm.Message)
	dht.shutdown = make(chan struct{})
56
	dht.routes = NewRoutingTable(20, convertPeerID(p.ID))
57 58 59
	return dht, nil
}

60
// Start up background goroutines needed by the DHT
61 62 63 64
func (dht *IpfsDHT) Start() {
	go dht.handleMessages()
}

65 66 67 68 69 70 71 72 73 74
// Connect to a new peer at the given address
func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) error {
	peer := new(peer.Peer)
	peer.AddAddress(addr)

	conn,err := swarm.Dial("tcp", peer)
	if err != nil {
		return err
	}

75
	err = identify.Handshake(dht.self, peer, conn.Incoming.MsgChan, conn.Outgoing.MsgChan)
76 77 78 79
	if err != nil {
		return err
	}

80
	dht.network.StartConn(conn)
81

82
	dht.routes.Update(peer)
83
	return nil
Jeromy's avatar
Jeromy committed
84 85
}

86 87
// Read in all messages from swarm and handle them appropriately
// NOTE: this function is just a quick sketch
88
func (dht *IpfsDHT) handleMessages() {
89
	u.DOut("Begin message handling routine")
90 91 92
	for {
		select {
		case mes := <-dht.network.Chan.Incoming:
93 94
			u.DOut("recieved message from swarm.")

95 96 97 98 99 100 101
			pmes := new(DHTMessage)
			err := proto.Unmarshal(mes.Data, pmes)
			if err != nil {
				u.PErr("Failed to decode protobuf message: %s", err)
				continue
			}

102 103 104
			// Update peers latest visit in routing table
			dht.routes.Update(mes.Peer)

105
			// Note: not sure if this is the correct place for this
106 107 108 109 110 111 112 113 114 115 116
			if pmes.GetResponse() {
				dht.listenLock.RLock()
				ch, ok := dht.listeners[pmes.GetId()]
				dht.listenLock.RUnlock()
				if ok {
					ch <- mes
				}

				// this is expected behaviour during a timeout
				u.DOut("Received response with nobody listening...")
				continue
117
			}
118 119
			//

120
			u.DOut("Got message type: %d", pmes.GetType())
121
			switch pmes.GetType() {
122 123 124
			case DHTMessage_GET_VALUE:
				dht.handleGetValue(mes.Peer, pmes)
			case DHTMessage_PUT_VALUE:
Jeromy's avatar
Jeromy committed
125
				dht.handlePutValue(mes.Peer, pmes)
126
			case DHTMessage_FIND_NODE:
Jeromy's avatar
Jeromy committed
127
				dht.handleFindNode(mes.Peer, pmes)
128
			case DHTMessage_ADD_PROVIDER:
129 130
			case DHTMessage_GET_PROVIDERS:
			case DHTMessage_PING:
131
				dht.handlePing(mes.Peer, pmes)
132 133
			}

134 135
		case err := <-dht.network.Chan.Errors:
			panic(err)
136 137
		case <-dht.shutdown:
			return
138
		}
139
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140
}
141

142
func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *DHTMessage) {
Jeromy's avatar
Jeromy committed
143 144 145
	dskey := ds.NewKey(pmes.GetKey())
	i_val, err := dht.datastore.Get(dskey)
	if err == nil {
146 147 148 149 150 151 152 153 154
		resp := &pDHTMessage{
			Response: true,
			Id: *pmes.Id,
			Key: *pmes.Key,
			Value: i_val.([]byte),
		}

		mes := swarm.NewMessage(p, resp.ToProtobuf())
		dht.network.Chan.Outgoing <- mes
Jeromy's avatar
Jeromy committed
155
	} else if err == ds.ErrNotFound {
156 157 158 159 160 161 162
		// Find closest node(s) to desired key and reply with that info
		// TODO: this will need some other metadata in the protobuf message
		//			to signal to the querying node that the data its receiving
		//			is actually a list of other nodes
	}
}

Jeromy's avatar
Jeromy committed
163
// Store a value in this nodes local storage
164
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *DHTMessage) {
Jeromy's avatar
Jeromy committed
165 166 167 168 169 170
	dskey := ds.NewKey(pmes.GetKey())
	err := dht.datastore.Put(dskey, pmes.GetValue())
	if err != nil {
		// For now, just panic, handle this better later maybe
		panic(err)
	}
171 172 173
}

func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *DHTMessage) {
174 175 176 177 178
	resp := &pDHTMessage{
		Type: pmes.GetType(),
		Response: true,
		Id: pmes.GetId(),
	}
179

180
	dht.network.Chan.Outgoing <-swarm.NewMessage(p, resp.ToProtobuf())
181 182
}

183 184 185 186 187 188 189 190 191 192 193 194
func (dht *IpfsDHT) handleFindNode(p *peer.Peer, pmes *DHTMessage) {
	panic("Not implemented.")
}

func (dht *IpfsDHT) handleGetProviders(p *peer.Peer, pmes *DHTMessage) {
	panic("Not implemented.")
}

func (dht *IpfsDHT) handleAddProvider(p *peer.Peer, pmes *DHTMessage) {
	panic("Not implemented.")
}

195

196 197
// Register a handler for a specific message ID, used for getting replies
// to certain messages (i.e. response to a GET_VALUE message)
198 199
func (dht *IpfsDHT) ListenFor(mesid uint64) <-chan *swarm.Message {
	lchan := make(chan *swarm.Message)
200 201 202 203 204
	dht.listenLock.Lock()
	dht.listeners[mesid] = lchan
	dht.listenLock.Unlock()
	return lchan
}
205

206
// Unregister the given message id from the listener map
Jeromy's avatar
Jeromy committed
207 208 209 210 211 212 213 214 215 216
func (dht *IpfsDHT) Unlisten(mesid uint64) {
	dht.listenLock.Lock()
	ch, ok := dht.listeners[mesid]
	if ok {
		delete(dht.listeners, mesid)
	}
	dht.listenLock.Unlock()
	close(ch)
}

217 218 219 220 221
// Stop all communications from this node and shut down
func (dht *IpfsDHT) Halt() {
	dht.shutdown <- struct{}{}
	dht.network.Close()
}
222 223

// Ping a node, log the time it took
224
func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) error {
225
	// Thoughts: maybe this should accept an ID and do a peer lookup?
226 227
	u.DOut("Enter Ping.")

228 229
	pmes := pDHTMessage{Id: GenerateMessageID(), Type: DHTMessage_PING}
	mes := swarm.NewMessage(p, pmes.ToProtobuf())
230 231

	before := time.Now()
232
	response_chan := dht.ListenFor(pmes.Id)
233 234 235 236 237 238
	dht.network.Chan.Outgoing <- mes

	tout := time.After(timeout)
	select {
	case <-response_chan:
		roundtrip := time.Since(before)
239 240
		u.POut("Ping took %s.", roundtrip.String())
		return nil
241 242 243
	case <-tout:
		// Timed out, think about removing node from network
		u.DOut("Ping node timed out.")
244
		return u.ErrTimeout
245 246
	}
}