Synchronizer.java 5.5 KB

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