Browse Source

repair attacks without device id

Julien Clauter 9 years ago
parent
commit
84cef21507

+ 56 - 25
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -304,6 +304,56 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
         this.updateSyncDevices(devices);
 	}
 
+    /**
+     * Updates the own devices and connects attack records without a device to the own device
+     */
+    synchronized public void updateUntrackedAttacks(){
+        SQLiteDatabase db = this.getWritableDatabase();
+        String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A WHERE " + AttackEntry.COLUMN_NAME_DEVICE + " IS NULL ORDER BY " + AttackEntry.COLUMN_NAME_ATTACK_ID + " DESC";
+        Cursor cursor = db.rawQuery(selectQuery, null);
+
+        SyncDevice ownDevice = currentDevice();
+
+        long highestID = ownDevice.getHighest_attack_id();
+
+        ArrayList<AttackRecord> records = new ArrayList<AttackRecord>();
+
+        if (cursor.moveToFirst()) {
+            do {
+                AttackRecord record = this.createAttackRecord(cursor);
+                record.setDevice(ownDevice.getDeviceID());
+                highestID = (highestID > record.getAttack_id())? highestID : record.getAttack_id();
+                records.add(record);
+            } while (cursor.moveToNext());
+        }
+        cursor.close();
+        ownDevice.setHighest_attack_id(highestID);
+
+
+        // UPDATE RECORDS
+        if (records.size() > 0){
+            db.beginTransaction();
+            try {
+                for (AttackRecord record : records){
+                    this.insertAttackRecordWithOnConflict(record,db);
+                }
+                db.setTransactionSuccessful();
+            } finally {
+                db.endTransaction();
+            }
+        }
+
+        if (highestID != ownDevice.getHighest_attack_id()){
+            // THERE WERE ATTACKS WITHOUT A DEVICE ID
+            ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
+            devices.add(ownDevice);
+            updateSyncDevices(devices);
+        }
+
+
+        db.close();
+    }
+
     /**
      * Adds a given {@link AttackRecord}s to the database.
      *
@@ -421,31 +471,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 		syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, currentDevice.getDeviceID());
 		syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
 		syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, record.getAttack_id());
-		//String query = "SELECT  * FROM " + SyncInfoEntry.TABLE_NAME +
-		//			  " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
-		//			   "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
-		//Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
-		//long attackCount = 0;
-		//long portscanCount = 0;
-		//if (cursor.moveToFirst()){
-		//	attackCount = cursor.getLong(2);
-		//	portscanCount = cursor.getLong(3);
-		//}
-		
-		//if("PORTSCAN".equals(record.getProtocol())){
-		//	portscanCount++;
-		//}else { attackCount++; }
-			
-		//Log.i("DBHelper", "Update number of attack: " + attackCount);
-		
-		//ContentValues synInfoValues = new ContentValues();
-		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
-		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
-		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
-		//synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
-
-		// Inserting Rows
-		//db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
+
 		db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
 		db.close(); // Closing database connection
 	}
@@ -1064,6 +1090,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
      * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
      */
     public synchronized SyncInfo getOwnState(){
+        updateUntrackedAttacks();
+
         ArrayList<SyncDevice> devices = this.getSyncDevices();
 
         HashMap<String, Long> deviceMap = new HashMap<String, Long>();
@@ -2004,6 +2032,9 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 
         SQLiteDatabase db = this.getReadableDatabase();
 
+        SyncDevice currentDevice = currentDevice();
+        String own_device_id = currentDevice.getDeviceID();
+
         for (SyncDevice sDevice : updatedDevices){
             String deviceID = sDevice.getDeviceID();
             Long maxID = deviceMap.get(deviceID);

+ 15 - 3
src/de/tudarmstadt/informatik/hostage/sync/Synchronizer.java

@@ -4,6 +4,8 @@ package de.tudarmstadt.informatik.hostage.sync;
  * Created by Julien on 08.12.2014.
  */
 
+import android.util.Log;
+
 import java.net.ServerSocket;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -104,6 +106,7 @@ public class Synchronizer {
      */
     private void updateNewNetworks(ArrayList<NetworkRecord> others){
         if (others != null && others.size() > 0){
+            Log.i("DEBUG_Sync", "Updating Network Data Objects: " + others.size());
             this.dbh.updateNetworkInformation(others);
         }
     }
@@ -118,6 +121,8 @@ public class Synchronizer {
             ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
             ArrayList<String> ownDevicesds = this.dbh.getAllDevicesIds();
 
+            Log.i("DEBUG_Sync", "Updating Devices: " + otherDevices.size());
+
             ArrayList<String> n = this.diffArray(otherDeviceIds, ownDevicesds);
             for (String deviceId : n){
                 SyncDevice device = new SyncDevice();
@@ -154,8 +159,10 @@ public class Synchronizer {
      * @param updates list of new attack information
      */
     private void updateNewAttacks(ArrayList<SyncRecord> updates){
-        if (updates != null && updates.size() > 0)
+        if (updates != null && updates.size() > 0) {
+            Log.i("DEBUG_Sync", "Updating Attack Objects: " + updates.size());
             this.dbh.insertSyncRecords(updates);
+        }
     }
 
 
@@ -175,7 +182,9 @@ public class Synchronizer {
     private ArrayList<SyncRecord> getUnsyncedRecords(SyncInfo si){
 
         if (si.deviceMap != null){
-            return this.dbh.getUnsyncedAttacksFor(si.deviceMap, true);
+            ArrayList<SyncRecord> records = this.dbh.getUnsyncedAttacksFor(si.deviceMap, true);
+            Log.i("DEBUG_Sync", "Sending Attack Objects: " + records.size());
+            return records;
         }
         return new ArrayList<SyncRecord>();
     }
@@ -188,7 +197,10 @@ public class Synchronizer {
     private ArrayList<NetworkRecord> getMissingNetworkInformation(ArrayList<String> otherBSSIDs){
 
         if (otherBSSIDs != null){
-            return this.dbh.getMissingNetworkRecords(otherBSSIDs);
+            ArrayList<NetworkRecord> records = this.dbh.getMissingNetworkRecords(otherBSSIDs);
+            Log.i("DEBUG_Sync", "Sending Network Objects: " + records.size());
+            return records;
+
         }
         return new ArrayList<NetworkRecord>();
     }