2014-08-18 20:38:44 -07:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2014-12-16 08:55:46 -08:00
|
|
|
ctxgroup "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
|
2014-12-29 05:43:56 -08:00
|
|
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
2014-08-19 22:05:49 -07:00
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
2014-10-25 07:12:01 -07:00
|
|
|
|
2015-02-23 16:51:09 +01:00
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
2014-08-18 20:38:44 -07:00
|
|
|
)
|
|
|
|
|
2015-02-13 08:29:10 +00:00
|
|
|
type providerInfo struct {
|
|
|
|
Creation time.Time
|
|
|
|
Value peer.ID
|
|
|
|
}
|
|
|
|
|
2014-08-18 20:38:44 -07:00
|
|
|
type ProviderManager struct {
|
|
|
|
providers map[u.Key][]*providerInfo
|
2014-09-04 20:32:46 +00:00
|
|
|
local map[u.Key]struct{}
|
|
|
|
lpeer peer.ID
|
|
|
|
getlocal chan chan []u.Key
|
2014-08-19 22:05:49 -07:00
|
|
|
newprovs chan *addProv
|
|
|
|
getprovs chan *getProv
|
2014-09-04 20:32:46 +00:00
|
|
|
period time.Duration
|
2014-12-16 08:55:46 -08:00
|
|
|
ctxgroup.ContextGroup
|
2014-08-18 20:38:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type addProv struct {
|
2014-08-19 22:05:49 -07:00
|
|
|
k u.Key
|
2014-12-19 12:19:56 -08:00
|
|
|
val peer.ID
|
2014-08-18 20:38:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type getProv struct {
|
2014-08-19 22:05:49 -07:00
|
|
|
k u.Key
|
2014-12-19 12:19:56 -08:00
|
|
|
resp chan []peer.ID
|
2014-08-18 20:38:44 -07:00
|
|
|
}
|
|
|
|
|
2014-10-25 07:12:01 -07:00
|
|
|
func NewProviderManager(ctx context.Context, local peer.ID) *ProviderManager {
|
2014-08-18 20:38:44 -07:00
|
|
|
pm := new(ProviderManager)
|
|
|
|
pm.getprovs = make(chan *getProv)
|
|
|
|
pm.newprovs = make(chan *addProv)
|
|
|
|
pm.providers = make(map[u.Key][]*providerInfo)
|
2014-09-04 20:32:46 +00:00
|
|
|
pm.getlocal = make(chan chan []u.Key)
|
|
|
|
pm.local = make(map[u.Key]struct{})
|
2014-12-16 08:55:46 -08:00
|
|
|
pm.ContextGroup = ctxgroup.WithContext(ctx)
|
2014-10-25 07:12:01 -07:00
|
|
|
|
|
|
|
pm.Children().Add(1)
|
2014-08-18 20:38:44 -07:00
|
|
|
go pm.run()
|
2014-10-25 07:12:01 -07:00
|
|
|
|
2014-08-18 20:38:44 -07:00
|
|
|
return pm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *ProviderManager) run() {
|
2014-10-25 07:12:01 -07:00
|
|
|
defer pm.Children().Done()
|
|
|
|
|
2014-08-18 20:38:44 -07:00
|
|
|
tick := time.NewTicker(time.Hour)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case np := <-pm.newprovs:
|
2014-12-19 12:19:56 -08:00
|
|
|
if np.val == pm.lpeer {
|
2014-09-04 20:32:46 +00:00
|
|
|
pm.local[np.k] = struct{}{}
|
|
|
|
}
|
2014-08-18 20:38:44 -07:00
|
|
|
pi := new(providerInfo)
|
|
|
|
pi.Creation = time.Now()
|
|
|
|
pi.Value = np.val
|
|
|
|
arr := pm.providers[np.k]
|
|
|
|
pm.providers[np.k] = append(arr, pi)
|
2014-10-25 07:12:01 -07:00
|
|
|
|
2014-08-18 20:38:44 -07:00
|
|
|
case gp := <-pm.getprovs:
|
2014-12-19 12:19:56 -08:00
|
|
|
var parr []peer.ID
|
2014-08-18 20:38:44 -07:00
|
|
|
provs := pm.providers[gp.k]
|
|
|
|
for _, p := range provs {
|
|
|
|
parr = append(parr, p.Value)
|
|
|
|
}
|
|
|
|
gp.resp <- parr
|
2014-10-25 07:12:01 -07:00
|
|
|
|
2014-09-04 20:32:46 +00:00
|
|
|
case lc := <-pm.getlocal:
|
|
|
|
var keys []u.Key
|
|
|
|
for k, _ := range pm.local {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
lc <- keys
|
2014-10-25 07:12:01 -07:00
|
|
|
|
2014-08-18 20:38:44 -07:00
|
|
|
case <-tick.C:
|
|
|
|
for k, provs := range pm.providers {
|
|
|
|
var filtered []*providerInfo
|
|
|
|
for _, p := range provs {
|
2014-08-19 22:05:49 -07:00
|
|
|
if time.Now().Sub(p.Creation) < time.Hour*24 {
|
2014-08-18 20:38:44 -07:00
|
|
|
filtered = append(filtered, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pm.providers[k] = filtered
|
|
|
|
}
|
2014-10-25 07:12:01 -07:00
|
|
|
|
|
|
|
case <-pm.Closing():
|
2014-08-18 20:38:44 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
func (pm *ProviderManager) AddProvider(k u.Key, val peer.ID) {
|
2014-08-18 20:38:44 -07:00
|
|
|
pm.newprovs <- &addProv{
|
2014-08-19 22:05:49 -07:00
|
|
|
k: k,
|
2014-08-18 20:38:44 -07:00
|
|
|
val: val,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
func (pm *ProviderManager) GetProviders(ctx context.Context, k u.Key) []peer.ID {
|
2014-12-02 00:55:07 -08:00
|
|
|
gp := &getProv{
|
|
|
|
k: k,
|
2014-12-19 12:19:56 -08:00
|
|
|
resp: make(chan []peer.ID, 1), // buffered to prevent sender from blocking
|
2014-12-02 00:55:07 -08:00
|
|
|
}
|
2014-12-02 00:40:50 -08:00
|
|
|
select {
|
2014-12-05 22:54:16 -08:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
2014-12-02 00:40:50 -08:00
|
|
|
case pm.getprovs <- gp:
|
2014-12-05 22:54:16 -08:00
|
|
|
}
|
|
|
|
select {
|
2014-12-02 00:40:50 -08:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
2014-12-05 22:54:16 -08:00
|
|
|
case peers := <-gp.resp:
|
|
|
|
return peers
|
2014-12-02 00:40:50 -08:00
|
|
|
}
|
2014-08-18 20:38:44 -07:00
|
|
|
}
|
2014-08-23 22:21:20 -07:00
|
|
|
|
2014-09-04 20:32:46 +00:00
|
|
|
func (pm *ProviderManager) GetLocal() []u.Key {
|
|
|
|
resp := make(chan []u.Key)
|
|
|
|
pm.getlocal <- resp
|
|
|
|
return <-resp
|
|
|
|
}
|