Skip to content

Commit

Permalink
dont engage d2d discovery if nodes already connected (#20)
Browse files Browse the repository at this point in the history
* dont engage d2d discovery if nodes already connected

* add some matrix tests

* version update
  • Loading branch information
asabya authored Oct 7, 2021
1 parent 655b263 commit de118df
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .version
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mobile=0.0.11
cli=0.0.2
mobile=0.0.12
cli=0.0.3
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Change Log
All notable changes to this project will be documented in this file.

## ## [v0.0.12] - 2021-10-07

### Added

- [655b263](https://github.com/datahop/ipfs-lite/commit/655b263) Auto Disconnect after content replication for D2D discovery

### Changed

- [84b7b42](https://github.com/datahop/ipfs-lite/commit/84b7b42) Do not initiate D2D Discovery in nodes are pre-connected

## [v0.0.11] - 2021-09-22

### Added
Expand Down
4 changes: 4 additions & 0 deletions internal/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ func (mKeeper *MatrixKeeper) NodeConnectionFailed(address string) {
nodeMatrix.ConnectionFailureCount++

nodeMatrix.BLEDiscoveredAt = 0
nodeMatrix.WifiConnectedAt = 0
nodeMatrix.RSSI = 0
nodeMatrix.Speed = 0
nodeMatrix.Frequency = 0
}

// NodeDisconnected updates connectivity disconnection time info with another node
Expand Down
64 changes: 64 additions & 0 deletions internal/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ds "github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
leveldb "github.com/ipfs/go-ds-leveldb"
"github.com/libp2p/go-libp2p-core/peer"
)

var root = filepath.Join("./test", "root1")
Expand Down Expand Up @@ -117,6 +118,69 @@ func TestMatrixKeeperFlushWithData(t *testing.T) {
}
}

func TestMatrixKeeperNodeMatrixOps(t *testing.T) {
<-time.After(time.Second)
defer removeRepo(t)
mKeeper := NewMatrixKeeper(initDatastore(t))
if mKeeper.NodeMatrix == nil {
t.Fatal("NodeMatrix keeper should not be null")
}
defer mKeeper.db.Close()
address := "discoveredNodeOne"
mKeeper.BLEDiscovered(address)
if mKeeper.GetNodeStat(address).BLEDiscoveredAt == 0 {
t.Fatal("BLEDiscoveredAt not updated")
}
mKeeper.WifiConnected(address, 5, 5, 5)
if mKeeper.GetNodeStat(address).WifiConnectedAt == 0 {
t.Fatal("WifiConnectedAt not updated")
}
mKeeper.NodeConnectionFailed(address)
if mKeeper.GetNodeStat(address).ConnectionFailureCount != 1 {
t.Fatal("ConnectionFailureCount not updated")
}

mKeeper.BLEDiscovered(address)
mKeeper.WifiConnected(address, 5, 5, 5)
mKeeper.NodeConnected(address)
if mKeeper.GetNodeStat(address).ConnectionSuccessCount != 1 {
t.Fatal("ConnectionFailureCount not updated")
}

mKeeper.NodeDisconnected(address)
if len(mKeeper.GetNodeStat(address).ConnectionHistory) != 1 {
t.Fatal("ConnectionHistory not updated")
}
}

func TestMatrixKeeperContentMatrixOps(t *testing.T) {
<-time.After(time.Second)
defer removeRepo(t)
mKeeper := NewMatrixKeeper(initDatastore(t))
if mKeeper.NodeMatrix == nil {
t.Fatal("NodeMatrix keeper should not be null")
}
defer mKeeper.db.Close()
hash := "Hash"
address := "discoveredNodeOne"
tag := "Tag"
var size int64 = 256
mKeeper.ContentDownloadStarted(tag, hash, size)
cs := mKeeper.GetContentStat(hash)
if cs.Tag != tag && cs.DownloadStartedAt == 0 {
t.Fatal("ContentDownloadStart failed")
}
mKeeper.ContentDownloadFinished(hash)
cs = mKeeper.GetContentStat(hash)
if cs.AvgSpeed == 0 && cs.DownloadFinishedAt == 0 {
t.Fatal("ContentDownloadFinished failed")
}
mKeeper.ContentAddProvider(hash, peer.ID(address))
if len(mKeeper.GetContentStat(hash).ProvidedBy) != 1 {
t.Fatal("ContentAddProvider not updated")
}
}

func TestMatrixKeeperTicker(t *testing.T) {
<-time.After(time.Second)
defer removeRepo(t)
Expand Down
14 changes: 14 additions & 0 deletions mobile/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

p2pnet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)
Expand Down Expand Up @@ -188,6 +189,13 @@ func (b *discoveryService) DiscoveryPeerDifferentStatus(device string, topic str
log.Errorf("failed parsing peerinfo %s", err.Error())
return
}

conn := hop.peer.Host.Network().Connectedness(peerInfo.ID)
if conn == p2pnet.Connected {
log.Debug("Peer already connected")
return
}

hop.peer.Repo.Matrix().BLEDiscovered(peerInfo.ID.String())
hop.wifiCon.Connect(network, pass, "192.168.49.2", peerInfo.ID.String())
b.handleConnectionRequest = func() {
Expand All @@ -204,6 +212,12 @@ func (b *discoveryService) AdvertiserPeerDifferentStatus(topic string, value []b
log.Debugf("advertising new peer device different status : %s", string(value))
log.Debugf("peerinfo: %s", id)

conn := hop.peer.Host.Network().Connectedness(peer.ID(id))
if conn == p2pnet.Connected {
log.Debug("Peer already connected")
return
}

hop.peer.Repo.Matrix().BLEDiscovered(id)
b.discovery.Stop()
b.wifiHS.Start()
Expand Down

0 comments on commit de118df

Please sign in to comment.