WiFiP2pClientTask.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package de.tudarmstadt.informatik.hostage.sync.wifi_direct;
  2. import android.util.Log;
  3. import com.example.android.wifidirect.WiFiDirectActivity;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.OutputStream;
  9. import java.net.InetSocketAddress;
  10. import java.net.Socket;
  11. /**
  12. * Created by Julien on 07.01.2015.
  13. */
  14. public class WiFiP2pClientTask extends BackgroundTask {
  15. public interface WiFiP2pCompletionListener {
  16. public void didSucceed();
  17. public void didFail();
  18. }
  19. private String hostIP;
  20. private Socket socket;
  21. private WiFiP2pCompletionListener completionListener;
  22. public String getHostIP() {
  23. return hostIP;
  24. }
  25. public void setHostIP(String hostIP) {
  26. this.hostIP = hostIP;
  27. }
  28. public int port(){
  29. return 8988;
  30. }
  31. public int time_out(){
  32. return 1000;
  33. }
  34. public WiFiP2pClientTask(String hostIP, WiFiP2pCompletionListener l){
  35. super();
  36. this.hostIP = hostIP;
  37. this.completionListener = l;
  38. }
  39. @Override
  40. public void interrupt(boolean b){
  41. super.interrupt(b);
  42. if (b && this.socket != null) {
  43. try {
  44. this.socket.close();
  45. } catch (IOException e) {
  46. Log.e("WiFiP2pClientTask", e.getMessage());
  47. }
  48. }
  49. }
  50. @Override
  51. public void performInBackground(){
  52. int tryNum = 0;
  53. while (!this.isInterrupted()){
  54. this.socket = new Socket();
  55. try {
  56. Log.d(WiFiDirectActivity.TAG, "Opening client socket - ");
  57. socket.bind(null);
  58. socket.connect((new InetSocketAddress(hostIP, this.port())), this.time_out());
  59. Log.d(WiFiDirectActivity.TAG, "Client socket - " + socket.isConnected());
  60. this.handleConnection(socket);
  61. Log.d("WiFiP2pClientTask", "Client: Data written");
  62. } catch (ClassNotFoundException e){
  63. Log.e("WiFiP2pClientTask", e.getMessage());
  64. this.completionListener.didFail();
  65. return;
  66. } catch (IOException e) {
  67. Log.e("WiFiP2pClientTask", e.getMessage());
  68. if(tryNum >= 5) {
  69. this.interrupt(true);
  70. break;
  71. }
  72. long seconds_to_wait = (long) Math.min(60, Math.pow(2, tryNum));
  73. Log.i("client", "could not connect to server. Will try again in " + seconds_to_wait + "s");
  74. try {
  75. Thread.sleep(seconds_to_wait * 1000);
  76. } catch (InterruptedException ie){
  77. }
  78. } finally {
  79. if (socket != null) {
  80. if (socket.isConnected()) {
  81. try {
  82. socket.close();
  83. } catch (IOException e) {
  84. // Give up
  85. e.printStackTrace();
  86. this.completionListener.didFail();
  87. return;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. this.completionListener.didSucceed();
  94. }
  95. private void handleConnection(Socket client) throws IOException, ClassNotFoundException {
  96. InputStream is = client.getInputStream();
  97. ObjectInputStream ois = new ObjectInputStream(is);
  98. OutputStream os = client.getOutputStream();
  99. ObjectOutputStream oos = new ObjectOutputStream(os);
  100. Object obj = null;
  101. do {
  102. WiFiP2pSerializableObject receivedObj = ( WiFiP2pSerializableObject) obj;
  103. obj = null;
  104. WiFiP2pSerializableObject toSend = this.handleReceivedObject(receivedObj);
  105. if (toSend != null) {
  106. oos.writeObject(toSend);
  107. oos.flush();
  108. oos.reset();
  109. obj = ois.readObject();
  110. }
  111. } while (obj != null && obj instanceof WiFiP2pSerializableObject);
  112. oos.close();
  113. os.close();
  114. ois.close();
  115. is.close();
  116. this.interrupt(true);
  117. }
  118. public WiFiP2pSerializableObject handleReceivedObject(WiFiP2pSerializableObject receivedObj){
  119. return null;
  120. }
  121. }