Synchronizer.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.List;
  8. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  9. import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
  10. import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
  11. import de.tudarmstadt.informatik.hostage.logging.SyncRecord;
  12. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  13. import de.tudarmstadt.informatik.hostage.system.Device;
  14. public class Synchronizer {
  15. private HostageDBOpenHelper dbh;
  16. public Synchronizer(HostageDBOpenHelper dbh){
  17. super();
  18. this.dbh = dbh;
  19. }
  20. /**
  21. *
  22. * Update Idea:
  23. * PullDevices
  24. * PullNetworks
  25. * PullAttacks
  26. *
  27. * Commit Idea:
  28. * PushDevice
  29. * PushNetworks
  30. * PushAttacks
  31. *
  32. */
  33. /**
  34. * Returns all recorded BSSIDs.
  35. * @return array list of bssids
  36. */
  37. public ArrayList<String> getAllBSSIDs(){
  38. return this.dbh.getAllBSSIDS();
  39. }
  40. /**
  41. * Returns all devices ids.
  42. * @return ArrayList<String>
  43. */
  44. public ArrayList<String> getAllDeviceIDs(){
  45. return this.dbh.getAllDevicesIds();
  46. }
  47. /**
  48. * Returns own state of all registered devices.
  49. * @return ArrayList<SyncDevice>
  50. */
  51. public SyncInfo getOwnState(){
  52. return this.dbh.getOwnState();
  53. }
  54. /***
  55. *
  56. * PULL METHODS
  57. *
  58. *
  59. */
  60. /**
  61. * Inserts a list of networks
  62. * @param others all missing networks
  63. */
  64. public void updateNewNetworks(ArrayList<NetworkRecord> others){
  65. if (others != null && others.size() > 0){
  66. this.dbh.updateNetworkInformation(others);
  67. }
  68. }
  69. /**
  70. * Updates new inserted devices.
  71. * @param otherDeviceIds ArrayList<String> other device ids
  72. */
  73. public void updateNewDevices(ArrayList<String> otherDeviceIds){
  74. if (otherDeviceIds != null){
  75. ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
  76. for (String deviceId : otherDeviceIds){
  77. SyncDevice device = new SyncDevice();
  78. device.setDeviceID(deviceId);
  79. device.setHighest_attack_id(-1);
  80. device.setLast_sync_timestamp(0);
  81. otherDevices.add(device);
  82. }
  83. if (otherDevices.size() > 0)
  84. this.dbh.updateSyncDevices(otherDevices);
  85. }
  86. }
  87. /**
  88. * Get all missing sync records from the other device.
  89. * @param updates list of new attack information
  90. */
  91. public void updateNewAttacks(ArrayList<SyncRecord> updates){
  92. if (updates != null && updates.size() > 0)
  93. this.dbh.insertSyncRecords(updates);
  94. }
  95. /**
  96. *
  97. * PUSH METHODS
  98. *
  99. *
  100. * */
  101. /**
  102. * Parses an array of device id strings and sends all missing sync devices ids.
  103. * @param otherIDs other device ids
  104. * @return missing device ids
  105. */
  106. public ArrayList<String> pushDeviceIds(ArrayList<String> otherIDs){
  107. if (otherIDs != null){
  108. return this.dbh.getMissingDeviceIds(otherIDs);
  109. //this.sendMissingDeviceIds(serverSocket, missingIds);
  110. }
  111. return new ArrayList<String>();
  112. }
  113. /**
  114. * Returns list of unsynced records.
  115. * @param si other states {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
  116. * @return unsynced sync records
  117. */
  118. public ArrayList<SyncRecord> pushUnsyncedRecords(SyncInfo si){
  119. if (si.deviceMap != null){
  120. return this.dbh.getUnsyncedAttacksFor(si.deviceMap, false);
  121. }
  122. return new ArrayList<SyncRecord>();
  123. }
  124. /**
  125. * Returns list of missing network records.
  126. * @param otherBSSIDs list of other bssids
  127. * @return array list of network records to push.
  128. */
  129. public ArrayList<NetworkRecord> pushNetworkInformation(ArrayList<String> otherBSSIDs){
  130. if (otherBSSIDs != null){
  131. return this.dbh.getMissingNetworkRecords(otherBSSIDs);
  132. }
  133. return new ArrayList<NetworkRecord>();
  134. }
  135. }