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.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 * */ /*** * * PULL METHODS * * */ /** * Get a list of missing network records from the other device. * @param serverSocket server socket */ public void pullNetworks(ServerSocket serverSocket){ ArrayList myNetworks = this.dbh.getAllBSSIDS(); ArrayList others = this.getOtherNetworkInformation(serverSocket, myNetworks); if (others != null && others.size() > 0){ this.dbh.updateNetworkInformation(others); } } /** * Get a list of missing devices from the other device. * @param serverSocket server socket */ public void pullNewDevices(ServerSocket serverSocket){ ArrayList myDevices = this.dbh.getAllDevicesIds(); ArrayList otherDeviceIds = this.getOtherDevices(serverSocket, myDevices); 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 serverSocket server socket */ public void pullAttacks(ServerSocket serverSocket){ ArrayList myState = this.dbh.getSyncDevices(); ArrayList updates = this.getChanges(serverSocket, myState); 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 getOtherDevices(ServerSocket serverSocket, ArrayList 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 getOtherNetworkInformation(ServerSocket serverSocket, List 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 getChanges(ServerSocket serverSocket, ArrayList 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 otherIDs = null; if (otherIDs != null){ ArrayList missingIds = this.dbh.getMissingDeviceIds(otherIDs); this.sendMissingDeviceIds(serverSocket, missingIds); } } /** * 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 otherDeviceState = null; if (otherDeviceState != null){ ArrayList unsynced = this.dbh.getUnsyncedAttacksFor(otherDeviceState, false); this.sendUnsyncedRecords(serverSocket, unsynced); } } /** * 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 otherBSSIDs = null; if (otherBSSIDs != null){ ArrayList missings = this.dbh.getMissingNetworkRecords(otherBSSIDs); this.sendMissingNetworkRecords(serverSocket, missings); } } /** * 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 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 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 unsynced){ // TODO make output stream here } }