Synchronizer.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package de.tudarmstadt.informatik.hostage.sync;
  2. /**
  3. * Created by Julien on 08.12.2014.
  4. */
  5. import android.util.Log;
  6. import java.net.ServerSocket;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Set;
  11. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  12. import de.tudarmstadt.informatik.hostage.logging.SyncData;
  13. import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
  14. import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
  15. import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
  16. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  17. import de.tudarmstadt.informatik.hostage.system.Device;
  18. public class Synchronizer {
  19. private HostageDBOpenHelper dbh;
  20. public Synchronizer(HostageDBOpenHelper dbh){
  21. super();
  22. this.dbh = dbh;
  23. }
  24. /**
  25. * Returns own state of all registered devices.
  26. * @return ArrayList<SyncDevice>
  27. */
  28. public SyncInfo getSyncInfo(){
  29. return this.dbh.getOwnState();
  30. }
  31. /**
  32. * Updates from the given sync data.
  33. * @param syncData {@link de.tudarmstadt.informatik.hostage.logging.SyncData}
  34. */
  35. public void updateFromSyncData(SyncData syncData){
  36. this.updateNewNetworks(syncData.networkRecords);
  37. this.updateNewAttacks(syncData.syncRecords);
  38. }
  39. /**
  40. * Updates the devices list by the given {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}.
  41. * @param sInfo {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
  42. * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncData}
  43. */
  44. public SyncData getSyncData(SyncInfo sInfo){
  45. SyncData sData = new SyncData();
  46. ArrayList<String> bssids = sInfo.bssids;
  47. HashMap<String, Long> deviceMap = sInfo.deviceMap;
  48. ArrayList<String> deviceIds = null;
  49. if (deviceMap != null && deviceMap.keySet() != null){
  50. deviceIds = this.stringSetToArrayList(deviceMap.keySet());
  51. this.updateNewDevices(deviceIds);
  52. }
  53. ArrayList<NetworkRecord> nets= new ArrayList<NetworkRecord>();
  54. if (bssids != null){
  55. nets = this.getMissingNetworkInformation(bssids);
  56. }
  57. ArrayList<SyncRecord> sRec = this.getUnsyncedRecords(sInfo);
  58. sData.syncRecords = sRec;
  59. sData.networkRecords = nets;
  60. return sData;
  61. }
  62. /**
  63. * Converts a string set in an array list
  64. * @param s the set to convert
  65. * @return array list
  66. */
  67. private ArrayList<String> stringSetToArrayList(Set<String> s){
  68. ArrayList<String> list = new ArrayList<String>();
  69. for (String string : s){
  70. list.add(string);
  71. }
  72. return list;
  73. }
  74. /***
  75. *
  76. * PULL METHODS
  77. *
  78. *
  79. */
  80. /**
  81. * Inserts a list of networks
  82. * @param others all missing networks
  83. */
  84. private void updateNewNetworks(ArrayList<NetworkRecord> others){
  85. if (others != null && others.size() > 0){
  86. Log.i("DEBUG_Sync", "Updating Network Data Objects: " + others.size());
  87. this.dbh.updateNetworkInformation(others);
  88. }
  89. }
  90. /**
  91. * Updates new inserted devices.
  92. * @param otherDeviceIds ArrayList<String> other device ids
  93. */
  94. public void updateNewDevices(ArrayList<String> otherDeviceIds){
  95. if (otherDeviceIds != null){
  96. ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
  97. ArrayList<String> ownDevicesds = this.dbh.getAllDevicesIds();
  98. if (otherDeviceIds.size() > 0)
  99. Log.i("DEBUG_Sync", "Updating Devices: " + otherDevices.size());
  100. ArrayList<String> n = this.diffArray(otherDeviceIds, ownDevicesds);
  101. for (String deviceId : n){
  102. SyncDevice device = new SyncDevice();
  103. device.setDeviceID(deviceId);
  104. device.setHighest_attack_id(-1);
  105. device.setLast_sync_timestamp(0);
  106. otherDevices.add(device);
  107. }
  108. if (otherDevices.size() > 0)
  109. this.dbh.updateSyncDevices(otherDevices);
  110. }
  111. }
  112. /**
  113. * Diffs to array by their content.
  114. * @param origin the original array
  115. * @param toRemove all strings to remove
  116. * @return diffed array list
  117. */
  118. private ArrayList<String> diffArray(ArrayList<String> origin, ArrayList<String> toRemove){
  119. ArrayList<String> n = new ArrayList<String>();
  120. for (String s : origin){
  121. if (!toRemove.contains(s)){
  122. n.add(s);
  123. }
  124. }
  125. return n;
  126. }
  127. /**
  128. * Get all missing sync records from the other device.
  129. * @param updates list of new attack information
  130. */
  131. private void updateNewAttacks(ArrayList<SyncRecord> updates){
  132. if (updates != null && updates.size() > 0) {
  133. Log.i("DEBUG_Sync", "Updating Attack Objects: " + updates.size());
  134. this.dbh.insertSyncRecords(updates);
  135. }
  136. }
  137. /**
  138. *
  139. * PUSH METHODS
  140. *
  141. *
  142. * */
  143. /**
  144. * Returns list of unsynced records.
  145. * @param si other states {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
  146. * @return unsynced sync records
  147. */
  148. private ArrayList<SyncRecord> getUnsyncedRecords(SyncInfo si){
  149. if (si.deviceMap != null){
  150. ArrayList<SyncRecord> records = this.dbh.getUnsyncedAttacksFor(si.deviceMap, true);
  151. Log.i("DEBUG_Sync", "Sending Attack Objects: " + records.size());
  152. return records;
  153. }
  154. return new ArrayList<SyncRecord>();
  155. }
  156. /**
  157. * Returns list of missing network records.
  158. * @param otherBSSIDs list of other bssids
  159. * @return array list of network records to push.
  160. */
  161. private ArrayList<NetworkRecord> getMissingNetworkInformation(ArrayList<String> otherBSSIDs){
  162. if (otherBSSIDs != null){
  163. ArrayList<NetworkRecord> records = this.dbh.getMissingNetworkRecords(otherBSSIDs);
  164. Log.i("DEBUG_Sync", "Sending Network Objects: " + records.size());
  165. return records;
  166. }
  167. return new ArrayList<NetworkRecord>();
  168. }
  169. }