Skip to content
This repository has been archived by the owner on Dec 29, 2019. It is now read-only.

Commit

Permalink
Registration now uses buffered input and output.
Browse files Browse the repository at this point in the history
  • Loading branch information
neatorobito committed Jun 14, 2015
1 parent aa112f6 commit e63186a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/main/java/com/peak/salut/BackgroundClientRegistrationJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("/", "");
Expand All @@ -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;
Expand All @@ -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)
{
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/peak/salut/BackgroundServerRegistrationJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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.");
}
Expand Down

0 comments on commit e63186a

Please sign in to comment.