diff --git a/server.go b/server.go index ca95859..b6b0c43 100644 --- a/server.go +++ b/server.go @@ -29,7 +29,7 @@ func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentE var dhtRouting routing.Routing if runAcceleratedDHTClient { - wrappedDHT, err := newWrappedStandardAndAcceleratedDHTClient(ctx, h) + wrappedDHT, err := newBundledDHT(ctx, h) if err != nil { return err } diff --git a/server_dht.go b/server_dht.go index 462810a..70cf291 100644 --- a/server_dht.go +++ b/server_dht.go @@ -13,18 +13,18 @@ import ( "github.com/libp2p/go-libp2p/core/routing" ) -type wrappedStandardAndAcceleratedDHTClient struct { - standard *dht.IpfsDHT - accelerated *fullrt.FullRT +type bundledDHT struct { + standard *dht.IpfsDHT + fullRT *fullrt.FullRT } -func newWrappedStandardAndAcceleratedDHTClient(ctx context.Context, h host.Host) (routing.Routing, error) { +func newBundledDHT(ctx context.Context, h host.Host) (routing.Routing, error) { standardDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeClient), dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...)) if err != nil { return nil, err } - acceleratedDHT, err := fullrt.NewFullRT(h, "/ipfs", + fullRT, err := fullrt.NewFullRT(h, "/ipfs", fullrt.DHTOption( dht.BucketSize(20), dht.Validator(record.NamespacedValidator{ @@ -38,54 +38,43 @@ func newWrappedStandardAndAcceleratedDHTClient(ctx context.Context, h host.Host) return nil, err } - return &wrappedStandardAndAcceleratedDHTClient{ - standard: standardDHT, - accelerated: acceleratedDHT, + return &bundledDHT{ + standard: standardDHT, + fullRT: fullRT, }, nil } -func (w *wrappedStandardAndAcceleratedDHTClient) Provide(ctx context.Context, c cid.Cid, b bool) error { - if w.accelerated.Ready() { - return w.accelerated.Provide(ctx, c, b) +func (b *bundledDHT) getDHT() routing.Routing { + if b.fullRT.Ready() { + return b.fullRT } - return w.standard.Provide(ctx, c, b) + return b.standard } -func (w *wrappedStandardAndAcceleratedDHTClient) FindProvidersAsync(ctx context.Context, c cid.Cid, i int) <-chan peer.AddrInfo { - if w.accelerated.Ready() { - return w.accelerated.FindProvidersAsync(ctx, c, i) - } - return w.standard.FindProvidersAsync(ctx, c, i) +func (b *bundledDHT) Provide(ctx context.Context, c cid.Cid, brdcst bool) error { + return b.getDHT().Provide(ctx, c, brdcst) } -func (w *wrappedStandardAndAcceleratedDHTClient) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) { - if w.accelerated.Ready() { - return w.accelerated.FindPeer(ctx, p) - } - return w.standard.FindPeer(ctx, p) +func (b *bundledDHT) FindProvidersAsync(ctx context.Context, c cid.Cid, i int) <-chan peer.AddrInfo { + return b.getDHT().FindProvidersAsync(ctx, c, i) } -func (w *wrappedStandardAndAcceleratedDHTClient) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error { - if w.accelerated.Ready() { - return w.accelerated.PutValue(ctx, key, value, opts...) - } - return w.standard.PutValue(ctx, key, value, opts...) +func (b *bundledDHT) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error) { + return b.getDHT().FindPeer(ctx, id) } -func (w *wrappedStandardAndAcceleratedDHTClient) GetValue(ctx context.Context, s string, opts ...routing.Option) ([]byte, error) { - if w.accelerated.Ready() { - return w.accelerated.GetValue(ctx, s, opts...) - } - return w.standard.GetValue(ctx, s, opts...) +func (b *bundledDHT) PutValue(ctx context.Context, k string, v []byte, option ...routing.Option) error { + return b.getDHT().PutValue(ctx, k, v, option...) } -func (w *wrappedStandardAndAcceleratedDHTClient) SearchValue(ctx context.Context, s string, opts ...routing.Option) (<-chan []byte, error) { - if w.accelerated.Ready() { - return w.accelerated.SearchValue(ctx, s, opts...) - } - return w.standard.SearchValue(ctx, s, opts...) +func (b *bundledDHT) GetValue(ctx context.Context, s string, option ...routing.Option) ([]byte, error) { + return b.getDHT().GetValue(ctx, s, option...) +} + +func (b *bundledDHT) SearchValue(ctx context.Context, s string, option ...routing.Option) (<-chan []byte, error) { + return b.getDHT().SearchValue(ctx, s, option...) } -func (w *wrappedStandardAndAcceleratedDHTClient) Bootstrap(ctx context.Context) error { - return w.standard.Bootstrap(ctx) +func (b *bundledDHT) Bootstrap(ctx context.Context) error { + return b.standard.Bootstrap(ctx) }