WiFiP2pClientTask.java 5.2 KB

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