diff options
author | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2019-03-04 16:55:17 -0500 |
---|---|---|
committer | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2019-03-04 16:55:17 -0500 |
commit | d3ee2002f9ec16a52b60a28912da10eaebec0d83 (patch) | |
tree | 907888dfcae57bb9f7af379188632a5ffc6df310 | |
parent | b1b707347a0d7209f5a94b21a203404880feb8dc (diff) | |
download | sdl_android-d3ee2002f9ec16a52b60a28912da10eaebec0d83.tar.gz |
Move RpcConverter to base and reuse it in both libraries
3 files changed, 28 insertions, 173 deletions
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java deleted file mode 100644 index 07b5ccc25..000000000 --- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java +++ /dev/null @@ -1,146 +0,0 @@ -package com.smartdevicelink.managers.lifecycle; - -import android.util.Log; -import com.smartdevicelink.marshal.JsonRPCMarshaller; -import com.smartdevicelink.protocol.ProtocolMessage; -import com.smartdevicelink.protocol.enums.FunctionID; -import com.smartdevicelink.proxy.RPCMessage; -import com.smartdevicelink.proxy.RPCStruct; -import com.smartdevicelink.util.DebugTool; -import com.smartdevicelink.util.Version; - -import java.lang.reflect.InvocationTargetException; -import java.util.Hashtable; -import java.util.Set; - -/** - * Converts a protocol message into an RPC. - * Built to reduce boiler plate code for each RPC added. - */ -public class RpcConverter { - - private static final String TAG = "RpcConverter"; - - private static final String RPC_PACKAGE = "com.smartdevicelink.proxy.rpc."; - private static final String RESPONSE_KEY = "Response"; - private static final String GENERIC_RESPONSE_STRING = FunctionID.GENERIC_RESPONSE.toString(); - - /** - * Extracts the RPC out of the payload of a given protocol message - * @param message protocolMessage that has the RPC in the payload - * @param protocolVersion RPC spec version that should be used to create RPC - * @return the extracted RPC - */ - public static RPCMessage extractRpc(ProtocolMessage message, Version protocolVersion){ - Hashtable<String, Object> tempTable = convertProtocolMessage(message, protocolVersion); - if(tempTable != null){ - try{ - return convertTableToRpc(tempTable); - }catch (Exception e){ - DebugTool.logError("Error converting RPC",e); - } - } - return null; - } - - static Hashtable<String, Object> convertProtocolMessage(ProtocolMessage message, Version protocolVersion){ - Hashtable<String, Object> hash = new Hashtable<>(); - if (protocolVersion!= null && protocolVersion.getMajor() > 1) { - - Hashtable<String, Object> hashTemp = new Hashtable<>(); - hashTemp.put(RPCMessage.KEY_CORRELATION_ID, message.getCorrID()); - if (message.getJsonSize() > 0) { - final Hashtable<String, Object> mhash = JsonRPCMarshaller.unmarshall(message.getData()); - if (mhash != null) { - hashTemp.put(RPCMessage.KEY_PARAMETERS, mhash); - } - } - - String functionName = FunctionID.getFunctionName(message.getFunctionID()); - - if (functionName != null) { - hashTemp.put(RPCMessage.KEY_FUNCTION_NAME, functionName); - } else { - DebugTool.logWarning("Dispatch Incoming Message - function name is null unknown RPC. FunctionId: " + message.getFunctionID()); - return null; - } - - if (message.getRPCType() == 0x00) { - hash.put(RPCMessage.KEY_REQUEST, hashTemp); - } else if (message.getRPCType() == 0x01) { - hash.put(RPCMessage.KEY_RESPONSE, hashTemp); - } else if (message.getRPCType() == 0x02) { - hash.put(RPCMessage.KEY_NOTIFICATION, hashTemp); - } - - if (message.getBulkData() != null) hash.put(RPCStruct.KEY_BULK_DATA, message.getBulkData()); - if (message.getPayloadProtected()) hash.put(RPCStruct.KEY_PROTECTED, true); - - return hash; - } else { - return JsonRPCMarshaller.unmarshall(message.getData()); - } - } - - - public static RPCMessage convertTableToRpc(Hashtable<String,Object> rpcHashTable){ - - Hashtable<String,Object> params; - if(rpcHashTable.containsKey((RPCMessage.KEY_RESPONSE))){ - params = (Hashtable)rpcHashTable.get((RPCMessage.KEY_RESPONSE)); - }else if(rpcHashTable.containsKey((RPCMessage.KEY_NOTIFICATION))){ - params = (Hashtable)rpcHashTable.get((RPCMessage.KEY_NOTIFICATION)); - }else if(rpcHashTable.containsKey((RPCMessage.KEY_REQUEST))){ - params = (Hashtable)rpcHashTable.get((RPCMessage.KEY_REQUEST)); - }else{ - DebugTool.logError(TAG + " Corrupted RPC table."); - return null; - } - - if(DebugTool.isDebugEnabled()) { - if (params != null) { - Set<String> keySet = params.keySet(); - for (String key : keySet) { - Log.i(TAG, key + " - " + params.get(key)); - } - } - } - - if(params.containsKey(RPCMessage.KEY_FUNCTION_NAME)){ - StringBuilder rpcClassName = new StringBuilder(); - String functionName = (String)params.get(RPCMessage.KEY_FUNCTION_NAME); - rpcClassName.append(RPC_PACKAGE); - rpcClassName.append (functionName); - - if(rpcHashTable.containsKey(RPCMessage.KEY_RESPONSE) - && !GENERIC_RESPONSE_STRING.equals(functionName)){ - rpcClassName.append(RESPONSE_KEY); - } - - DebugTool.logInfo(TAG + " Attempting to create " + rpcClassName.toString()); - try { - Class rpcClass = Class.forName(rpcClassName.toString()); - if(rpcClass != null){ - java.lang.reflect.Constructor rpcConstructor = rpcClass.getConstructor(Hashtable.class); - if(rpcConstructor != null){ - return (RPCMessage)rpcConstructor.newInstance(rpcHashTable); - } - } - } catch (ClassNotFoundException e) { - } catch (NoSuchMethodException e) { - } catch (IllegalAccessException e) { - } catch (InstantiationException e) { - } catch (InvocationTargetException e) { - } catch (ClassCastException e){ - - } - }else{ - DebugTool.logError(TAG + " Unable to parse into RPC"); - } - - return null; - } - - -} - diff --git a/base/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java b/base/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java index 020677924..07b5ccc25 100644 --- a/base/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java +++ b/base/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java @@ -13,6 +13,10 @@ import java.lang.reflect.InvocationTargetException; import java.util.Hashtable; import java.util.Set; +/** + * Converts a protocol message into an RPC. + * Built to reduce boiler plate code for each RPC added. + */ public class RpcConverter { private static final String TAG = "RpcConverter"; @@ -21,25 +25,25 @@ public class RpcConverter { private static final String RESPONSE_KEY = "Response"; private static final String GENERIC_RESPONSE_STRING = FunctionID.GENERIC_RESPONSE.toString(); + /** + * Extracts the RPC out of the payload of a given protocol message + * @param message protocolMessage that has the RPC in the payload + * @param protocolVersion RPC spec version that should be used to create RPC + * @return the extracted RPC + */ public static RPCMessage extractRpc(ProtocolMessage message, Version protocolVersion){ Hashtable<String, Object> tempTable = convertProtocolMessage(message, protocolVersion); if(tempTable != null){ try{ - RPCMessage message1 = convertTableToRpc(tempTable); - if(message1 != null){ - Log.v(TAG, "RPC type: " + message1.getClass().getCanonicalName()); - }else{ - Log.i(TAG, "Message was null"); - } - return message1; + return convertTableToRpc(tempTable); }catch (Exception e){ - e.printStackTrace(); + DebugTool.logError("Error converting RPC",e); } } return null; } - public static Hashtable<String, Object> convertProtocolMessage(ProtocolMessage message, Version protocolVersion){ + static Hashtable<String, Object> convertProtocolMessage(ProtocolMessage message, Version protocolVersion){ Hashtable<String, Object> hash = new Hashtable<>(); if (protocolVersion!= null && protocolVersion.getMajor() > 1) { @@ -47,16 +51,12 @@ public class RpcConverter { hashTemp.put(RPCMessage.KEY_CORRELATION_ID, message.getCorrID()); if (message.getJsonSize() > 0) { final Hashtable<String, Object> mhash = JsonRPCMarshaller.unmarshall(message.getData()); - //hashTemp.put(Names.parameters, mhash.get(Names.parameters)); if (mhash != null) { hashTemp.put(RPCMessage.KEY_PARAMETERS, mhash); } } - // Log.d(TAG, "Function id: " + message.getFunctionID()); - String functionName = FunctionID.getFunctionName(message.getFunctionID()); - Log.d(TAG, "Function NAME: " + functionName); if (functionName != null) { hashTemp.put(RPCMessage.KEY_FUNCTION_NAME, functionName); @@ -64,6 +64,7 @@ public class RpcConverter { DebugTool.logWarning("Dispatch Incoming Message - function name is null unknown RPC. FunctionId: " + message.getFunctionID()); return null; } + if (message.getRPCType() == 0x00) { hash.put(RPCMessage.KEY_REQUEST, hashTemp); } else if (message.getRPCType() == 0x01) { @@ -71,6 +72,7 @@ public class RpcConverter { } else if (message.getRPCType() == 0x02) { hash.put(RPCMessage.KEY_NOTIFICATION, hashTemp); } + if (message.getBulkData() != null) hash.put(RPCStruct.KEY_BULK_DATA, message.getBulkData()); if (message.getPayloadProtected()) hash.put(RPCStruct.KEY_PROTECTED, true); @@ -91,15 +93,17 @@ public class RpcConverter { }else if(rpcHashTable.containsKey((RPCMessage.KEY_REQUEST))){ params = (Hashtable)rpcHashTable.get((RPCMessage.KEY_REQUEST)); }else{ - Log.e(TAG, "Corrupted RPC table."); + DebugTool.logError(TAG + " Corrupted RPC table."); return null; } - if(params != null){ - Set<String> keySet = params.keySet(); - for(String key: keySet){ - Log.i(TAG, key + " - " + params.get(key) ); - } + if(DebugTool.isDebugEnabled()) { + if (params != null) { + Set<String> keySet = params.keySet(); + for (String key : keySet) { + Log.i(TAG, key + " - " + params.get(key)); + } + } } if(params.containsKey(RPCMessage.KEY_FUNCTION_NAME)){ @@ -113,7 +117,7 @@ public class RpcConverter { rpcClassName.append(RESPONSE_KEY); } - Log.v(TAG, "Attempting to create " + rpcClassName.toString()); + DebugTool.logInfo(TAG + " Attempting to create " + rpcClassName.toString()); try { Class rpcClass = Class.forName(rpcClassName.toString()); if(rpcClass != null){ @@ -123,20 +127,15 @@ public class RpcConverter { } } } catch (ClassNotFoundException e) { - e.printStackTrace(); } catch (NoSuchMethodException e) { - e.printStackTrace(); } catch (IllegalAccessException e) { - e.printStackTrace(); } catch (InstantiationException e) { - e.printStackTrace(); } catch (InvocationTargetException e) { - e.printStackTrace(); } catch (ClassCastException e){ - e.printStackTrace(); + } }else{ - Log.w(TAG, "Unable to parse into RPC"); + DebugTool.logError(TAG + " Unable to parse into RPC"); } return null; @@ -144,3 +143,4 @@ public class RpcConverter { } + diff --git a/base_android/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java b/base_android/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java new file mode 120000 index 000000000..88f7feccc --- /dev/null +++ b/base_android/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java @@ -0,0 +1 @@ +../../../../../../../../base/src/main/java/com/smartdevicelink/managers/lifecycle/RpcConverter.java
\ No newline at end of file |