1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-16 20:15:08 +08:00
Files
kubo/routing/mock/centralized_server.go
Ho-Sheng Hsiao bf22aeec0a Reorged imports from jbenet/go-ipfs to ipfs/go-ipfs
- Modified Godeps/Godeps.json by hand
- [TEST] Updated welcome docs hash to sharness
- [TEST] Updated contact doc
- [TEST] disabled breaking test (t0080-repo refs local)
2015-03-31 12:52:25 -07:00

87 lines
1.8 KiB
Go

package mockrouting
import (
"math/rand"
"sync"
"time"
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
peer "github.com/ipfs/go-ipfs/p2p/peer"
u "github.com/ipfs/go-ipfs/util"
"github.com/ipfs/go-ipfs/util/testutil"
)
// server is the mockrouting.Client's private interface to the routing server
type server interface {
Announce(peer.PeerInfo, u.Key) error
Providers(u.Key) []peer.PeerInfo
Server
}
// s is an implementation of the private server interface
type s struct {
delayConf DelayConfig
lock sync.RWMutex
providers map[u.Key]map[peer.ID]providerRecord
}
type providerRecord struct {
Peer peer.PeerInfo
Created time.Time
}
func (rs *s) Announce(p peer.PeerInfo, k u.Key) error {
rs.lock.Lock()
defer rs.lock.Unlock()
_, ok := rs.providers[k]
if !ok {
rs.providers[k] = make(map[peer.ID]providerRecord)
}
rs.providers[k][p.ID] = providerRecord{
Created: time.Now(),
Peer: p,
}
return nil
}
func (rs *s) Providers(k u.Key) []peer.PeerInfo {
rs.delayConf.Query.Wait() // before locking
rs.lock.RLock()
defer rs.lock.RUnlock()
var ret []peer.PeerInfo
records, ok := rs.providers[k]
if !ok {
return ret
}
for _, r := range records {
if time.Now().Sub(r.Created) > rs.delayConf.ValueVisibility.Get() {
ret = append(ret, r.Peer)
}
}
for i := range ret {
j := rand.Intn(i + 1)
ret[i], ret[j] = ret[j], ret[i]
}
return ret
}
func (rs *s) Client(p testutil.Identity) Client {
return rs.ClientWithDatastore(context.Background(), p, ds.NewMapDatastore())
}
func (rs *s) ClientWithDatastore(_ context.Context, p testutil.Identity, datastore ds.Datastore) Client {
return &client{
peer: p,
datastore: ds.NewMapDatastore(),
server: rs,
}
}