Skip to content

Commit

Permalink
Merge pull request #16 from lillialexis/50076
Browse files Browse the repository at this point in the history
[50076] Fix up the TCP socket code to better handle errors, disconnection/reconnection, reconnection, etc.
  • Loading branch information
lillialexis committed Nov 16, 2015
2 parents 2707d24 + 7c07adb commit a169615
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private String getOutputFromUrl(String url){
while ((s=buffer.readLine())!= null ){
output.append(s);
}
}catch(IOException e1){
}catch(Exception e1){
e1.printStackTrace();
Log.i(TAG, "THIS FAILED IN STREAM");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,27 @@ public void setButtons(UserCredentials userCredentials) {
keylbl.setText("Key Valid To:");

keyManagementLayout.setVisibility(View.GONE);
lock.setEnabled(userCredentials.getAuthorizedServices().isLock());
unlock.setEnabled(userCredentials.getAuthorizedServices().isLock());
trunk.setEnabled(userCredentials.getAuthorizedServices().isTrunk());
find.setEnabled(userCredentials.getAuthorizedServices().isLights());
start.setEnabled(userCredentials.getAuthorizedServices().isEngine());
stop.setEnabled(userCredentials.getAuthorizedServices().isEngine());
panic.setEnabled(userCredentials.getAuthorizedServices().isHazard());
validDate.setVisibility(View.VISIBLE);

if (!userCredentials.hasAnyAuthorizedServices() || !userCredentials.isKeyValid()) {
lock.setEnabled(false);
unlock.setEnabled(false);
trunk.setEnabled(false);
find.setEnabled(false);
start.setEnabled(false);
stop.setEnabled(false);
panic.setEnabled(false);

validDate.setText("Revoked");
} else {
lock.setEnabled(userCredentials.getAuthorizedServices().isLock());
unlock.setEnabled(userCredentials.getAuthorizedServices().isLock());
trunk.setEnabled(userCredentials.getAuthorizedServices().isTrunk());
find.setEnabled(userCredentials.getAuthorizedServices().isLights());
start.setEnabled(userCredentials.getAuthorizedServices().isEngine());
stop.setEnabled(userCredentials.getAuthorizedServices().isEngine());
panic.setEnabled(userCredentials.getAuthorizedServices().isHazard());

validDate.setText(userCredentials.getValidTo());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.app.AlertDialog;
import android.content.*;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.http.HttpResponseCache;
import android.os.IBinder;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -190,9 +192,15 @@ public void setStatus(String msg){
startActivity(intent);
}
else{
login_fragment.userName.setText("");
login_fragment.password.setText("");
Toast.makeText(LoginActivity.this,"username and password don't match",Toast.LENGTH_LONG).show();
//login_fragment.userName.setText("");
//login_fragment.password.setText("");
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null || networkInfo.getDetailedState() == NetworkInfo.DetailedState.DISCONNECTED) {
Toast.makeText(LoginActivity.this, "Network unavailable", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, "username and password don't match", Toast.LENGTH_LONG).show();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void onNext(final RangeObject ro) {
UserCredentials userCredentials = ServerNode.getUserCredentials();
if (userCredentials != null) {
try {
if (userCredentials.getUserType().equals("guest") && (userCredentials.getAuthorizedServices().isLock() || !userCredentials.isKeyValid())) { // TODO: Test
if (userCredentials.getUserType().equals("guest") && (!userCredentials.getAuthorizedServices().isLock() || !userCredentials.isKeyValid())) { // TODO: Test
Log.d(TAG, "User is not authorized to lock/unlock car.");

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import com.google.gson.Gson;
Expand Down Expand Up @@ -83,13 +84,23 @@ public class ServerNode
private final static ServiceBundle certProvServiceBundle = new ServiceBundle(applicationContext, RVI_DOMAIN, CERT_PROV_BUNDLE, certProvServiceIdentifiers);
private final static ServiceBundle reportingServiceBundle = new ServiceBundle(applicationContext, RVI_DOMAIN, REPORTING_BUNDLE, reportingServiceIdentifiers);

private enum ConnectionStatus
{
DISCONNECTED,
CONNECTING,
CONNECTED;
}

private static ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;

private static ServerNode ourInstance = new ServerNode();

public static ServerNode getInstance() {
return ourInstance;
}

private ServerNode() {
/* Listeners */
ServiceBundle.ServiceBundleListener serviceBundleListener = new ServiceBundle.ServiceBundleListener()
{
@Override
Expand Down Expand Up @@ -139,29 +150,83 @@ public void onServiceInvoked(ServiceBundle serviceBundle, String serviceIdentifi
}
};

RVINode.RVINodeListener nodeListener = new RVINode.RVINodeListener()
{
@Override
public void nodeDidConnect() {
Log.d(TAG, "Connected to RVI provisioning server!");
connectionStatus = ConnectionStatus.CONNECTED;

stopRepeatingTask();
}

@Override
public void nodeDidFailToConnect(Throwable trigger) {
Log.d(TAG, "Failed to connect to RVI provisioning server!");
connectionStatus = ConnectionStatus.DISCONNECTED;

//startRepeatingTask();
}

@Override
public void nodeDidDisconnect(Throwable trigger) {
Log.d(TAG, "Disconnected from RVI provisioning server!");
connectionStatus = ConnectionStatus.DISCONNECTED;

/* Try and reconnect */
startRepeatingTask();
}
};

certProvServiceBundle.setListener(serviceBundleListener);
reportingServiceBundle.setListener(serviceBundleListener);


rviNode.setListener(nodeListener);

rviNode.addBundle(certProvServiceBundle);
rviNode.addBundle(reportingServiceBundle);
}

public static void connect() {
connectToServer();

Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable()
{
@Override
public void run() {
if (connectionStatus == ConnectionStatus.DISCONNECTED) connect();

timerHandler.postDelayed(this, 3000);
}
};


void startRepeatingTask() {
timerHandler.postDelayed(timerRunnable, 0);
}

void stopRepeatingTask() {
timerHandler.removeCallbacks(timerRunnable);
}

private static void connectToServer() {
if (rviNode.isConnected()) rviNode.disconnect();


public static void connect() {
Log.d(TAG, "Attempting to connect to RVI provisioning server.");

rviNode.setServerUrl(preferences.getString("pref_rvi_server", "38.129.64.40"));
rviNode.setServerPort(Integer.parseInt(preferences.getString("pref_rvi_server_port", "8807")));

connectionStatus = ConnectionStatus.CONNECTING;

rviNode.connect();
}

public static void requestRemoteCredentials() {
Log.d(TAG, "Requesting remote credentials from RVI provisioning server.");

if (connectionStatus == ConnectionStatus.DISCONNECTED) connect();

HashMap<String, String> parameters = new HashMap<>();

try {
Expand All @@ -175,10 +240,18 @@ public static void requestRemoteCredentials() {
}

public static void modifyRemoteCredentials(UserCredentials remoteCredentials) {
Log.d(TAG, "Modifying remote credentials on RVI provisioning server.");

if (connectionStatus == ConnectionStatus.DISCONNECTED) connect();

certProvServiceBundle.invokeService(CERT_MODIFY, remoteCredentials, 5000);
}

public static void createRemoteCredentials(UserCredentials remoteCredentials) {
Log.d(TAG, "Creating remote credentials on RVI provisioning server.");

if (connectionStatus == ConnectionStatus.DISCONNECTED) connect();

certProvServiceBundle.invokeService(CERT_CREATE, remoteCredentials, 5000);
}

Expand Down Expand Up @@ -228,7 +301,9 @@ public static Collection<UserCredentials> getRemoteCredentialsList() {
if (credListStr == null) return null;

Collection<UserCredentials> credsList = null;
Type collectionType = new TypeToken<Collection<UserCredentials>>() {}.getType();
Type collectionType = new TypeToken<Collection<UserCredentials>>()
{
}.getType();

try {
credsList = gson.fromJson(credListStr, collectionType);
Expand Down Expand Up @@ -301,23 +376,4 @@ public static void setThereIsNewInvokedServiceReport(Boolean isNewReport) {
editor.putBoolean(NEW_INVOKED_SERVICE_REPORT_KEY, isNewReport);
editor.commit();
}

/* Listeners */
private static RVINode.RVINodeListener nodeListener = new RVINode.RVINodeListener()
{
@Override
public void nodeDidConnect() {

}

@Override
public void nodeDidFailToConnect() {

}

@Override
public void nodeDidDisconnect() {

}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public boolean isConnected() {
}

@Override
public boolean isEnabled() {
public boolean isConfigured() {
return false;
}

Expand All @@ -43,7 +43,8 @@ public void connect() {
}

@Override
public void disconnect() {
public void disconnect(Throwable trigger) {
if (mRemoteConnectionListener != null) mRemoteConnectionListener.onRemoteConnectionDidDisconnect(trigger);
}

@Override
Expand Down
Loading

0 comments on commit a169615

Please sign in to comment.