Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

#86 #87

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

#86 #87

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public interface Listener {
public static final int DEFAULT_SCAN_MODE = ScanSettings.SCAN_MODE_BALANCED;
public static final long DEFAULT_SCAN_REPORT_DELAY_IN_FOREGROUND_IN_MILLISECONDS = 500;
public static final long DEFAULT_SCAN_REPORT_DELAY_IN_BACKGROUND_IN_MILLISECONDS = 1000;
public static final long DEFAULT_PEER_PROPERTIES_UPDATE_PERIOD_IN_MILLISECONDS = 2000;

// Keys for shared preferences
private static final String KEY_AUTOMATE_BLUETOOTH_MAC_ADDRESS_RESOLUTION = "automate_bluetooth_mac_address_resolution";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

import android.os.CountDownTimer;
import android.util.Log;

import org.thaliproject.p2p.btconnectorlib.DiscoveryManagerSettings;
import org.thaliproject.p2p.btconnectorlib.PeerProperties;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -15,26 +17,29 @@

/**
* A model for discovered peers.
*
* <p>
* Peer expiration is based on a time elapsed since we last saw the peer.
*/
public class PeerModel {
public interface Listener {
/**
* Called when a new peer is added to the model.
*
* @param peerProperties The properties of the added peer.
*/
void onPeerAdded(PeerProperties peerProperties);

/**
* Called when an existing peer is updated.
*
* @param peerProperties The properties of the updated peer.
*/
void onPeerUpdated(PeerProperties peerProperties);

/**
* Called when an existing peer expired and was removed from the model.
* Peer expiration is based on a time elapsed since we last saw the peer.
*
* @param peerProperties The properties of the expired and removed peer.
*/
void onPeerExpiredAndRemoved(PeerProperties peerProperties);
Expand All @@ -48,6 +53,7 @@ public interface Listener {

/**
* Constructor.
*
* @param settings The Discovery manager settings.
*/
public PeerModel(Listener listener, DiscoveryManagerSettings settings) {
Expand All @@ -57,6 +63,7 @@ public PeerModel(Listener listener, DiscoveryManagerSettings settings) {

/**
* Adds the given listener.
*
* @param listener The listener to add.
*/
public void addListener(Listener listener) {
Expand All @@ -68,6 +75,7 @@ public void addListener(Listener listener) {

/**
* Removes the given listener.
*
* @param listener The listener to remove.
*/
public void removeListener(Listener listener) {
Expand All @@ -92,9 +100,9 @@ public void clear() {

/**
* Recreates the check expired peers timer.
*
* <p>
* Peer expiration is based on a time elapsed since we last saw the peer.
*
* <p>
* This method takes no argument, since it is expected that the time has been updated to the
* settings, where the timer will retrieve it when reconstructed.
*/
Expand All @@ -107,6 +115,7 @@ public void onPeerExpirationTimeChanged() {

/**
* Tries to find a discovered peer with the given Bluetooth MAC address.
*
* @param bluetoothMacAddress The Bluetooth MAC address of a peer to find.
* @return A peer properties instance if found, null if not.
*/
Expand All @@ -127,6 +136,7 @@ public synchronized PeerProperties getDiscoveredPeerByBluetoothMacAddress(final

/**
* Tries to find a discovered peer with the given device address.
*
* @param deviceAddress The device address of a peer to find.
* @return A peer properties instance if found, null if not.
*/
Expand All @@ -147,6 +157,7 @@ public synchronized PeerProperties getDiscoveredPeerByDeviceAddress(final String

/**
* Removes the given peer properties from the collection.
*
* @param peerPropertiesToRemove The peer properties to remove.
* @return The found and removed peer properties or null, if not found.
*/
Expand Down Expand Up @@ -174,16 +185,35 @@ public synchronized PeerProperties removePeer(PeerProperties peerPropertiesToRem
return oldPeerProperties;
}

/**
* return peer properties if they are not expired or null
*
* @param peerPropertiesToAddOrUpdate The peer properties .
* @return The found not expired peer properties or null otherwise.
*/
public PeerProperties getNotExpiredPeerProperties(PeerProperties peerPropertiesToAddOrUpdate) {
PeerProperties oldPeerProperties = null;
Timestamp lastUpdatePeerPropertiesTime = mDiscoveredPeers.get(peerPropertiesToAddOrUpdate);
long expireTime = System.currentTimeMillis() -
DiscoveryManagerSettings.DEFAULT_PEER_PROPERTIES_UPDATE_PERIOD_IN_MILLISECONDS;
if (lastUpdatePeerPropertiesTime != null && expireTime > lastUpdatePeerPropertiesTime.getTime()) {
removePeer(peerPropertiesToAddOrUpdate);
} else {
oldPeerProperties = removePeer(peerPropertiesToAddOrUpdate);
}
return oldPeerProperties;
}

/**
* Adds or updates the given peer properties to the collection.
*
* @param peerPropertiesToAddOrUpdate The peer properties to add/update.
*/
public void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate) {
synchronized (this) {
if (peerPropertiesToAddOrUpdate != null) {
//Log.v(TAG, "addOrUpdateDiscoveredPeer: " + peerProperties.toString());
PeerProperties oldPeerProperties = removePeer(peerPropertiesToAddOrUpdate);

PeerProperties oldPeerProperties = getNotExpiredPeerProperties(peerPropertiesToAddOrUpdate);
if (oldPeerProperties != null) {
// This one was already in the list

Expand All @@ -207,14 +237,15 @@ public void addOrUpdateDiscoveredPeer(PeerProperties peerPropertiesToAddOrUpdate
for (Listener listener : mListeners) {
listener.onPeerAdded(peerPropertiesToAddOrUpdate);
}

}

mDiscoveredPeers.put(peerPropertiesToAddOrUpdate, new Timestamp(new Date().getTime()));

Log.v(TAG, "addOrUpdateDiscoveredPeer: "
+ ((oldPeerProperties == null)
? ("New peer, " + peerPropertiesToAddOrUpdate.toString() + ", added")
: ("Timestamp of peer " + peerPropertiesToAddOrUpdate.toString() + " updated"))
? ("New peer, " + peerPropertiesToAddOrUpdate.toString() + ", added")
: ("Timestamp of peer " + peerPropertiesToAddOrUpdate.toString() + " updated"))
+ " - the peer count is " + mDiscoveredPeers.size());

if (mCheckExpiredPeersTimer == null) {
Expand All @@ -235,7 +266,7 @@ public synchronized void checkListForExpiredPeers() {

// Find and copy expired peers to a separate list
while (iterator.hasNext()) {
HashMap.Entry entry = (HashMap.Entry)iterator.next();
HashMap.Entry entry = (HashMap.Entry) iterator.next();
PeerProperties entryPeerProperties = (PeerProperties) entry.getKey();
Timestamp entryTimestamp = (Timestamp) entry.getValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ public void testAddOrUpdateDiscoveredPeer_Add() throws Exception {
public void testAddOrUpdateDiscoveredPeer_Update() throws Exception {
// prepare a peer's list
HashMap<PeerProperties, Timestamp> mDiscoveredPeers = new HashMap<>();
mDiscoveredPeers.put(mMockPeerProperties, new Timestamp(1000L));
//should use real time (not 1000L) because in method used value of System.currentTimeMillis()
long initialTime = System.currentTimeMillis();
mDiscoveredPeers.put(mMockPeerProperties, new Timestamp(initialTime));

Field mDiscoveredPeersField = mPeerModel.getClass().getDeclaredField("mDiscoveredPeers");
mDiscoveredPeersField.setAccessible(true);
Expand Down Expand Up @@ -314,7 +316,8 @@ public void testAddOrUpdateDiscoveredPeer_Update() throws Exception {
public void testAddOrUpdateDiscoveredPeer_Update2() throws Exception {
// prepare a peer's list
HashMap<PeerProperties, Timestamp> mDiscoveredPeers = new HashMap<>();
mDiscoveredPeers.put(mMockPeerProperties, new Timestamp(1000L));
long initialTime = System.currentTimeMillis();
mDiscoveredPeers.put(mMockPeerProperties, new Timestamp(initialTime));

Field mDiscoveredPeersField = mPeerModel.getClass().getDeclaredField("mDiscoveredPeers");
mDiscoveredPeersField.setAccessible(true);
Expand All @@ -339,6 +342,37 @@ public void testAddOrUpdateDiscoveredPeer_Update2() throws Exception {
assertThat("The peer is added", peers.size(), is(1));
}

@Test
public void testAddOrUpdateDiscoveredPeer_UpdateTimeslot() throws Exception {
HashMap<PeerProperties, Timestamp> mDiscoveredPeers = new HashMap<>();
long initialTime = System.currentTimeMillis();
long expiredTime = initialTime -
DiscoveryManagerSettings.DEFAULT_PEER_PROPERTIES_UPDATE_PERIOD_IN_MILLISECONDS - 1;
mDiscoveredPeers.put(mMockPeerProperties, new Timestamp(expiredTime));

Field mDiscoveredPeersField = mPeerModel.getClass().getDeclaredField("mDiscoveredPeers");
mDiscoveredPeersField.setAccessible(true);
mDiscoveredPeersField.set(mPeerModel, mDiscoveredPeers);

doNothing().when(mMockListener).onPeerUpdated(isA(PeerProperties.class));
when(mMockDiscoveryManagerSettings.getPeerExpiration())
.thenReturn(DiscoveryManagerSettings.DEFAULT_PEER_EXPIRATION_IN_MILLISECONDS);
when(mMockPeerProperties.hasMoreInformation(isA(PeerProperties.class))).thenReturn(false);

mPeerModel.addOrUpdateDiscoveredPeer(mMockPeerProperties);

verify(mMockListener, never()).onPeerUpdated(isA(PeerProperties.class));
verify(mMockListener, times(1)).onPeerAdded(isA(PeerProperties.class));
verify(mMockDiscoveryManagerSettings, times(1)).getPeerExpiration();
Field discoveredPeersField = mPeerModel.getClass().getDeclaredField("mDiscoveredPeers");
discoveredPeersField.setAccessible(true);
@SuppressWarnings("unchecked")
HashMap<PeerProperties, Timestamp> peers
= (HashMap<PeerProperties, Timestamp>) discoveredPeersField.get(mPeerModel);

assertThat("The peer is added", peers.size(), is(1));
}

@Test
public void testCheckListForExpiredPeers() throws Exception {
HashMap<PeerProperties, Timestamp> mDiscoveredPeers = new HashMap<>();
Expand Down
4 changes: 2 additions & 2 deletions BtConnectorLib/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 10 15:27:10 PDT 2013
#Thu Sep 01 13:00:17 MSK 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip
2 changes: 1 addition & 1 deletion BtConnectorLib/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include ':btconnectorlib2'
gradle.ext.version = "0.3.2";
gradle.ext.version = "0.3.4_alpha";
gradle.ext.group = "org.thaliproject.p2p.btconnectorlib"