Browse Source

Commented Methods in synchronizer;
Added TODOs in synchronizer;
Update SyncDevices after storing sync records in the database;

Julien Clauter 9 years ago
parent
commit
08460f145a

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

@@ -280,7 +280,44 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         } finally {
             db.endTransaction();
         }
+
+        this.updateSyncDevicesMaxID(db);
+
         db.close(); // Closing database connection
+
+    }
+
+    /**
+     * Updates the sync devices max sync id.
+     */
+    public void updateSyncDevicesMaxID(SQLiteDatabase db){
+        HashMap<String, Long> deviceIDmap = new HashMap<String, Long>();
+
+        String selectQuery = "SELECT "+AttackEntry.COLUMN_NAME_DEVICE+ ","+ AttackEntry.COLUMN_NAME_SYNC_ID+" FROM " + AttackEntry.TABLE_NAME + " A "  + " GROUP BY " + AttackEntry.COLUMN_NAME_DEVICE + " HAVING " + AttackEntry.COLUMN_NAME_SYNC_ID + " = MAX( " + AttackEntry.COLUMN_NAME_SYNC_ID + " )";
+
+        {
+            Cursor cursor = db.rawQuery(selectQuery, null);
+            // looping through all rows and adding to list
+            if (cursor.moveToFirst()) {
+                do {
+                    String device_id = cursor.getString(0);
+                    long sync_id = cursor.getLong(1);
+
+                    deviceIDmap.put(device_id, sync_id);
+
+                } while (cursor.moveToNext());
+            }
+            cursor.close();
+        }
+        {
+            ArrayList<SyncDevice> allDevices = this.getSyncDevices();
+            for (SyncDevice device : allDevices){
+                long sync_id = deviceIDmap.get(device.getDeviceID());
+                device.setHighest_attack_id(sync_id);
+            }
+            this.updateSyncDevices(allDevices);
+        }
+
     }
 	
 	public void updateSyncAttackCounter(AttackRecord record){

+ 124 - 34
src/de/tudarmstadt/informatik/hostage/sync/Synchronizer.java

@@ -24,6 +24,31 @@ public class Synchronizer {
         this.dbh = dbh;
     }
 
+    /**
+     *
+     * Update Idea:
+     * PullDevices
+     * PullNetworks
+     * PullAttacks
+     *
+     * Commit Idea:
+     * PushDevice
+     * PushNetworks
+     * PushAttacks
+     *
+     */
+
+    /***
+     *
+     *  PULL METHODS
+     *
+     *
+     */
+
+    /**
+     * Get a list of missing network records from the other device.
+     * @param serverSocket server socket
+     */
     public void pullNetworks(ServerSocket serverSocket){
         ArrayList<String> myNetworks = this.dbh.getAllBSSIDS();
 
@@ -33,25 +58,34 @@ public class Synchronizer {
         }
     }
 
+    /**
+     * Get a list of missing devices from the other device.
+     * @param serverSocket server socket
+     */
     public void pullNewDevices(ServerSocket serverSocket){
         ArrayList<String> myDevices = this.dbh.getAllDevicesIds();
-        ArrayList<String> otherDeviceIds = this.getOtherDevies(serverSocket, myDevices);
+        ArrayList<String> otherDeviceIds = this.getOtherDevices(serverSocket, myDevices);
 
-        ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
+        if (otherDeviceIds != null){
+            ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
 
-        for (String deviceId : otherDeviceIds){
-            SyncDevice device = new SyncDevice();
-            device.setDeviceID(deviceId);
-            device.setHighest_attack_id(-1);
-            device.setLast_sync_timestamp(0);
-            otherDevices.add(device);
-        }
-
-        if (otherDevices != null && otherDevices.size() > 0)
-            this.dbh.updateSyncDevices(otherDevices);
+            for (String deviceId : otherDeviceIds){
+                SyncDevice device = new SyncDevice();
+                device.setDeviceID(deviceId);
+                device.setHighest_attack_id(-1);
+                device.setLast_sync_timestamp(0);
+                otherDevices.add(device);
+            }
 
+            if (otherDevices.size() > 0)
+                this.dbh.updateSyncDevices(otherDevices);
+        }
     }
 
+    /**
+     * Get all missing sync records from the other device.
+     * @param serverSocket server socket
+     */
     public void pullAttacks(ServerSocket serverSocket){
         ArrayList<SyncDevice> myState = this.dbh.getSyncDevices();
 
@@ -61,8 +95,54 @@ public class Synchronizer {
             this.dbh.insertSyncRecords(updates);
     }
 
-    public void pushNetworkInformation(ServerSocket serverSocket){
+    /**
+     * 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;
+    }
+
+
+    /**
+    *
+    *  PUSH METHODS
+    *
+    *
+    * */
+
+    /**
+     * Parses an array of device id strings and sends all missing sync devices ids.
+     * @param serverSocket server socket
+     */
+    public void pushDeviceIds(ServerSocket serverSocket){
+
+        // TODO parse other device ids from input stream here
         ArrayList<String> otherIDs = null;
 
 
@@ -72,8 +152,13 @@ public class Synchronizer {
         }
     }
 
+    /**
+     * Parses an array of sync devices and sends the all unsynced records.
+     * @param serverSocket server socket
+     */
     public void pushUnsyncedRecords(ServerSocket serverSocket){
 
+        // TODO parse other sync devices from input stream here
         ArrayList<SyncDevice> otherDeviceState = null;
 
         if (otherDeviceState != null){
@@ -82,7 +167,13 @@ public class Synchronizer {
         }
     }
 
-    public void pushDeviceIds(ServerSocket serverSocket){
+    /**
+     * Parses an array of bssid strings and sends all missing network records.
+     * @param serverSocket server socket
+     */
+    public void pushNetworkInformation(ServerSocket serverSocket){
+
+        // TODO parse other bssids from input stream
         ArrayList<String> otherBSSIDs = null;
 
 
@@ -92,32 +183,31 @@ public class Synchronizer {
         }
     }
 
-    private ArrayList<String> getOtherDevies(ServerSocket serverSocket, ArrayList<String> ownDevices){
-
-        return null;
-    }
-
-    private ArrayList<NetworkRecord> getOtherNetworkInformation(ServerSocket serverSocket, List<String> records){
-
-        return null;
-    }
-
-    private ArrayList<SyncRecord> getChanges(ServerSocket serverSocket, ArrayList<SyncDevice> ownState){
-
-        return null;
-    }
-
-
-    private void sendMissingNetworkRecords(ServerSocket serverSocket, ArrayList<NetworkRecord> m){
-
+    /**
+     * 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
     }