WiFiP2pClientTask.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package de.tudarmstadt.informatik.hostage.sync.wifi_direct;
  2. import android.net.wifi.p2p.WifiP2pDevice;
  3. import android.net.wifi.p2p.WifiP2pManager;
  4. import android.util.Log;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.OutputStream;
  10. import java.net.InetSocketAddress;
  11. import java.net.Socket;
  12. import de.tudarmstadt.informatik.hostage.R;
  13. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  14. /**
  15. * Created by Julien on 07.01.2015.
  16. *
  17. * The client task.
  18. *
  19. * The client is the initial actor and will try to connect to the host as long as the task is not interrupted or successfully connected to the host.
  20. *
  21. *
  22. */
  23. public abstract class WiFiP2pClientTask extends BackgroundTask {
  24. // Set the port here for the client and host task.
  25. public static int DEFAULT_WIFI_PORT = 1029;
  26. private String hostIP;
  27. private Socket socket;
  28. private WifiP2pDevice ownDevice;
  29. public String getHostIP() {
  30. return hostIP;
  31. }
  32. public void setHostIP(String hostIP) {
  33. this.hostIP = hostIP;
  34. }
  35. public static int port(){
  36. return DEFAULT_WIFI_PORT;
  37. }
  38. public static int time_out(){
  39. return 1000;
  40. }
  41. public WiFiP2pClientTask(String hostIP, WifiP2pDevice ownDevice, BackgroundTaskCompletionListener l){
  42. super(l);
  43. this.ownDevice = ownDevice;
  44. this.hostIP = hostIP;
  45. }
  46. @Override
  47. public void interrupt(boolean b){
  48. super.interrupt(b);
  49. if (b && this.socket != null) {
  50. try {
  51. this.socket.close();
  52. } catch (IOException e) {
  53. Log.e("DEBUG_WiFiP2p", e.getMessage());
  54. }
  55. }
  56. }
  57. @Override
  58. public String performInBackground(){
  59. String e_message = null;
  60. int tryNum = 1;
  61. int max_tries = 10;
  62. while (!this.isInterrupted() && tryNum <= max_tries){
  63. this.socket = new Socket();
  64. try {
  65. Log.d("DEBUG_WiFiP2p", "ClientTask - Opening client socket - ");
  66. socket.bind(null);
  67. socket.connect((new InetSocketAddress(hostIP, port())), time_out());
  68. Log.d("DEBUG_WiFiP2p", "ClientTask socket - " + socket.isConnected());
  69. this.handleConnection(socket);
  70. Log.d("DEBUG_WiFiP2p", "ClientTask - Data written");
  71. } catch (ClassNotFoundException e){
  72. Log.e("DEBUG_WiFiP2p", e.getMessage());
  73. e_message = e.getLocalizedMessage();
  74. if (e_message == null){
  75. e_message = WiFiP2pServerTask.ERROR_COMMUNICATION_FAILED;// COMMUNICATION_ERROR
  76. }
  77. return e_message; } catch (IOException e) {
  78. Log.e("DEBUG_WiFiP2p", e.getMessage());
  79. if(this.isInterrupted()) {
  80. this.interrupt(true);
  81. break;
  82. }
  83. long seconds_to_wait = (long) Math.min(60, Math.pow(2, 1));
  84. tryNum++;
  85. Log.i("DEBUG_WiFiP2p", "ClientTaskError - could not connect to server. Will try again in " + 1 + "s");
  86. try {
  87. Thread.sleep(seconds_to_wait * time_out());
  88. } catch (InterruptedException ie){
  89. ie.printStackTrace();
  90. }
  91. } finally {
  92. if (socket != null) {
  93. if (socket.isConnected()) {
  94. try {
  95. socket.close();
  96. } catch (IOException e) {
  97. // Give up
  98. e.printStackTrace();
  99. Log.d("DEBUG_WiFiP2p","ClientTaskError - Failed to close socket - "+ e.getLocalizedMessage());
  100. e_message = e.getLocalizedMessage();
  101. if (e_message == null){
  102. e_message = WiFiP2pServerTask.ERROR_CONNECTION_FAILED;// FAILED TO CONNECT
  103. }
  104. return e_message;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. if (tryNum > max_tries){
  111. e_message = WiFiP2pServerTask.ERROR_CONNECTION_TIMEOUT;// CONNECTION_TIMEOUT
  112. }
  113. return e_message;
  114. }
  115. /**
  116. * Initiates the client task and sends the first response to the host.
  117. * @param client the client socket
  118. * @throws IOException
  119. * @throws ClassNotFoundException
  120. */
  121. private void handleConnection(Socket client) throws IOException, ClassNotFoundException {
  122. InputStream is = client.getInputStream();
  123. ObjectInputStream ois = new ObjectInputStream(is);
  124. OutputStream os = client.getOutputStream();
  125. ObjectOutputStream oos = new ObjectOutputStream(os);
  126. Object obj = null;
  127. do {
  128. WiFiP2pSerializableObject receivedObj = ( WiFiP2pSerializableObject) obj;
  129. obj = null;
  130. WiFiP2pSerializableObject toSend = this.handleReceivedObject(receivedObj);
  131. if (toSend != null) {
  132. toSend.setActingDevice_IP_address(this.ownDevice.deviceAddress);
  133. oos.writeObject(toSend);
  134. oos.flush();
  135. oos.reset();
  136. obj = ois.readObject();
  137. }
  138. } while (obj != null && obj instanceof WiFiP2pSerializableObject);
  139. oos.close();
  140. os.close();
  141. ois.close();
  142. is.close();
  143. this.interrupt(true);
  144. }
  145. /**
  146. * This method is initially called with a null parameter to inform about a initial state.
  147. * Return null to disable the client task.
  148. * @param receivedObj the response for the last request, null if it is the first call on the host.
  149. * @return WiFiP2pSerializableObject request object
  150. */
  151. public abstract WiFiP2pSerializableObject handleReceivedObject(WiFiP2pSerializableObject receivedObj);
  152. }