diff --git a/src/main/java/com/peak/salut/BackgroundClientRegistrationJob.java b/src/main/java/com/peak/salut/BackgroundClientRegistrationJob.java index d5291c2..e730d6b 100644 --- a/src/main/java/com/peak/salut/BackgroundClientRegistrationJob.java +++ b/src/main/java/com/peak/salut/BackgroundClientRegistrationJob.java @@ -5,6 +5,8 @@ import com.arasthel.asyncjob.AsyncJob; import com.bluelinelabs.logansquare.LoganSquare; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -43,14 +45,17 @@ public void doOnBackground() { //TODO Use buffered streams. Log.v(Salut.TAG, "Sending client registration data to server..."); String serializedClient = LoganSquare.serialize(salutInstance.thisDevice); - DataOutputStream toClient = new DataOutputStream(registrationSocket.getOutputStream()); + BufferedOutputStream bufferedOut = new BufferedOutputStream(registrationSocket.getOutputStream()); + DataOutputStream toClient = new DataOutputStream(bufferedOut); toClient.writeUTF(serializedClient); toClient.flush(); + Log.v(Salut.TAG, "Receiving server registration data..."); + BufferedInputStream bufferedInput = new BufferedInputStream(registrationSocket.getInputStream()); + DataInputStream fromServer = new DataInputStream(bufferedInput); + if(!salutInstance.thisDevice.isRegistered) { - Log.v(Salut.TAG, "Receiving server registration data..."); - DataInputStream fromServer = new DataInputStream(registrationSocket.getInputStream()); String serializedServer = fromServer.readUTF(); SalutDevice serverDevice = LoganSquare.parse(serializedServer, SalutDevice.class); serverDevice.serviceAddress = registrationSocket.getInetAddress().toString().replace("/", ""); @@ -71,8 +76,7 @@ public void run() { } else { - DataInputStream fromServer = new DataInputStream(registrationSocket.getInputStream()); - String registrationCode = fromServer.readUTF(); //TODO Use to verify. + String registrationCode = fromServer.readUTF(); //TODO Use to verify salutInstance.thisDevice.isRegistered = false; salutInstance.registeredHost = null; @@ -83,6 +87,10 @@ public void run() { Log.d(Salut.TAG, "This device has successfully been unregistered from the server."); } + + toClient.close(); + fromServer.close(); + } catch (IOException ex) { diff --git a/src/main/java/com/peak/salut/BackgroundServerRegistrationJob.java b/src/main/java/com/peak/salut/BackgroundServerRegistrationJob.java index 2b6101f..b9ee37c 100644 --- a/src/main/java/com/peak/salut/BackgroundServerRegistrationJob.java +++ b/src/main/java/com/peak/salut/BackgroundServerRegistrationJob.java @@ -5,6 +5,8 @@ import com.arasthel.asyncjob.AsyncJob; import com.bluelinelabs.logansquare.LoganSquare; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket; @@ -28,17 +30,22 @@ public void doOnBackground() { //TODO Use buffered streams. Log.v(Salut.TAG, "Receiving client registration data..."); - DataInputStream fromClient = new DataInputStream(clientSocket.getInputStream()); + BufferedInputStream bufferInput = new BufferedInputStream(clientSocket.getInputStream()); + DataInputStream fromClient = new DataInputStream(bufferInput); String serializedClient = fromClient.readUTF(); + SalutDevice clientDevice = LoganSquare.parse(serializedClient, SalutDevice.class); clientDevice.serviceAddress = clientSocket.getInetAddress().toString().replace("/", ""); + BufferedOutputStream bufferedOut = new BufferedOutputStream(clientSocket.getOutputStream()); + DataOutputStream toClient = new DataOutputStream(bufferedOut); + if (!clientDevice.isRegistered) { Log.v(Salut.TAG, "Sending server registration data..."); String serializedServer = LoganSquare.serialize(salutInstance.thisDevice); - DataOutputStream toClient = new DataOutputStream(clientSocket.getOutputStream()); toClient.writeUTF(serializedServer); + toClient.flush(); Log.d(Salut.TAG, "Registered device and user: " + clientDevice); clientDevice.isRegistered = true; @@ -61,7 +68,6 @@ public void run() { Log.d(Salut.TAG, "\nReceived request to unregister device.\n"); Log.v(Salut.TAG, "Sending registration code..."); - DataOutputStream toClient = new DataOutputStream(clientSocket.getOutputStream()); toClient.writeUTF(Salut.UNREGISTER_CODE); toClient.flush(); @@ -74,6 +80,10 @@ public void run() { } } } + + fromClient.close(); + toClient.close(); + } catch (Exception ex) { Log.e(Salut.TAG, "An error occurred while dealing with registration for a client."); }