Просмотр исходного кода

SyncInfo in synchronizer;
Refactored synchronizer;

Julien Clauter 10 лет назад
Родитель
Сommit
368abac3a4

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

@@ -17,6 +17,7 @@ import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
 import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
 import de.tudarmstadt.informatik.hostage.logging.Record;
 import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
+import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
 import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
 import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
 import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
@@ -933,6 +934,22 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 
         db.close();
     }
+
+    /**
+     * Returns the own state containing all registered devices ids and their max sync_id
+     * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
+     */
+    public SyncInfo getOwnState(){
+        ArrayList<SyncDevice> devices = this.getSyncDevices();
+
+        HashMap<String, Long> deviceMap = new HashMap<String, Long>();
+        for (SyncDevice device : devices){
+            deviceMap.put(device.getDeviceID(), device.getHighest_attack_id());
+        }
+        SyncInfo syncInfo = new SyncInfo();
+        syncInfo.deviceMap  = deviceMap;
+        return syncInfo;
+    }
 	
 	/**
 	 * Returns a HashMap of all devices that were previously synchronized with.
@@ -1729,6 +1746,43 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         return recordList;
     }
 
+
+    /**
+     * Returns all missing / newly inserted and updated {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s.
+     * @param oldDeviceMap map with device id and max sync_id for the device
+     * @param includeMissing boolean
+     * @return array of {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}s
+     */
+    public ArrayList<SyncDevice> getUpdatedDevicesFor(HashMap<String, Long> oldDeviceMap, boolean includeMissing){
+
+        ArrayList<SyncDevice> recordList = new ArrayList<SyncDevice>();
+        String selectQuery = "SELECT * FROM " + SyncDeviceEntry.TABLE_NAME;
+        SQLiteDatabase db = this.getReadableDatabase();
+        Cursor cursor = db.rawQuery(selectQuery, null);
+
+        // looping through all rows and adding to list
+        if (cursor.moveToFirst()) {
+            do {
+                SyncDevice record = createSyncDevice(cursor);
+                // Adding record to list
+                if (oldDeviceMap.containsKey(record.getDeviceID())){
+                    Long oldSyncId = oldDeviceMap.get(record.getDeviceID());
+                    if (oldSyncId < record.getHighest_attack_id()){
+                        recordList.add(record);
+                    }
+                } else {
+                    if (includeMissing)
+                        recordList.add(record);
+                }
+            } while (cursor.moveToNext());
+        }
+        cursor.close();
+
+        // return record list
+        db.close();
+        return recordList;
+    }
+
     /**
      * Returns all device ids.
      * @return list of all device ids.
@@ -1821,6 +1875,49 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
     }
 
 
+    /**
+     * Returns all new {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s for the given devices (including all missing devices).
+     * @param deviceMap map of device id and max sync_id of it
+     * @param includeMissingDevices boolean
+     * @return list of {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}s
+     */
+    public ArrayList<SyncRecord> getUnsyncedAttacksFor(HashMap<String,Long> deviceMap, boolean includeMissingDevices){
+
+        ArrayList<SyncDevice> updatedDevices = this.getUpdatedDevicesFor(deviceMap, includeMissingDevices);
+
+        ArrayList<SyncRecord> recordList = new ArrayList<SyncRecord>();
+
+        SQLiteDatabase db = this.getReadableDatabase();
+
+        for (SyncDevice sDevice : updatedDevices){
+            String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " NATURAL JOIN " + PacketEntry.TABLE_NAME + " P "
+                    + " WHERE "
+                    +" ( "
+                    + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
+                    + " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + sDevice.getDeviceID() + "'"
+                    + " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + sDevice.getHighest_attack_id()
+                    + " ) "
+                    //+ " GROUP BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_DEVICE
+                    + " ORDER BY " + AttackEntry.TABLE_NAME + "." + AttackEntry.COLUMN_NAME_SYNC_ID + " DESC ";
+            Cursor cursor = db.rawQuery(selectQuery, null);
+
+            // looping through all rows and adding to list
+            if (cursor != null){
+                if (cursor.moveToFirst()) {
+                    do {
+                        SyncRecord record = createSyncRecord(cursor);
+                        recordList.add(record);
+                    } while (cursor.moveToNext());
+                }
+                cursor.close();
+            }
+        }
+
+        // return record list
+        db.close();
+        return recordList;
+    }
+
     /**
      * Attacks per BSSID
      * @param filter (LogFilter) query filter

+ 52 - 103
src/de/tudarmstadt/informatik/hostage/sync/Synchronizer.java

@@ -10,6 +10,7 @@ import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
 import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
+import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
 import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
 import de.tudarmstadt.informatik.hostage.system.Device;
@@ -38,6 +39,30 @@ public class Synchronizer {
      *
      */
 
+    /**
+     * Returns all recorded BSSIDs.
+     * @return array list of bssids
+     */
+    public ArrayList<String> getAllBSSIDs(){
+        return this.dbh.getAllBSSIDS();
+    }
+
+    /**
+     * Returns all devices ids.
+     * @return  ArrayList<String>
+     */
+    public ArrayList<String> getAllDeviceIDs(){
+        return this.dbh.getAllDevicesIds();
+    }
+    /**
+     * Returns own state of all registered devices.
+     * @return  ArrayList<SyncDevice>
+     */
+    public SyncInfo getOwnState(){
+        return this.dbh.getOwnState();
+    }
+
+
     /***
      *
      *  PULL METHODS
@@ -46,25 +71,20 @@ public class Synchronizer {
      */
 
     /**
-     * Get a list of missing network records from the other device.
-     * @param serverSocket server socket
+     * Inserts a list of networks
+     * @param others all missing networks
      */
-    public void pullNetworks(ServerSocket serverSocket){
-        ArrayList<String> myNetworks = this.dbh.getAllBSSIDS();
-
-        ArrayList<NetworkRecord> others = this.getOtherNetworkInformation(serverSocket, myNetworks);
+    public void updateNewNetworks(ArrayList<NetworkRecord> others){
         if (others != null && others.size() > 0){
             this.dbh.updateNetworkInformation(others);
         }
     }
 
     /**
-     * Get a list of missing devices from the other device.
-     * @param serverSocket server socket
+     * Updates new inserted devices.
+     * @param otherDeviceIds ArrayList<String> other device ids
      */
-    public void pullNewDevices(ServerSocket serverSocket){
-        ArrayList<String> myDevices = this.dbh.getAllDevicesIds();
-        ArrayList<String> otherDeviceIds = this.getOtherDevices(serverSocket, myDevices);
+    public void updateNewDevices(ArrayList<String> otherDeviceIds){
 
         if (otherDeviceIds != null){
             ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
@@ -84,49 +104,13 @@ public class Synchronizer {
 
     /**
      * Get all missing sync records from the other device.
-     * @param serverSocket server socket
+     * @param updates list of new attack information
      */
-    public void pullAttacks(ServerSocket serverSocket){
-        ArrayList<SyncDevice> myState = this.dbh.getSyncDevices();
-
-        ArrayList<SyncRecord> updates = this.getChanges(serverSocket, myState);
-
+    public void updateNewAttacks(ArrayList<SyncRecord> updates){
         if (updates != null && updates.size() > 0)
             this.dbh.insertSyncRecords(updates);
     }
 
-    /**
-     * Sends own devices ids and parses a list of missing device ids.
-     * @param serverSocket server socket
-     * @param ownDevices own device ids
-     * @return list of missing device ids
-     */
-    private ArrayList<String> getOtherDevices(ServerSocket serverSocket, ArrayList<String> ownDevices){
-        // TODO parse other device ids from in put stream here
-        return null;
-    }
-
-    /**
-     * Sends all network bssids to the  other devices and parses all missing network records.
-     * @param serverSocket server socket
-     * @param records network bssids
-     * @return list of missing networks
-     */
-    private ArrayList<NetworkRecord> getOtherNetworkInformation(ServerSocket serverSocket, List<String> records){
-        // TODO Parse missing network records from input stream here
-        return null;
-    }
-
-    /**
-     * Sends the own state to the other device and parses the answer.
-     * @param serverSocket server socket
-     * @param ownState our state
-     * @return list of unsynced sync records
-     */
-    private ArrayList<SyncRecord> getChanges(ServerSocket serverSocket, ArrayList<SyncDevice> ownState){
-        // TODO Parse unsynced sync records from in put stream here
-        return null;
-    }
 
 
     /**
@@ -138,77 +122,42 @@ public class Synchronizer {
 
     /**
      * Parses an array of device id strings and sends all missing sync devices ids.
-     * @param serverSocket server socket
+     * @param otherIDs other device ids
+     * @return missing device ids
      */
-    public void pushDeviceIds(ServerSocket serverSocket){
-
-        // TODO parse other device ids from input stream here
-        ArrayList<String> otherIDs = null;
-
+    public ArrayList<String> pushDeviceIds(ArrayList<String> otherIDs){
 
         if (otherIDs != null){
-            ArrayList<String> missingIds = this.dbh.getMissingDeviceIds(otherIDs);
-            this.sendMissingDeviceIds(serverSocket, missingIds);
+            return this.dbh.getMissingDeviceIds(otherIDs);
+            //this.sendMissingDeviceIds(serverSocket, missingIds);
         }
+        return new ArrayList<String>();
     }
 
     /**
-     * Parses an array of sync devices and sends the all unsynced records.
-     * @param serverSocket server socket
+     * Returns list of unsynced records.
+     * @param si other states {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
+     * @return unsynced sync records
      */
-    public void pushUnsyncedRecords(ServerSocket serverSocket){
+    public ArrayList<SyncRecord> pushUnsyncedRecords(SyncInfo si){
 
-        // TODO parse other sync devices from input stream here
-        ArrayList<SyncDevice> otherDeviceState = null;
-
-        if (otherDeviceState != null){
-            ArrayList<SyncRecord> unsynced = this.dbh.getUnsyncedAttacksFor(otherDeviceState, false);
-            this.sendUnsyncedRecords(serverSocket, unsynced);
+        if (si.deviceMap != null){
+            return this.dbh.getUnsyncedAttacksFor(si.deviceMap, false);
         }
+        return new ArrayList<SyncRecord>();
     }
 
     /**
-     * Parses an array of bssid strings and sends all missing network records.
-     * @param serverSocket server socket
+     * Returns list of missing network records.
+     * @param otherBSSIDs list of other bssids
+     * @return array list of network records to push.
      */
-    public void pushNetworkInformation(ServerSocket serverSocket){
-
-        // TODO parse other bssids from input stream
-        ArrayList<String> otherBSSIDs = null;
-
+    public ArrayList<NetworkRecord> pushNetworkInformation(ArrayList<String> otherBSSIDs){
 
         if (otherBSSIDs != null){
-            ArrayList<NetworkRecord> missings = this.dbh.getMissingNetworkRecords(otherBSSIDs);
-            this.sendMissingNetworkRecords(serverSocket, missings);
+            return this.dbh.getMissingNetworkRecords(otherBSSIDs);
         }
+        return new ArrayList<NetworkRecord>();
     }
 
-    /**
-     * Sends all missing network records over an object output stream.
-     * @param serverSocket server socket
-     * @param n missing network information
-     */
-    private void sendMissingNetworkRecords(ServerSocket serverSocket, ArrayList<NetworkRecord> n){
-        // TODO make output stream here
-    }
-
-    /**
-     * Sends all missing device ids over an object output stream
-     * @param serverSocket server socket
-     * @param missingIds all missing device ids
-     */
-    private void sendMissingDeviceIds(ServerSocket serverSocket, ArrayList<String> missingIds){
-        // TODO make output stream here
-    }
-
-    /**
-     * Sends all unsynced sync records over an object output stream
-     * @param serverSocket server socket
-     * @param unsynced array of unsynced sync records
-     */
-    private void sendUnsyncedRecords(ServerSocket serverSocket, ArrayList<SyncRecord> unsynced){
-        // TODO make output stream here
-    }
-
-
 }