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

import (
	"errors"
	"fmt"
	"time"

Jeromy's avatar
Jeromy committed
8
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
9

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

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

Jeromy's avatar
Jeromy committed
17
// The number of closer peers to send on requests.
18 19
var CloserPeerCount = 4

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

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

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

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

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

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

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

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

77 78 79 80 81 82 83 84
		rec := new(pb.Record)
		err := proto.Unmarshal(byts, rec)
		if err != nil {
			log.Error("Failed to unmarshal dht record from datastore")
			return nil, err
		}

		resp.Record = rec
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85 86 87 88 89
	}

	// 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
90
		log.Debugf("handleGetValue returning %d provider[s]", len(provs))
91
		resp.ProviderPeers = pb.PeersToPBPeers(provs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92 93 94
	}

	// Find closest peer on given cluster to desired key and reply with that info
95
	closer := dht.betterPeersToQuery(pmes, CloserPeerCount)
96
	if closer != nil {
97
		for _, p := range closer {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
			log.Debugf("handleGetValue returning closer peer: '%s'", p)
Jeromy's avatar
Jeromy committed
99
			if len(p.Addresses()) < 1 {
Juan Batiz-Benet's avatar
notes  
Juan Batiz-Benet committed
100
				log.Critical("no addresses on peer being sent!")
Jeromy's avatar
Jeromy committed
101
			}
102
		}
103
		resp.CloserPeers = pb.PeersToPBPeers(closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104 105 106 107 108 109
	}

	return resp, nil
}

// Store a value in this peer local storage
110
func (dht *IpfsDHT) handlePutValue(p peer.Peer, pmes *pb.Message) (*pb.Message, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111 112
	dht.dslock.Lock()
	defer dht.dslock.Unlock()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113
	dskey := u.Key(pmes.GetKey()).DsKey()
114 115 116

	err := dht.verifyRecord(pmes.GetRecord())
	if err != nil {
Jeromy's avatar
Jeromy committed
117
		fmt.Println(u.Key(pmes.GetRecord().GetAuthor()))
118 119 120 121 122 123 124 125 126 127 128
		log.Error("Bad dht record in put request")
		return nil, err
	}

	data, err := proto.Marshal(pmes.GetRecord())
	if err != nil {
		return nil, err
	}

	err = dht.datastore.Put(dskey, data)
	log.Debugf("%s handlePutValue %v\n", dht.self, dskey)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129
	return pmes, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130 131
}

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

137 138
func (dht *IpfsDHT) handleFindPeer(p peer.Peer, pmes *pb.Message) (*pb.Message, error) {
	resp := pb.NewMessage(pmes.GetType(), "", pmes.GetClusterLevel())
139
	var closest []peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141

	// if looking for self... special case where we send it on CloserPeers.
142 143
	if peer.ID(pmes.GetKey()).Equal(dht.self.ID()) {
		closest = []peer.Peer{dht.self}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144
	} else {
145
		closest = dht.betterPeersToQuery(pmes, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146 147 148
	}

	if closest == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149
		log.Errorf("handleFindPeer: could not find anything.")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
150 151 152
		return resp, nil
	}

153
	var withAddresses []peer.Peer
154
	for _, p := range closest {
155
		if len(p.Addresses()) > 0 {
156 157
			withAddresses = append(withAddresses, p)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
158 159
	}

160
	for _, p := range withAddresses {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161
		log.Debugf("handleFindPeer: sending back '%s'", p)
162
	}
163
	resp.CloserPeers = pb.PeersToPBPeers(withAddresses)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164 165 166
	return resp, nil
}

167 168
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
169 170

	// check if we have this value, to add ourselves as provider.
Jeromy's avatar
Jeromy committed
171
	log.Debugf("handling GetProviders: '%s'", u.Key(pmes.GetKey()))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172 173
	dsk := u.Key(pmes.GetKey()).DsKey()
	has, err := dht.datastore.Has(dsk)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
174
	if err != nil && err != ds.ErrNotFound {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
175
		log.Errorf("unexpected datastore error: %v\n", err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
176 177 178 179 180 181 182 183 184 185 186
		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 {
187
		resp.ProviderPeers = pb.PeersToPBPeers(providers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
188 189 190
	}

	// Also send closer peers.
191
	closer := dht.betterPeersToQuery(pmes, CloserPeerCount)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
192
	if closer != nil {
193
		resp.CloserPeers = pb.PeersToPBPeers(closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194 195 196 197 198 199 200
	}

	return resp, nil
}

type providerInfo struct {
	Creation time.Time
201
	Value    peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202 203
}

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
207
	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
208

209
	// add provider should use the address given in the message
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
210 211
	for _, pb := range pmes.GetProviderPeers() {
		pid := peer.ID(pb.GetId())
212
		if pid.Equal(p.ID()) {
213 214 215

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
220
			log.Infof("received provider %s %s for %s", p, addr, key)
221
			p.AddAddress(addr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
222 223 224
			dht.providers.AddProvider(key, p)

		} else {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225
			log.Errorf("handleAddProvider received provider %s from %s", pid, p)
226 227 228
		}
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229
	return pmes, nil // send back same msg as confirmation.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230
}