CommunicationThread.java 4.6 KB

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