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

import (
	"errors"
	"fmt"
	"time"

	peer "github.com/jbenet/go-ipfs/peer"
9
	pb "github.com/jbenet/go-ipfs/routing/dht/pb"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
	u "github.com/jbenet/go-ipfs/util"

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

15 16
var CloserPeerCount = 4

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

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

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

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

	// 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.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
	log.Debugf("%s handleGetValue looking into ds", dht.self)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
	dskey := u.Key(pmes.GetKey()).DsKey()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
	iVal, err := dht.datastore.Get(dskey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
	log.Debugf("%s handleGetValue looking into ds GOT %v", dht.self, iVal)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57

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

62 63 64 65
	// 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
66
	if err == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
		log.Debugf("%s handleGetValue success!", dht.self)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69 70 71 72 73 74 75 76 77 78 79

		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 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
		log.Debugf("handleGetValue returning %d provider[s]", len(provs))
81
		resp.ProviderPeers = pb.PeersToPBPeers(provs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82 83 84
	}

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

	return resp, nil
}

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

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

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

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

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

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

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

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

	// check if we have this value, to add ourselves as provider.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148
	log.Debugf("handling GetProviders: '%s'", pmes.GetKey())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149 150
	dsk := u.Key(pmes.GetKey()).DsKey()
	has, err := dht.datastore.Has(dsk)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151
	if err != nil && err != ds.ErrNotFound {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
152
		log.Errorf("unexpected datastore error: %v\n", err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153 154 155 156 157 158 159 160 161 162 163
		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 {
164
		resp.ProviderPeers = pb.PeersToPBPeers(providers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165 166 167
	}

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

	return resp, nil
}

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

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

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

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

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

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

		} else {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202
			log.Errorf("handleAddProvider received provider %s from %s", pid, p)
203 204 205
		}
	}

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

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