Browse Source

Merge branch 'merge_v1' of https://git.tk.informatik.tu-darmstadt.de/scm-ssi-hostage-v3 into merge_v1

Alexander Brakowski 9 years ago
parent
commit
f19759545d

+ 1 - 0
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -948,6 +948,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         }
         SyncInfo syncInfo = new SyncInfo();
         syncInfo.deviceMap  = deviceMap;
+        syncInfo.bssids = this.getAllBSSIDS();
         return syncInfo;
     }
 	

+ 80 - 19
src/de/tudarmstadt/informatik/hostage/sync/Synchronizer.java

@@ -6,9 +6,12 @@ package de.tudarmstadt.informatik.hostage.sync;
 
 import java.net.ServerSocket;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Set;
 
 import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
+import de.tudarmstadt.informatik.hostage.logging.SyncData;
 import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
 import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
 import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
@@ -63,6 +66,59 @@ public class Synchronizer {
     }
 
 
+    /**
+     * Updates from the given sync data.
+     * @param syncData {@link de.tudarmstadt.informatik.hostage.logging.SyncData}
+     */
+    public void updateFromSyncData(SyncData syncData){
+        this.updateNewNetworks(syncData.networkRecords);
+        this.updateNewAttacks(syncData.syncRecords);
+    }
+
+    /**
+     * Updates the devices list by the given {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}.
+     * @param sInfo  {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
+     * @return  {@link de.tudarmstadt.informatik.hostage.logging.SyncData}
+     */
+    public SyncData getSyncData(SyncInfo sInfo){
+        SyncData sData = new SyncData();
+        ArrayList<String> bssids = sInfo.bssids;
+        HashMap<String, Long> deviceMap = sInfo.deviceMap;
+        ArrayList<String> deviceIds = null;
+
+        if (deviceMap != null && deviceMap.keySet() != null){
+            deviceIds = this.stringSetToArrayList(deviceMap.keySet());
+
+            this.updateNewDevices(deviceIds);
+        }
+
+        ArrayList<NetworkRecord> nets= new ArrayList<NetworkRecord>();
+        if (bssids != null){
+            nets = this.getMissingNetworkInformation(bssids);
+        }
+
+        ArrayList<SyncRecord> sRec = this.getUnsyncedRecords(sInfo);
+
+        sData.syncRecords = sRec;
+        sData.networkRecords = nets;
+
+        return sData;
+    }
+
+    /**
+     * Converts a string set in an array list
+     * @param s the set to convert
+     * @return array list
+     */
+    private ArrayList<String> stringSetToArrayList(Set<String> s){
+        ArrayList<String> list = new ArrayList<String>();
+
+        for (String string : s){
+            list.add(string);
+        }
+        return list;
+    }
+
     /***
      *
      *  PULL METHODS
@@ -84,12 +140,14 @@ public class Synchronizer {
      * Updates new inserted devices.
      * @param otherDeviceIds ArrayList<String> other device ids
      */
-    public void updateNewDevices(ArrayList<String> otherDeviceIds){
+    private void updateNewDevices(ArrayList<String> otherDeviceIds){
 
         if (otherDeviceIds != null){
             ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
+            ArrayList<String> ownDevicesds = this.dbh.getAllDevicesIds();
 
-            for (String deviceId : otherDeviceIds){
+            ArrayList<String> n = this.diffArray(otherDeviceIds, ownDevicesds);
+            for (String deviceId : n){
                 SyncDevice device = new SyncDevice();
                 device.setDeviceID(deviceId);
                 device.setHighest_attack_id(-1);
@@ -102,11 +160,28 @@ public class Synchronizer {
         }
     }
 
+    /**
+     * Diffs to array by their content.
+     * @param origin the original array
+     * @param toRemove all strings to remove
+     * @return diffed array list
+     */
+    private ArrayList<String> diffArray(ArrayList<String> origin, ArrayList<String> toRemove){
+        ArrayList<String> n = new ArrayList<String>();
+
+        for (String s : origin){
+            if (!toRemove.contains(s)){
+                n.add(s);
+            }
+        }
+        return n;
+    }
+
     /**
      * Get all missing sync records from the other device.
      * @param updates list of new attack information
      */
-    public void updateNewAttacks(ArrayList<SyncRecord> updates){
+    private void updateNewAttacks(ArrayList<SyncRecord> updates){
         if (updates != null && updates.size() > 0)
             this.dbh.insertSyncRecords(updates);
     }
@@ -120,26 +195,12 @@ public class Synchronizer {
     *
     * */
 
-    /**
-     * Parses an array of device id strings and sends all missing sync devices ids.
-     * @param otherIDs other device ids
-     * @return missing device ids
-     */
-    public ArrayList<String> pushDeviceIds(ArrayList<String> otherIDs){
-
-        if (otherIDs != null){
-            return this.dbh.getMissingDeviceIds(otherIDs);
-            //this.sendMissingDeviceIds(serverSocket, missingIds);
-        }
-        return new ArrayList<String>();
-    }
-
     /**
      * Returns list of unsynced records.
      * @param si other states {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
      * @return unsynced sync records
      */
-    public ArrayList<SyncRecord> pushUnsyncedRecords(SyncInfo si){
+    public ArrayList<SyncRecord> getUnsyncedRecords(SyncInfo si){
 
         if (si.deviceMap != null){
             return this.dbh.getUnsyncedAttacksFor(si.deviceMap, false);
@@ -152,7 +213,7 @@ public class Synchronizer {
      * @param otherBSSIDs list of other bssids
      * @return array list of network records to push.
      */
-    public ArrayList<NetworkRecord> pushNetworkInformation(ArrayList<String> otherBSSIDs){
+    private ArrayList<NetworkRecord> getMissingNetworkInformation(ArrayList<String> otherBSSIDs){
 
         if (otherBSSIDs != null){
             return this.dbh.getMissingNetworkRecords(otherBSSIDs);