package de.tudarmstadt.informatik.hostage.sync; /** * Created by Julien on 08.12.2014. */ import java.net.ServerSocket; import java.util.ArrayList; 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; public class Synchronizer { private HostageDBOpenHelper dbh; public Synchronizer(HostageDBOpenHelper dbh){ super(); this.dbh = dbh; } /** * * Update Idea: * PullDevices * PullNetworks * PullAttacks * * Commit Idea: * PushDevice * PushNetworks * PushAttacks * */ /** * Returns all recorded BSSIDs. * @return array list of bssids */ public ArrayList getAllBSSIDs(){ return this.dbh.getAllBSSIDS(); } /** * Returns all devices ids. * @return ArrayList */ public ArrayList getAllDeviceIDs(){ return this.dbh.getAllDevicesIds(); } /** * Returns own state of all registered devices. * @return ArrayList */ public SyncInfo getOwnState(){ return this.dbh.getOwnState(); } /*** * * PULL METHODS * * */ /** * Inserts a list of networks * @param others all missing networks */ public void updateNewNetworks(ArrayList others){ if (others != null && others.size() > 0){ this.dbh.updateNetworkInformation(others); } } /** * Updates new inserted devices. * @param otherDeviceIds ArrayList other device ids */ public void updateNewDevices(ArrayList otherDeviceIds){ if (otherDeviceIds != null){ ArrayList otherDevices = new ArrayList(); 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 updates list of new attack information */ public void updateNewAttacks(ArrayList updates){ if (updates != null && updates.size() > 0) this.dbh.insertSyncRecords(updates); } /** * * PUSH METHODS * * * */ /** * 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 pushDeviceIds(ArrayList otherIDs){ if (otherIDs != null){ return this.dbh.getMissingDeviceIds(otherIDs); //this.sendMissingDeviceIds(serverSocket, missingIds); } return new ArrayList(); } /** * Returns list of unsynced records. * @param si other states {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo} * @return unsynced sync records */ public ArrayList pushUnsyncedRecords(SyncInfo si){ if (si.deviceMap != null){ return this.dbh.getUnsyncedAttacksFor(si.deviceMap, false); } return new ArrayList(); } /** * Returns list of missing network records. * @param otherBSSIDs list of other bssids * @return array list of network records to push. */ public ArrayList pushNetworkInformation(ArrayList otherBSSIDs){ if (otherBSSIDs != null){ return this.dbh.getMissingNetworkRecords(otherBSSIDs); } return new ArrayList(); } }