Commit 0f9bbfba authored by José Soares's avatar José Soares

Centrality functionalities improved. Optimized peer scan.

parent f5ddbde0
......@@ -62,6 +62,7 @@ public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvaila
*
* srcDevice.deviceName: SAMSUNG NEO
*/
Log.d("teste", "MAKING A SCAN ON TXTRECORD:");
Log.i("teste", fullDomainName + " " + txtRecordMap + " " + srcDevice.deviceName);
......@@ -98,9 +99,6 @@ public class ContextualManagerReceive implements WifiP2pListener.TxtRecordAvaila
peer.setCentrality(C);
//TODO peer.setLatitude(latitude);
//TODO peer.setLongitude(longitude);
if(peer.getIsConnected() == 0){
peer.setNumEncounters(peer.getNumEncounters()+1);
}
dataSource.updatePeer(peer, checkWeek("peers"));
Log.d("teste", "UPDATED PEER");
}
......
......@@ -56,9 +56,6 @@ public class ContextualManagerAvailability {
*/
private static ArrayList<Double> sumArrays(ArrayList<Double> usagePerHour1, ArrayList<Integer> usagePerHour2){
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY); // hour
ArrayList<Double> res = new ArrayList<>();
for (int i = 0; i < usagePerHour1.size(); i++) {
//-1 + -1 = -1
......@@ -67,15 +64,15 @@ public class ContextualManagerAvailability {
}
//-1+30 = 30 --> 30/currentHour
else if ( usagePerHour1.get(i) == -1 && usagePerHour2.get(i) != -1){
res.add((double) usagePerHour2.get(i) / hour);
res.add((double) usagePerHour2.get(i) / System.currentTimeMillis()/1000);
}
//30+-1 = 30 --> 30/currentHour
else if (usagePerHour2.get(i) == -1 && usagePerHour1.get(i) != -1){
res.add(usagePerHour1.get(i) / hour);
res.add(usagePerHour1.get(i) / System.currentTimeMillis()/1000);
}
//30+30 = 60 --> 60/currentHour
else{
res.add((usagePerHour1.get(i) + usagePerHour2.get(i)) / hour); //todo optimize without divide for the hour in the sumArrays method
res.add((usagePerHour1.get(i) + usagePerHour2.get(i)) / System.currentTimeMillis()/1000); //todo optimize without divide for the hour in the sumArrays method
}
}
return res;
......
......@@ -22,7 +22,6 @@ import static com.senception.contextualmanager.services.ContextualManagerService
*
* @file Contains ContextualManagerCentrality. Class that handles the inference of the centrality.
*/
// (C) Affinity/ContextualManagerCentrality
public class ContextualManagerCentrality {
//Formula: C(i) = 1/λ Σj A(j) * p(j)
......@@ -32,13 +31,18 @@ public class ContextualManagerCentrality {
encounter: encounter between a pair
average_encounter_duration: avg duration of an encounter between a pair
d(i,j):
A(j) = adjacency's matrix (vector of 1s e 0s) -- considered allways as 1. 1-connected | 0-disconnected
λ ∈ [0,1]
A(j): adjancency
d(i,j) ∈ [0,100]
*/
public static double calculateC(ContextualManagerDataSource dataSource){
//double degree;
int numEncounters;
double avgEncDur;
double distance = 1; // we're not taking into consideration the distance in this implementation -> only in the future
double lambda = 0.8; // should be the max of a lambda vector.
//Todo: Calculate C
//get peer list
......@@ -48,10 +52,14 @@ public class ContextualManagerCentrality {
peerList = dataSource.getAllPeers(checkWeek("peers"));
for (ContextualManagerAP peer : peerList) {
centrality += peer.getNumEncounters()*peer.getAvgEncounterDuration();
//Todo only if peer is connected (A->1)
numEncounters = peer.getNumEncounters();
avgEncDur = peer.getAvgEncounterDuration();
centrality += numEncounters*avgEncDur/distance;
}
Log.d("teste", "Sum = " + centrality);
}
centrality = centrality * (1/lambda);
return centrality;
}
}
......@@ -66,7 +66,16 @@ public class ContextualManagerWifiP2P extends BroadcastReceiver implements WifiP
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (manager != null) {
/*if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
//TODO
} else {
//TODO
}
//Log.d(TAG, "P2P state changed - " + state);
} else */if (manager != null) {
Log.d("teste", "making a scan");
manager.requestPeers(channel, new WifiP2pManager.PeerListListener() {
......
......@@ -16,6 +16,9 @@ import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceRequest;
import android.util.Log;
import com.senception.contextualmanager.pipelines.ContextualManagerWifiP2P;
import com.senception.contextualmanager.services.ContextualManagerService;
import java.util.Map;
......@@ -69,7 +72,8 @@ class WifiP2pSearcher implements DnsSdServiceResponseListener, DnsSdTxtRecordLis
* This method starts the discoveries
*/
void startDiscovery() {
discoverPeers();
ContextualManagerWifiP2P.discoverPeer(); // using the CM discovery
//discoverPeers();
discoverServices();
}
......@@ -91,7 +95,7 @@ class WifiP2pSearcher implements DnsSdServiceResponseListener, DnsSdTxtRecordLis
/**
* This method does the discovery peers
*/
private void discoverPeers() {
/*private void discoverPeers() {
mWifiP2pManager.discoverPeers(mChannel, new android.net.wifi.p2p.WifiP2pManager.ActionListener() {
@Override
......@@ -104,7 +108,7 @@ class WifiP2pSearcher implements DnsSdServiceResponseListener, DnsSdTxtRecordLis
Log.i(TAG, "Peers discovery failed, reason: " + reason);
}
});
}
}*/
/**
* This method does the discovery services
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment