handlers.go 5.73 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10
package dht

import (
	"errors"
	"fmt"
	"time"

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

11
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
)

14 15
var CloserPeerCount = 4

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
// dhthandler specifies the signature of functions that handle DHT messages.
17
type dhtHandler func(peer.Peer, *Message) (*Message, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19 20 21 22

func (dht *IpfsDHT) handlerForMsgType(t Message_MessageType) dhtHandler {
	switch t {
	case Message_GET_VALUE:
		return dht.handleGetValue
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24
	case Message_PUT_VALUE:
		return dht.handlePutValue
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26
	case Message_FIND_NODE:
		return dht.handleFindPeer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29 30
	case Message_ADD_PROVIDER:
		return dht.handleAddProvider
	case Message_GET_PROVIDERS:
		return dht.handleGetProviders
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32 33 34 35 36 37
	case Message_PING:
		return dht.handlePing
	default:
		return nil
	}
}

38
func (dht *IpfsDHT) handleGetValue(p peer.Peer, pmes *Message) (*Message, error) {
39
	log.Debug("%s handleGetValue for key: %s\n", dht.self, pmes.GetKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41

	// setup response
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
	resp := newMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43 44 45 46 47 48 49 50

	// first, is the key even a key?
	key := pmes.GetKey()
	if key == "" {
		return nil, errors.New("handleGetValue but no key was provided")
	}

	// let's first check if we have the value locally.
Jeromy's avatar
Jeromy committed
51
	log.Debug("%s handleGetValue looking into ds", dht.self)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
	dskey := u.Key(pmes.GetKey()).DsKey()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
	iVal, err := dht.datastore.Get(dskey)
Jeromy's avatar
Jeromy committed
54
	log.Debug("%s handleGetValue looking into ds GOT %v", dht.self, iVal)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56

	// if we got an unexpected error, bail.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
	if err != nil && err != ds.ErrNotFound {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58 59 60
		return nil, err
	}

61 62 63 64
	// Note: changed the behavior here to return _as much_ info as possible
	// (potentially all of {value, closer peers, provider})

	// if we have the value, send it back
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
	if err == nil {
Jeromy's avatar
Jeromy committed
66
		log.Debug("%s handleGetValue success!", dht.self)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67 68 69 70 71 72 73 74 75 76 77 78

		byts, ok := iVal.([]byte)
		if !ok {
			return nil, fmt.Errorf("datastore had non byte-slice value for %v", dskey)
		}

		resp.Value = byts
	}

	// if we know any providers for the requested value, return those.
	provs := dht.providers.GetProviders(u.Key(pmes.GetKey()))
	if len(provs) > 0 {
79
		log.Debug("handleGetValue returning %d provider[s]\n", len(provs))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83
		resp.ProviderPeers = peersToPBPeers(provs)
	}

	// Find closest peer on given cluster to desired key and reply with that info
84
	closer := dht.betterPeersToQuery(pmes, CloserPeerCount)
85
	if closer != nil {
86 87
		for _, p := range closer {
			log.Debug("handleGetValue returning closer peer: '%s'", p)
Jeromy's avatar
Jeromy committed
88 89 90
			if len(p.Addresses()) < 1 {
				log.Error("no addresses on peer being sent!")
			}
91 92
		}
		resp.CloserPeers = peersToPBPeers(closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94 95 96 97 98
	}

	return resp, nil
}

// Store a value in this peer local storage
99
func (dht *IpfsDHT) handlePutValue(p peer.Peer, pmes *Message) (*Message, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100 101
	dht.dslock.Lock()
	defer dht.dslock.Unlock()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102
	dskey := u.Key(pmes.GetKey()).DsKey()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103
	err := dht.datastore.Put(dskey, pmes.GetValue())
104
	log.Debug("%s handlePutValue %v %v\n", dht.self, dskey, pmes.GetValue())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105
	return pmes, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106 107
}

108
func (dht *IpfsDHT) handlePing(p peer.Peer, pmes *Message) (*Message, error) {
109
	log.Debug("%s Responding to ping from %s!\n", dht.self, p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
	return pmes, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111 112
}

113
func (dht *IpfsDHT) handleFindPeer(p peer.Peer, pmes *Message) (*Message, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114
	resp := newMessage(pmes.GetType(), "", pmes.GetClusterLevel())
115
	var closest []peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116 117

	// if looking for self... special case where we send it on CloserPeers.
118 119
	if peer.ID(pmes.GetKey()).Equal(dht.self.ID()) {
		closest = []peer.Peer{dht.self}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
	} else {
121
		closest = dht.betterPeersToQuery(pmes, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122 123 124
	}

	if closest == nil {
125
		log.Error("handleFindPeer: could not find anything.")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126 127 128
		return resp, nil
	}

129
	var withAddresses []peer.Peer
130
	for _, p := range closest {
131
		if len(p.Addresses()) > 0 {
132 133
			withAddresses = append(withAddresses, p)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
134 135
	}

136 137 138 139
	for _, p := range withAddresses {
		log.Debug("handleFindPeer: sending back '%s'", p)
	}
	resp.CloserPeers = peersToPBPeers(withAddresses)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141 142
	return resp, nil
}

143
func (dht *IpfsDHT) handleGetProviders(p peer.Peer, pmes *Message) (*Message, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144
	resp := newMessage(pmes.GetType(), pmes.GetKey(), pmes.GetClusterLevel())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145 146

	// check if we have this value, to add ourselves as provider.
147
	log.Debug("handling GetProviders: '%s'", pmes.GetKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148 149
	dsk := u.Key(pmes.GetKey()).DsKey()
	has, err := dht.datastore.Has(dsk)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
150
	if err != nil && err != ds.ErrNotFound {
151
		log.Error("unexpected datastore error: %v\n", err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		has = false
	}

	// setup providers
	providers := dht.providers.GetProviders(u.Key(pmes.GetKey()))
	if has {
		providers = append(providers, dht.self)
	}

	// if we've got providers, send thos those.
	if providers != nil && len(providers) > 0 {
		resp.ProviderPeers = peersToPBPeers(providers)
	}

	// Also send closer peers.
167
	closer := dht.betterPeersToQuery(pmes, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
168
	if closer != nil {
169
		resp.CloserPeers = peersToPBPeers(closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
170 171 172 173 174 175 176
	}

	return resp, nil
}

type providerInfo struct {
	Creation time.Time
177
	Value    peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
178 179
}

180
func (dht *IpfsDHT) handleAddProvider(p peer.Peer, pmes *Message) (*Message, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181
	key := u.Key(pmes.GetKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
182

183
	log.Debug("%s adding %s as a provider for '%s'\n", dht.self, p, peer.ID(key))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
184

185
	// add provider should use the address given in the message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186 187
	for _, pb := range pmes.GetProviderPeers() {
		pid := peer.ID(pb.GetId())
188
		if pid.Equal(p.ID()) {
189 190 191 192 193 194

			addr, err := pb.Address()
			if err != nil {
				log.Error("provider %s error with address %s", p, *pb.Addr)
				continue
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
195 196

			log.Info("received provider %s %s for %s", p, addr, key)
197
			p.AddAddress(addr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
198 199 200 201
			dht.providers.AddProvider(key, p)

		} else {
			log.Error("handleAddProvider received provider %s from %s", pid, p)
202 203 204
		}
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
205
	return pmes, nil // send back same msg as confirmation.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
206 207 208 209 210 211 212
}

// Halt stops all communications from this peer and shut down
// TODO -- remove this in favor of context
func (dht *IpfsDHT) Halt() {
	dht.providers.Halt()
}