package de.tudarmstadt.informatik.hostage.sync; /** * Created by Julien on 08.12.2014. */ 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; import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper; import de.tudarmstadt.informatik.hostage.system.Device; public class Synchronizer { private HostageDBOpenHelper dbh; public Synchronizer(HostageDBOpenHelper dbh){ super(); this.dbh = dbh; } /** * Returns own state of all registered devices. * @return ArrayList */ public SyncInfo getSyncInfo(){ return this.dbh.getOwnState(); } /** * 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 bssids = sInfo.bssids; HashMap deviceMap = sInfo.deviceMap; ArrayList deviceIds = null; if (deviceMap != null && deviceMap.keySet() != null){ deviceIds = this.stringSetToArrayList(deviceMap.keySet()); this.updateNewDevices(deviceIds); } ArrayList nets= new ArrayList(); if (bssids != null){ nets = this.getMissingNetworkInformation(bssids); } ArrayList 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 stringSetToArrayList(Set s){ ArrayList list = new ArrayList(); for (String string : s){ list.add(string); } return list; } /*** * * PULL METHODS * * */ /** * Inserts a list of networks * @param others all missing networks */ private void updateNewNetworks(ArrayList others){ if (others != null && others.size() > 0){ this.dbh.updateNetworkInformation(others); } } /** * Updates new inserted devices. * @param otherDeviceIds ArrayList other device ids */ private void updateNewDevices(ArrayList otherDeviceIds){ if (otherDeviceIds != null){ ArrayList otherDevices = new ArrayList(); ArrayList ownDevicesds = this.dbh.getAllDevicesIds(); ArrayList n = this.diffArray(otherDeviceIds, ownDevicesds); for (String deviceId : n){ 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); } } /** * Diffs to array by their content. * @param origin the original array * @param toRemove all strings to remove * @return diffed array list */ private ArrayList diffArray(ArrayList origin, ArrayList toRemove){ ArrayList n = new ArrayList(); 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 */ private void updateNewAttacks(ArrayList updates){ if (updates != null && updates.size() > 0) this.dbh.insertSyncRecords(updates); } /** * * PUSH METHODS * * * */ /** * Returns list of unsynced records. * @param si other states {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo} * @return unsynced sync records */ private ArrayList getUnsyncedRecords(SyncInfo si){ if (si.deviceMap != null){ return this.dbh.getUnsyncedAttacksFor(si.deviceMap, true); } return new ArrayList(); } /** * Returns list of missing network records. * @param otherBSSIDs list of other bssids * @return array list of network records to push. */ private ArrayList getMissingNetworkInformation(ArrayList otherBSSIDs){ if (otherBSSIDs != null){ return this.dbh.getMissingNetworkRecords(otherBSSIDs); } return new ArrayList(); } }