Synchronizer.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.SyncRecord;
  11. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  12. import de.tudarmstadt.informatik.hostage.system.Device;
  13. public class Synchronizer {
  14. private HostageDBOpenHelper dbh;
  15. public Synchronizer(HostageDBOpenHelper dbh){
  16. super();
  17. this.dbh = dbh;
  18. }
  19. /**
  20. *
  21. * Update Idea:
  22. * PullDevices
  23. * PullNetworks
  24. * PullAttacks
  25. *
  26. * Commit Idea:
  27. * PushDevice
  28. * PushNetworks
  29. * PushAttacks
  30. *
  31. */
  32. /***
  33. *
  34. * PULL METHODS
  35. *
  36. *
  37. */
  38. /**
  39. * Get a list of missing network records from the other device.
  40. * @param serverSocket server socket
  41. */
  42. public void pullNetworks(ServerSocket serverSocket){
  43. ArrayList<String> myNetworks = this.dbh.getAllBSSIDS();
  44. ArrayList<NetworkRecord> others = this.getOtherNetworkInformation(serverSocket, myNetworks);
  45. if (others != null && others.size() > 0){
  46. this.dbh.updateNetworkInformation(others);
  47. }
  48. }
  49. /**
  50. * Get a list of missing devices from the other device.
  51. * @param serverSocket server socket
  52. */
  53. public void pullNewDevices(ServerSocket serverSocket){
  54. ArrayList<String> myDevices = this.dbh.getAllDevicesIds();
  55. ArrayList<String> otherDeviceIds = this.getOtherDevices(serverSocket, myDevices);
  56. if (otherDeviceIds != null){
  57. ArrayList<SyncDevice> otherDevices = new ArrayList<SyncDevice>();
  58. for (String deviceId : otherDeviceIds){
  59. SyncDevice device = new SyncDevice();
  60. device.setDeviceID(deviceId);
  61. device.setHighest_attack_id(-1);
  62. device.setLast_sync_timestamp(0);
  63. otherDevices.add(device);
  64. }
  65. if (otherDevices.size() > 0)
  66. this.dbh.updateSyncDevices(otherDevices);
  67. }
  68. }
  69. /**
  70. * Get all missing sync records from the other device.
  71. * @param serverSocket server socket
  72. */
  73. public void pullAttacks(ServerSocket serverSocket){
  74. ArrayList<SyncDevice> myState = this.dbh.getSyncDevices();
  75. ArrayList<SyncRecord> updates = this.getChanges(serverSocket, myState);
  76. if (updates != null && updates.size() > 0)
  77. this.dbh.insertSyncRecords(updates);
  78. }
  79. /**
  80. * Sends own devices ids and parses a list of missing device ids.
  81. * @param serverSocket server socket
  82. * @param ownDevices own device ids
  83. * @return list of missing device ids
  84. */
  85. private ArrayList<String> getOtherDevices(ServerSocket serverSocket, ArrayList<String> ownDevices){
  86. // TODO parse other device ids from in put stream here
  87. return null;
  88. }
  89. /**
  90. * Sends all network bssids to the other devices and parses all missing network records.
  91. * @param serverSocket server socket
  92. * @param records network bssids
  93. * @return list of missing networks
  94. */
  95. private ArrayList<NetworkRecord> getOtherNetworkInformation(ServerSocket serverSocket, List<String> records){
  96. // TODO Parse missing network records from input stream here
  97. return null;
  98. }
  99. /**
  100. * Sends the own state to the other device and parses the answer.
  101. * @param serverSocket server socket
  102. * @param ownState our state
  103. * @return list of unsynced sync records
  104. */
  105. private ArrayList<SyncRecord> getChanges(ServerSocket serverSocket, ArrayList<SyncDevice> ownState){
  106. // TODO Parse unsynced sync records from in put stream here
  107. return null;
  108. }
  109. /**
  110. *
  111. * PUSH METHODS
  112. *
  113. *
  114. * */
  115. /**
  116. * Parses an array of device id strings and sends all missing sync devices ids.
  117. * @param serverSocket server socket
  118. */
  119. public void pushDeviceIds(ServerSocket serverSocket){
  120. // TODO parse other device ids from input stream here
  121. ArrayList<String> otherIDs = null;
  122. if (otherIDs != null){
  123. ArrayList<String> missingIds = this.dbh.getMissingDeviceIds(otherIDs);
  124. this.sendMissingDeviceIds(serverSocket, missingIds);
  125. }
  126. }
  127. /**
  128. * Parses an array of sync devices and sends the all unsynced records.
  129. * @param serverSocket server socket
  130. */
  131. public void pushUnsyncedRecords(ServerSocket serverSocket){
  132. // TODO parse other sync devices from input stream here
  133. ArrayList<SyncDevice> otherDeviceState = null;
  134. if (otherDeviceState != null){
  135. ArrayList<SyncRecord> unsynced = this.dbh.getUnsyncedAttacksFor(otherDeviceState, false);
  136. this.sendUnsyncedRecords(serverSocket, unsynced);
  137. }
  138. }
  139. /**
  140. * Parses an array of bssid strings and sends all missing network records.
  141. * @param serverSocket server socket
  142. */
  143. public void pushNetworkInformation(ServerSocket serverSocket){
  144. // TODO parse other bssids from input stream
  145. ArrayList<String> otherBSSIDs = null;
  146. if (otherBSSIDs != null){
  147. ArrayList<NetworkRecord> missings = this.dbh.getMissingNetworkRecords(otherBSSIDs);
  148. this.sendMissingNetworkRecords(serverSocket, missings);
  149. }
  150. }
  151. /**
  152. * Sends all missing network records over an object output stream.
  153. * @param serverSocket server socket
  154. * @param n missing network information
  155. */
  156. private void sendMissingNetworkRecords(ServerSocket serverSocket, ArrayList<NetworkRecord> n){
  157. // TODO make output stream here
  158. }
  159. /**
  160. * Sends all missing device ids over an object output stream
  161. * @param serverSocket server socket
  162. * @param missingIds all missing device ids
  163. */
  164. private void sendMissingDeviceIds(ServerSocket serverSocket, ArrayList<String> missingIds){
  165. // TODO make output stream here
  166. }
  167. /**
  168. * Sends all unsynced sync records over an object output stream
  169. * @param serverSocket server socket
  170. * @param unsynced array of unsynced sync records
  171. */
  172. private void sendUnsyncedRecords(ServerSocket serverSocket, ArrayList<SyncRecord> unsynced){
  173. // TODO make output stream here
  174. }
  175. }