WiFiP2pServerTask.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.ServerSocket;
  11. import java.net.Socket;
  12. /**
  13. * Created by Julien on 07.01.2015.
  14. *
  15. * The Server Task.
  16. *
  17. * The Server task is waiting as long as a client is connecting to it.
  18. * As a result of this the main part and process will start after the client had send the first data.
  19. *
  20. * You never know how will become the host / server.
  21. * So you need to implement a strategy if you want do send just from one specific device to the other.
  22. *
  23. * * The server creates the object stream before the client can do it - to avoid a chicken and egg problem.
  24. */
  25. public abstract class WiFiP2pServerTask extends BackgroundTask {
  26. private ServerSocket serverSocket;
  27. private WifiP2pDevice ownDevice;
  28. @Override
  29. public void interrupt(boolean b){
  30. super.interrupt(b);
  31. if (b && this.serverSocket != null) {
  32. try {
  33. this.serverSocket.close();
  34. } catch (IOException e) {
  35. Log.e("WiFiP2pServerTask", e.getMessage());
  36. }
  37. }
  38. }
  39. public WiFiP2pServerTask( WifiP2pDevice ownDevice, BackgroundTaskCompletionListener l){
  40. super(l);
  41. this.ownDevice = ownDevice;
  42. }
  43. @Override
  44. public boolean performInBackground(){
  45. while (!this.isInterrupted()){
  46. try {
  47. this.serverSocket = new ServerSocket(WiFiP2pClientTask.port());
  48. Log.d("WiFiP2pServerTask", "Server: Socket opened");
  49. Socket client = this.serverSocket.accept();
  50. Log.d("WiFiP2pServerTask", "Server: connection done");
  51. this.handleConnection(client, this.serverSocket);
  52. client.close();
  53. serverSocket.close();
  54. return true;
  55. } catch (ClassNotFoundException e){
  56. e.printStackTrace();
  57. Log.e("WiFiP2pServerTask", e.getMessage());
  58. return false;
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. Log.e("WiFiP2pServerTask", e.getMessage());
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. /**
  68. * The server creates the object stream before the client can do it to avoid a chicken and egg problem.
  69. * @param client the client socket.
  70. * @param server the server socket.
  71. * @throws IOException
  72. * @throws ClassNotFoundException
  73. */
  74. private void handleConnection(Socket client, ServerSocket server) throws IOException, ClassNotFoundException {
  75. OutputStream os = client.getOutputStream();
  76. ObjectOutputStream oos = new ObjectOutputStream(os);
  77. oos.flush();
  78. InputStream is = client.getInputStream();
  79. ObjectInputStream ois = new ObjectInputStream(is);
  80. Object obj = ois.readObject();
  81. while (obj != null && obj instanceof WiFiP2pSerializableObject) {
  82. WiFiP2pSerializableObject receivedObj = ( WiFiP2pSerializableObject) obj;
  83. obj = null;
  84. WiFiP2pSerializableObject toSend = this.handleReceivedObject(receivedObj);
  85. if (toSend != null) {
  86. toSend.setActingDevice_IP_address(this.ownDevice.deviceAddress);
  87. oos.writeObject(toSend);
  88. oos.flush();
  89. oos.reset();
  90. }
  91. try {
  92. obj = ois.readObject();
  93. }catch (IOException e){
  94. // IF NULL WAS TRANSMITTED
  95. obj = null;
  96. }
  97. }
  98. oos.close();
  99. os.close();
  100. ois.close();
  101. is.close();
  102. }
  103. /**
  104. * This method will be called if the server receives data from the client.
  105. * Always return a WiFiP2pSerializableObject instance to give a simple response, otherwise the connection will be disconnected.
  106. * @param receivedObj WiFiP2pSerializableObject the clients request
  107. * @return WiFiP2pSerializableObject the response
  108. */
  109. abstract public WiFiP2pSerializableObject handleReceivedObject(WiFiP2pSerializableObject receivedObj);
  110. }