Synchronizer.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. *
  25. * Update Idea:
  26. * PullDevices
  27. * PullNetworks
  28. * PullAttacks
  29. *
  30. * Commit Idea:
  31. * PushDevice
  32. * PushNetworks
  33. * PushAttacks
  34. *
  35. */
  36. /**
  37. * Returns all recorded BSSIDs.
  38. * @return array list of bssids
  39. */
  40. public ArrayList<String> getAllBSSIDs(){
  41. return this.dbh.getAllBSSIDS();
  42. }
  43. /**
  44. * Returns all devices ids.
  45. * @return ArrayList<String>
  46. */
  47. public ArrayList<String> getAllDeviceIDs(){
  48. return this.dbh.getAllDevicesIds();
  49. }
  50. /**
  51. * Returns own state of all registered devices.
  52. * @return ArrayList<SyncDevice>
  53. */
  54. public SyncInfo getOwnState(){
  55. return this.dbh.getOwnState();
  56. }
  57. /**
  58. * Updates from the given sync data.
  59. * @param syncData {@link de.tudarmstadt.informatik.hostage.logging.SyncData}
  60. */
  61. public void updateFromSyncData(SyncData syncData){
  62. this.updateNewNetworks(syncData.networkRecords);
  63. this.updateNewAttacks(syncData.syncRecords);
  64. }
  65. /**
  66. * Updates the devices list by the given {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}.
  67. * @param sInfo {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
  68. * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncData}
  69. */
  70. public SyncData getSyncData(SyncInfo sInfo){
  71. SyncData sData = new SyncData();
  72. ArrayList<String> bssids = sInfo.bssids;
  73. HashMap<String, Long> deviceMap = sInfo.deviceMap;
  74. ArrayList<String> deviceIds = null;
  75. if (deviceMap != null && deviceMap.keySet() != null){
  76. deviceIds = this.stringSetToArrayList(deviceMap.keySet());
  77. this.updateNewDevices(deviceIds);
  78. }
  79. ArrayList<NetworkRecord> nets= new ArrayList<NetworkRecord>();
  80. if (bssids != null){
  81. nets = this.getMissingNetworkInformation(bssids);
  82. }
  83. ArrayList<SyncRecord> sRec = this.getUnsyncedRecords(sInfo);
  84. sData.syncRecords = sRec;
  85. sData.networkRecords = nets;
  86. return sData;
  87. }
  88. /**
  89. * Converts a string set in an array list
  90. * @param s the set to convert
  91. * @return array list
  92. */
  93. private ArrayList<String> stringSetToArrayList(Set<String> s){
  94. ArrayList<String> list = new ArrayList<String>();
  95. for (String string : s){
  96. list.add(string);
  97. }
  98. return list;
  99. }
  100. /***
  101. *
  102. * PULL METHODS
  103. *
  104. *
  105. */
  106. /**
  107. * Inserts a list of networks
  108. * @param others all missing networks
  109. */
  110. public void updateNewNetworks(ArrayList<NetworkRecord> others){
  111. if (others != null && others.size() > 0){
  112. this.dbh.updateNetworkInformation(others);
  113. }
  114. }
  115. /**
  116. * Updates new inserted devices.
  117. * @param otherDeviceIds ArrayList<String> other device ids
  118. */
  119. private void updateNewDevices(ArrayList<String> otherDeviceIds){
  120. if (otherDeviceIds != null){
  121. ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
  122. ArrayList<String> ownDevicesds = this.dbh.getAllDevicesIds();
  123. ArrayList<String> n = this.diffArray(otherDeviceIds, ownDevicesds);
  124. for (String deviceId : n){
  125. SyncDevice device = new SyncDevice();
  126. device.setDeviceID(deviceId);
  127. device.setHighest_attack_id(-1);
  128. device.setLast_sync_timestamp(0);
  129. otherDevices.add(device);
  130. }
  131. if (otherDevices.size() > 0)
  132. this.dbh.updateSyncDevices(otherDevices);
  133. }
  134. }
  135. /**
  136. * Diffs to array by their content.
  137. * @param origin the original array
  138. * @param toRemove all strings to remove
  139. * @return diffed array list
  140. */
  141. private ArrayList<String> diffArray(ArrayList<String> origin, ArrayList<String> toRemove){
  142. ArrayList<String> n = new ArrayList<String>();
  143. for (String s : origin){
  144. if (!toRemove.contains(s)){
  145. n.add(s);
  146. }
  147. }
  148. return n;
  149. }
  150. /**
  151. * Get all missing sync records from the other device.
  152. * @param updates list of new attack information
  153. */
  154. private void updateNewAttacks(ArrayList<SyncRecord> updates){
  155. if (updates != null && updates.size() > 0)
  156. this.dbh.insertSyncRecords(updates);
  157. }
  158. /**
  159. *
  160. * PUSH METHODS
  161. *
  162. *
  163. * */
  164. /**
  165. * Returns list of unsynced records.
  166. * @param si other states {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
  167. * @return unsynced sync records
  168. */
  169. public ArrayList<SyncRecord> getUnsyncedRecords(SyncInfo si){
  170. if (si.deviceMap != null){
  171. return this.dbh.getUnsyncedAttacksFor(si.deviceMap, false);
  172. }
  173. return new ArrayList<SyncRecord>();
  174. }
  175. /**
  176. * Returns list of missing network records.
  177. * @param otherBSSIDs list of other bssids
  178. * @return array list of network records to push.
  179. */
  180. private ArrayList<NetworkRecord> getMissingNetworkInformation(ArrayList<String> otherBSSIDs){
  181. if (otherBSSIDs != null){
  182. return this.dbh.getMissingNetworkRecords(otherBSSIDs);
  183. }
  184. return new ArrayList<NetworkRecord>();
  185. }
  186. }