WiFiP2pClientTask.java 3.9 KB

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