123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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<String> myNetworks = this.dbh.getAllBSSIDS();
- ArrayList<NetworkRecord> 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<String> myDevices = this.dbh.getAllDevicesIds();
- ArrayList<String> otherDeviceIds = this.getOtherDevices(serverSocket, myDevices);
- 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.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();
- ArrayList<SyncRecord> 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<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;
- if (otherIDs != null){
- ArrayList<String> 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<SyncDevice> otherDeviceState = null;
- if (otherDeviceState != null){
- ArrayList<SyncRecord> 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<String> otherBSSIDs = null;
- if (otherBSSIDs != null){
- ArrayList<NetworkRecord> 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<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
- }
- }
|