CommunicationThread.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package de.tudarmstadt.informatik.hostage.sync.bluetooth;
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  9. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  10. import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
  11. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  12. import de.tudarmstadt.informatik.hostage.sync.SyncMessage;
  13. import android.bluetooth.BluetoothSocket;
  14. import android.content.Context;
  15. import android.content.SharedPreferences;
  16. import android.os.Handler;
  17. import android.preference.PreferenceManager;
  18. import android.util.Log;
  19. public class CommunicationThread extends Thread {
  20. private final Context context;
  21. private final BluetoothSocket mmSocket;
  22. private final ObjectInputStream objectInput;
  23. private final ObjectOutputStream objectOuput;
  24. private final Handler mHandler;
  25. private final HostageDBOpenHelper mdbh;
  26. private final SharedPreferences pref;
  27. public CommunicationThread(Context con, BluetoothSocket socket, Handler handler) {
  28. mmSocket = socket;
  29. mHandler = handler;
  30. context = con;
  31. mdbh = new HostageDBOpenHelper(context);
  32. pref = PreferenceManager.getDefaultSharedPreferences(context);
  33. ObjectInputStream tmpIn = null;
  34. ObjectOutputStream tmpOut = null;
  35. // Get the input and output streams, using temp objects because
  36. // member streams are final
  37. try {
  38. tmpOut = new ObjectOutputStream(socket.getOutputStream());
  39. tmpIn = new ObjectInputStream(socket.getInputStream());
  40. } catch (IOException e) {
  41. mHandler.obtainMessage(BluetoothSync.CONNECTION_FAILED).sendToTarget();
  42. e.printStackTrace();
  43. }
  44. objectInput = tmpIn;
  45. objectOuput = tmpOut;
  46. }
  47. /* Call this from the main activity to shutdown the connection */
  48. public void cancel() {
  49. try {
  50. mmSocket.close();
  51. } catch (IOException e) {
  52. }
  53. }
  54. @Override
  55. public void run() {
  56. // Keep listening to the InputStream until an exception occurs
  57. while (true) {
  58. try {
  59. Object inputObject = objectInput.readObject();
  60. if(inputObject instanceof SyncMessage){
  61. handleMessage((SyncMessage) inputObject);
  62. }
  63. } catch (ClassNotFoundException e) {
  64. e.printStackTrace();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. break;
  68. }
  69. }
  70. }
  71. /**
  72. * Handles a received synchronization message.
  73. * @param message The received message.
  74. */
  75. private void handleMessage(SyncMessage message){
  76. Log.i("CommunicationThread", "Recieved: " + message.getMessage_code());
  77. switch(message.getMessage_code()){
  78. case SyncMessage.SYNC_REQUEST:
  79. HashMap<String, Long> devices_remote = (HashMap<String, Long>) message.getPayload();
  80. HashMap<String, Long> devices_local = mdbh.getSyncDevices();
  81. ArrayList<SyncInfoRecord> syncInfo = mdbh.getSyncInfo();
  82. long tracing_timestamp = 0;
  83. //TODO "TRACING" zentral definieren
  84. if(devices_local.containsKey("TRACING"))
  85. tracing_timestamp = devices_local.get("TRACING");
  86. for(Iterator<String> i = devices_remote.keySet().iterator(); i.hasNext(); ){
  87. String key = i.next();
  88. if((devices_local.containsKey(key) && devices_local.get(key) >= devices_remote.get(key))
  89. || (tracing_timestamp > devices_remote.get(key))){
  90. Log.i("CommunicationThread", "Removed: " + key);
  91. i.remove();
  92. }
  93. }
  94. mdbh.updateSyncDevices(devices_remote);
  95. for ( Iterator<SyncInfoRecord> i = syncInfo.iterator(); i.hasNext(); ){
  96. SyncInfoRecord info = i.next();
  97. if(devices_remote.containsKey(info.getDeviceID())){
  98. Log.i("CommunicationThread", "Removed from SyncInfo: " + info.getDeviceID());
  99. i.remove();
  100. }
  101. }
  102. write(new SyncMessage(SyncMessage.SYNC_RESPONSE_SYNC_INFO, syncInfo));
  103. write(new SyncMessage(SyncMessage.SYNC_RESPONSE_NET_INFO, mdbh.getNetworkInformation()));
  104. break;
  105. case SyncMessage.SYNC_RESPONSE_NET_INFO:
  106. ArrayList<NetworkRecord> netInfo = (ArrayList<NetworkRecord>) message.getPayload();
  107. mdbh.updateNetworkInformation(netInfo);
  108. mHandler.obtainMessage(BluetoothSync.SYNC_SUCCESSFUL).sendToTarget();
  109. break;
  110. case SyncMessage.SYNC_RESPONSE_SYNC_INFO:
  111. ArrayList<SyncInfoRecord> syncInfo_new = (ArrayList<SyncInfoRecord>) message.getPayload();
  112. mdbh.updateSyncInfo(syncInfo_new);
  113. break;
  114. default:
  115. //TODO DEFAULT WITH UNKNOWN MESSAGE CODE;
  116. }
  117. }
  118. /* Call this from the main activity to send data to the remote device */
  119. public void write(SyncMessage message) {
  120. try {
  121. objectOuput.writeObject(message);
  122. //TODO NACHRICHT SCHICKEN? mHandler.obtainMessage(BluetoothSync.MESSAGE_SENT).sendToTarget();
  123. } catch (IOException e) {
  124. mHandler.obtainMessage(BluetoothSync.CONNECTION_FAILED).sendToTarget();
  125. e.printStackTrace();
  126. }
  127. }
  128. }