mirror of
https://github.com/fluencelabs/go-libp2p-kad-dht
synced 2025-04-25 06:42:13 +00:00
clean up dht construction code
This commit is contained in:
parent
d09b2e73d0
commit
e015b2a2f6
38
dht.go
38
dht.go
@ -67,43 +67,45 @@ type IpfsDHT struct {
|
|||||||
|
|
||||||
// NewDHT creates a new DHT object with the given peer as the 'local' host
|
// NewDHT creates a new DHT object with the given peer as the 'local' host
|
||||||
func NewDHT(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT {
|
func NewDHT(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT {
|
||||||
dht := new(IpfsDHT)
|
dht := makeDHT(ctx, h, dstore)
|
||||||
dht.datastore = dstore
|
|
||||||
dht.self = h.ID()
|
|
||||||
dht.peerstore = h.Peerstore()
|
|
||||||
dht.host = h
|
|
||||||
|
|
||||||
// register for network notifs.
|
// register for network notifs.
|
||||||
dht.host.Network().Notify((*netNotifiee)(dht))
|
dht.host.Network().Notify((*netNotifiee)(dht))
|
||||||
|
|
||||||
dht.proc = goprocess.WithTeardown(func() error {
|
dht.proc = goprocessctx.WithContextAndTeardown(ctx, func() error {
|
||||||
// remove ourselves from network notifs.
|
// remove ourselves from network notifs.
|
||||||
dht.host.Network().StopNotify((*netNotifiee)(dht))
|
dht.host.Network().StopNotify((*netNotifiee)(dht))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
dht.strmap = make(map[peer.ID]*messageSender)
|
dht.proc.AddChild(dht.providers.Process())
|
||||||
dht.ctx = ctx
|
|
||||||
|
|
||||||
h.SetStreamHandler(ProtocolDHT, dht.handleNewStream)
|
h.SetStreamHandler(ProtocolDHT, dht.handleNewStream)
|
||||||
h.SetStreamHandler(ProtocolDHTOld, dht.handleNewStream)
|
h.SetStreamHandler(ProtocolDHTOld, dht.handleNewStream)
|
||||||
|
|
||||||
dht.providers = providers.NewProviderManager(dht.ctx, dht.self, dstore)
|
|
||||||
dht.proc.AddChild(dht.providers.Process())
|
|
||||||
goprocessctx.CloseAfterContext(dht.proc, ctx)
|
|
||||||
|
|
||||||
dht.routingTable = kb.NewRoutingTable(20, kb.ConvertPeerID(dht.self), time.Minute, dht.peerstore)
|
|
||||||
dht.birth = time.Now()
|
|
||||||
|
|
||||||
dht.Validator = make(record.Validator)
|
|
||||||
dht.Validator["pk"] = record.PublicKeyValidator
|
dht.Validator["pk"] = record.PublicKeyValidator
|
||||||
|
|
||||||
dht.Selector = make(record.Selector)
|
|
||||||
dht.Selector["pk"] = record.PublicKeySelector
|
dht.Selector["pk"] = record.PublicKeySelector
|
||||||
|
|
||||||
return dht
|
return dht
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT {
|
||||||
|
return &IpfsDHT{
|
||||||
|
datastore: dstore,
|
||||||
|
self: h.ID(),
|
||||||
|
peerstore: h.Peerstore(),
|
||||||
|
host: h,
|
||||||
|
strmap: make(map[peer.ID]*messageSender),
|
||||||
|
ctx: ctx,
|
||||||
|
providers: providers.NewProviderManager(ctx, h.ID(), dstore),
|
||||||
|
birth: time.Now(),
|
||||||
|
routingTable: kb.NewRoutingTable(KValue, kb.ConvertPeerID(h.ID()), time.Minute, h.Peerstore()),
|
||||||
|
|
||||||
|
Validator: make(record.Validator),
|
||||||
|
Selector: make(record.Selector),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// putValueToPeer stores the given key/value pair at the peer 'p'
|
// putValueToPeer stores the given key/value pair at the peer 'p'
|
||||||
func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID,
|
func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID,
|
||||||
key key.Key, rec *recpb.Record) error {
|
key key.Key, rec *recpb.Record) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user