From 74ca9025375300511ca1993d275bce1ca5c82055 Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Thu, 5 Mar 2020 16:20:48 -0500 Subject: [PATCH] use updated autonat events --- dht_test.go | 24 +++++------------------- go.mod | 2 +- go.sum | 2 ++ subscriber_notifee.go | 19 +++++++++++-------- 4 files changed, 19 insertions(+), 28 deletions(-) diff --git a/dht_test.go b/dht_test.go index 30a84bc82..e421bc409 100644 --- a/dht_test.go +++ b/dht_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/libp2p/go-libp2p-core/event" + "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/protocol" @@ -1782,22 +1783,7 @@ func TestDynamicModeSwitching(t *testing.T) { t.Fatal(err) } - var err error - var emitters struct { - evtLocalRoutabilityPrivate event.Emitter - evtLocalRoutabilityPublic event.Emitter - evtLocalRoutabilityUnknown event.Emitter - } - - emitters.evtLocalRoutabilityPublic, err = node.host.EventBus().Emitter(new(event.EvtLocalRoutabilityPublic)) - if err != nil { - t.Fatal(err) - } - emitters.evtLocalRoutabilityPrivate, err = node.host.EventBus().Emitter(new(event.EvtLocalRoutabilityPrivate)) - if err != nil { - t.Fatal(err) - } - emitters.evtLocalRoutabilityUnknown, err = node.host.EventBus().Emitter(new(event.EvtLocalRoutabilityUnknown)) + emitter, err := node.host.EventBus().Emitter(new(event.EvtLocalReachabilityChanged)) if err != nil { t.Fatal(err) } @@ -1818,17 +1804,17 @@ func TestDynamicModeSwitching(t *testing.T) { } } - emitters.evtLocalRoutabilityPrivate.Emit(event.EvtLocalRoutabilityPrivate{}) + emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPrivate}) time.Sleep(500 * time.Millisecond) assertDHTClient() - emitters.evtLocalRoutabilityPublic.Emit(event.EvtLocalRoutabilityPublic{}) + emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPublic}) time.Sleep(500 * time.Millisecond) assertDHTServer() - emitters.evtLocalRoutabilityUnknown.Emit(event.EvtLocalRoutabilityUnknown{}) + emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityUnknown}) time.Sleep(500 * time.Millisecond) assertDHTClient() diff --git a/go.mod b/go.mod index 7303a726e..7bb178364 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/jbenet/goprocess v0.1.3 github.com/libp2p/go-eventbus v0.1.0 github.com/libp2p/go-libp2p v0.5.3-0.20200221174525-7ba322244e0a - github.com/libp2p/go-libp2p-core v0.3.1 + github.com/libp2p/go-libp2p-core v0.3.2-0.20200305051524-d143201d83c2 github.com/libp2p/go-libp2p-kbucket v0.2.3 github.com/libp2p/go-libp2p-peerstore v0.1.4 github.com/libp2p/go-libp2p-record v0.1.2 diff --git a/go.sum b/go.sum index 985359ee9..9a642a88b 100644 --- a/go.sum +++ b/go.sum @@ -187,6 +187,8 @@ github.com/libp2p/go-libp2p-core v0.3.0 h1:F7PqduvrztDtFsAa/bcheQ3azmNo+Nq7m8hQY github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= github.com/libp2p/go-libp2p-core v0.3.1 h1:hEnSDjScfjYvPHoTgZhC4F62M8K1x1Oco/BY0RZ1N3s= github.com/libp2p/go-libp2p-core v0.3.1/go.mod h1:thvWy0hvaSBhnVBaW37BvzgVV68OUhgJJLAa6almrII= +github.com/libp2p/go-libp2p-core v0.3.2-0.20200305051524-d143201d83c2 h1:0CAWwNiZCcDRAwoPdr3u2WTjJvHeKMw7unB557IQPH0= +github.com/libp2p/go-libp2p-core v0.3.2-0.20200305051524-d143201d83c2/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= github.com/libp2p/go-libp2p-crypto v0.1.0 h1:k9MFy+o2zGDNGsaoZl0MA3iZ75qXxr9OOoAZF+sD5OQ= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= github.com/libp2p/go-libp2p-discovery v0.2.0 h1:1p3YSOq7VsgaL+xVHPi8XAmtGyas6D2J6rWBEfz/aiY= diff --git a/subscriber_notifee.go b/subscriber_notifee.go index 056793698..0b204aebf 100644 --- a/subscriber_notifee.go +++ b/subscriber_notifee.go @@ -41,11 +41,7 @@ func newSubscriberNotifiee(dht *IpfsDHT) (*subscriberNotifee, error) { // only register for events if the DHT is operating in ModeAuto var routabilitySub event.Subscription if dht.auto { - routabilitySub, err = dht.Host().EventBus().Subscribe([]interface{}{ - &event.EvtLocalRoutabilityPublic{}, - &event.EvtLocalRoutabilityPrivate{}, - &event.EvtLocalRoutabilityUnknown{}, - }, bufSize) + routabilitySub, err = dht.Host().EventBus().Subscribe(new(event.EvtLocalReachabilityChanged), bufSize) if err != nil { return nil, fmt.Errorf("dht not subscribed to local routability events; err: %s", err) } @@ -165,10 +161,17 @@ func handlePeerProtocolsUpdatedEvent(dht *IpfsDHT, evt interface{}) { func handleRoutabilityChanges(dht *IpfsDHT, evt interface{}) { var target mode - switch evt.(type) { - case event.EvtLocalRoutabilityPrivate, event.EvtLocalRoutabilityUnknown: + // something has gone really wrong if we get an event for another type + e, ok := evt.(event.EvtLocalReachabilityChanged) + if !ok { + logger.Errorf("got wrong type from subscription: %T", evt) + return + } + + switch e.Reachability { + case network.ReachabilityPrivate, network.ReachabilityUnknown: target = modeClient - case event.EvtLocalRoutabilityPublic: + case network.ReachabilityPublic: target = modeServer }