Skip to content

Commit

Permalink
Merge pull request #17 from PDXostc/develop
Browse files Browse the repository at this point in the history
Pre-PKI Release
  • Loading branch information
lillialexis authored Nov 22, 2016
2 parents c51921d + 2e431fc commit 7e74045
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void sendRviRequest(DlinkPacket dlinkPacket) {
if (!isConnected() || !isConfigured()) // TODO: Call error on listener
return;

new SendDataTask(dlinkPacket).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);//, dlinkPacket.toJsonString());
new SendDataTask(dlinkPacket).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);//, dlinkPacket.toJsonString());
}

@Override
Expand Down Expand Up @@ -97,7 +97,7 @@ private void connectSocket() {
Log.d(TAG, "Connecting to device: " + mDeviceAddress + ":" + mServiceRecord + ":" + mChannel);

ConnectTask connectAndAuthorizeTask = new ConnectTask(mDeviceAddress, mServiceRecord, mChannel);
connectAndAuthorizeTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
connectAndAuthorizeTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}

private class ConnectTask extends AsyncTask<Void, String, Throwable>
Expand Down Expand Up @@ -186,7 +186,7 @@ protected void onPostExecute(Throwable result) {
Log.d(TAG, "Connecting Bluetooth socket: Creating listener...");

ListenTask listenTask = new ListenTask();
listenTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
listenTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

if (mRemoteConnectionListener != null)
mRemoteConnectionListener.onRemoteConnectionDidConnect();
Expand Down
42 changes: 26 additions & 16 deletions src/main/java/com/jaguarlandrover/rvi/DlinkPacketParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ interface DlinkPacketParserListener
* @param packet the dlink packet
*/
void onPacketParsed(DlinkPacket packet);

/**
* Failed when trying to parse the packet. Callback method that notifies listener when a dlink packet was unable to
* be parsed out of the input stream coming from an rvi node over the network.
*
* @param error the error
*/
void onPacketFailedToParse(Throwable error);
}

/**
Expand Down Expand Up @@ -110,26 +118,28 @@ private DlinkPacket stringToPacket(String string) {

try {
packet = gson.fromJson(string, DlinkPacket.class);
} catch (Exception e) {
e.printStackTrace();
return null;
}

if (mDataParserListener instanceof DlinkPacketParserTestCaseListener)
((DlinkPacketParserTestCaseListener) mDataParserListener).onJsonObjectParsed(packet);
if (mDataParserListener instanceof DlinkPacketParserTestCaseListener)
((DlinkPacketParserTestCaseListener) mDataParserListener).onJsonObjectParsed(packet);

DlinkPacket.Command command = packet.mCmd;
DlinkPacket.Command command = packet.mCmd;

if (command == null)
return null;
if (command == null)
return null;

if (command == DlinkPacket.Command.AUTHORIZE) {
return gson.fromJson(string, DlinkAuthPacket.class);
} else if (command == DlinkPacket.Command.SERVICE_ANNOUNCE) {
return gson.fromJson(string, DlinkServiceAnnouncePacket.class);
} else if (command == DlinkPacket.Command.RECEIVE) {
return gson.fromJson(string, DlinkReceivePacket.class);
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
mDataParserListener.onPacketFailedToParse(e);

if (command == DlinkPacket.Command.AUTHORIZE) {
return gson.fromJson(string, DlinkAuthPacket.class);
} else if (command == DlinkPacket.Command.SERVICE_ANNOUNCE) {
return gson.fromJson(string, DlinkServiceAnnouncePacket.class);
} else if (command == DlinkPacket.Command.RECEIVE) {
return gson.fromJson(string, DlinkReceivePacket.class);
} else {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ class DlinkReceivePacket extends DlinkPacket
super(Command.RECEIVE);

mMod = "proto_json_rpc";
mService = service;


// TODO: With this paradigm, if one of the parameters of mService changes, mData string will still be the same.
//mData = mService.jsonString();//Base64.encodeToString(mService.jsonString().getBytes(), Base64.DEFAULT);
mService = service.copy();
}

// public DlinkReceivePacket(HashMap jsonHash) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/jaguarlandrover/rvi/RVINode.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public void onRVIDidReceivePacket(DlinkPacket packet) {
}
}

@Override
public void onRVIDidFailToReceivePacket(Throwable error) {
Log.d(TAG, Util.getMethodName() + ": " + ((error == null) ? "(null)" : error.getLocalizedMessage()));
}

@Override
public void onRVIDidSendPacket(DlinkPacket packet) {
if (packet == null) return;
Expand Down Expand Up @@ -153,7 +158,6 @@ public void setServerPort(Integer serverPort) {
mRemoteConnectionManager.setServerPort(serverPort);
}


/**
* Method to pass the SDK your app's JWT-encoded json credentials for invoking services on a remote node and receiving service invocations from a remote node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public enum ConnectionType
public void onPacketParsed(DlinkPacket packet) {
if (mListener != null) mListener.onRVIDidReceivePacket(packet);
}

@Override
public void onPacketFailedToParse(Throwable error) {
if (mListener != null) mListener.onRVIDidFailToReceivePacket(error);
}
});

RemoteConnectionInterface.RemoteConnectionListener connectionListener = new RemoteConnectionInterface.RemoteConnectionListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ interface RemoteConnectionManagerListener // TODO: Get rid of this middle man
*/
void onRVIDidReceivePacket(DlinkPacket packet);

/**
* RVI did receive packet but there was an error with the packet.
*
* @param error the error
*/
void onRVIDidFailToReceivePacket(Throwable error);

/**
* On RVI did send packet.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/jaguarlandrover/rvi/ServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void sendRviRequest(DlinkPacket dlinkPacket) {
return;
}

new SendDataTask(dlinkPacket).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);//, dlinkPacket.toJsonString());
new SendDataTask(dlinkPacket).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);//, dlinkPacket.toJsonString());
}

@Override
Expand Down Expand Up @@ -277,7 +277,7 @@ protected Throwable doInBackground(Void... params) {

wr.writeBytes(data);
wr.flush();
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();

return e;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/jaguarlandrover/rvi/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ boolean hasNodeIdentifier() {
return mNodeIdentifier != null;
}

private String getNodeIdentifier() {
return mNodeIdentifier;
}

/**
* Sets the node identifier portion of the fully-qualified service name
*
Expand Down Expand Up @@ -219,4 +223,13 @@ Long getTimeout() {
void setTimeout(Long timeout) {
mTimeout = timeout;
}

public Service copy() {
Service copy = new Service(this.getServiceIdentifier(), this.getDomain(), this.getBundleIdentifier(), this.getNodeIdentifier());

copy.setTimeout(this.getTimeout());
copy.setParameters(this.getParameters());

return copy;
}
}
28 changes: 21 additions & 7 deletions src/main/java/com/jaguarlandrover/rvi/ServiceBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;


Expand All @@ -37,7 +38,7 @@ public class ServiceBundle

private HashMap<String, Service> mRemoteServices = new HashMap<>();

private HashMap<String, Service> mPendingServiceInvocations = new HashMap<>();
private HashMap<String, ArrayList<Service>> mPendingServiceInvocations = new HashMap<>();
private RVINode mNode;

/**
Expand Down Expand Up @@ -174,11 +175,14 @@ void addRemoteService(String serviceIdentifier, String remoteNodeIdentifier) {
if (!mRemoteServices.containsKey(serviceIdentifier))
mRemoteServices.put(serviceIdentifier, new Service(serviceIdentifier, mDomain, mBundleIdentifier, remoteNodeIdentifier));

Service pendingServiceInvocation = mPendingServiceInvocations.get(serviceIdentifier);
if (pendingServiceInvocation != null) {
if (pendingServiceInvocation.getTimeout() >= System.currentTimeMillis()) {
pendingServiceInvocation.setNodeIdentifier(remoteNodeIdentifier);
mNode.invokeService(pendingServiceInvocation);
ArrayList<Service> pendingServiceInvocationList = mPendingServiceInvocations.get(serviceIdentifier);

if (pendingServiceInvocationList != null) {
for (Service pendingServiceInvocation : pendingServiceInvocationList) {
if (pendingServiceInvocation.getTimeout() >= System.currentTimeMillis()) {
pendingServiceInvocation.setNodeIdentifier(remoteNodeIdentifier);
mNode.invokeService(pendingServiceInvocation);
}
}

mPendingServiceInvocations.remove(serviceIdentifier);
Expand All @@ -200,6 +204,16 @@ void removeAllRemoteServices() {
mRemoteServices.clear();
}

private void queueServiceInvocation(String serviceIdentifier, Service service) {

ArrayList<Service> pendingServiceInvocationList = mPendingServiceInvocations.get(serviceIdentifier);
if (pendingServiceInvocationList != null) {
pendingServiceInvocationList.add(service.copy());
} else {
mPendingServiceInvocations.put(serviceIdentifier, new ArrayList<>(Arrays.asList(service.copy())));
}
}

/**
* Invoke/update a remote service on the remote RVI node
*
Expand All @@ -216,7 +230,7 @@ public void invokeService(String serviceIdentifier, Object parameters, Integer t
if (service.hasNodeIdentifier() && mNode != null) // TODO: Check the logic here
mNode.invokeService(service);
else
mPendingServiceInvocations.put(serviceIdentifier, service);
queueServiceInvocation(serviceIdentifier, service);
}

/**
Expand Down

0 comments on commit 7e74045

Please sign in to comment.