From: rtrimana <rtrimana@uci.edu>
Date: Wed, 23 Nov 2016 17:51:49 +0000 (-0800)
Subject: Fixing minor bugs in callback generation
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d0e9539bcff8773d0a7581c5e3c1894ab0c3d585;p=iot2.git

Fixing minor bugs in callback generation
---

diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java
index e8abdfa..3908962 100644
--- a/iotjava/iotpolicy/IoTCompiler.java
+++ b/iotjava/iotpolicy/IoTCompiler.java
@@ -1864,6 +1864,37 @@ public class IoTCompiler {
 	}
 
 
+	/**
+	 * HELPER: writeMethodHelperStructSetupJavaCallbackSkeleton() writes the method helper of the struct in skeleton class
+	 */
+	private void writeMethodHelperStructSetupJavaCallbackSkeleton(Collection<String> methods, 
+			InterfaceDecl intDecl) {
+
+		// Use this set to handle two same methodIds
+		for (String method : methods) {
+
+			List<String> methParams = intDecl.getMethodParams(method);
+			List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+			// Check for params with structs
+			for (int i = 0; i < methParams.size(); i++) {
+				String paramType = methPrmTypes.get(i);
+				String param = methParams.get(i);
+				String simpleType = getSimpleType(paramType);
+				if (isStructClass(simpleType)) {
+					int methodNumId = intDecl.getMethodNumId(method);
+					print("public int ___");
+					String helperMethod = methodNumId + "struct" + i;
+					println(helperMethod + "(IoTRMIObject rmiObj) {");
+					// Now, write the helper body of skeleton!
+					println("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null });");
+					println("return (int) paramObj[0];");
+					println("}\n");
+				}
+			}
+		}
+	}
+
+
 	/**
 	 * HELPER: writeCountVarStructJavaSkeleton() writes counter variable of struct for skeleton
 	 */
@@ -1891,10 +1922,11 @@ public class IoTCompiler {
 	/**
 	 * HELPER: writeInputCountVarStructJavaSkeleton() writes counter variable of struct for skeleton
 	 */
-	private void writeInputCountVarStructJavaSkeleton(String method, InterfaceDecl intDecl) {
+	private boolean writeInputCountVarStructJavaSkeleton(String method, InterfaceDecl intDecl) {
 
 		List<String> methParams = intDecl.getMethodParams(method);
 		List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+		boolean structExist = false;
 		// Check for params with structs
 		for (int i = 0; i < methParams.size(); i++) {
 			String paramType = methPrmTypes.get(i);
@@ -1902,6 +1934,7 @@ public class IoTCompiler {
 			String simpleType = getSimpleType(paramType);
 			boolean begin = true;
 			if (isStructClass(simpleType)) {
+				structExist = true;
 				if (!begin) {
 					print(", "); begin = false;
 				}
@@ -1909,6 +1942,7 @@ public class IoTCompiler {
 				print("struct" + methodNumId + "Size" + i);
 			}
 		}
+		return structExist;
 	}
 
 
@@ -1941,6 +1975,35 @@ public class IoTCompiler {
 	}
 
 
+	/**
+	 * HELPER: writeMethodCallStructJavaCallbackSkeleton() writes method call for wait invoke in skeleton
+	 */
+	private void writeMethodCallStructJavaCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
+
+		// Use this set to handle two same methodIds
+		for (String method : methods) {
+
+			List<String> methParams = intDecl.getMethodParams(method);
+			List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+			// Check for params with structs
+			for (int i = 0; i < methParams.size(); i++) {
+				String paramType = methPrmTypes.get(i);
+				String param = methParams.get(i);
+				String simpleType = getSimpleType(paramType);
+				if (isStructClass(simpleType)) {
+					int methodNumId = intDecl.getMethodNumId(method);
+					print("case ");
+					String helperMethod = methodNumId + "struct" + i;
+					String tempVar = "struct" + methodNumId + "Size" + i;
+					print(intDecl.getHelperMethodNumId(helperMethod) + ": ");
+					print(tempVar + " = ___");
+					println(helperMethod + "(rmiObj); break;");
+				}
+			}
+		}
+	}
+
+
 	/**
 	 * HELPER: writeJavaMethodPermission() writes permission checks in skeleton
 	 */
@@ -2092,23 +2155,58 @@ public class IoTCompiler {
 
 			List<String> methParams = intDecl.getMethodParams(method);
 			List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
-			String methodId = intDecl.getMethodId(method);
-			print("public void ___");
-			String helperMethod = methodId;
-			if (uniqueMethodIds.contains(methodId))
-				helperMethod = helperMethod + intDecl.getMethodNumId(method);
-			else
-				uniqueMethodIds.add(methodId);
-			// Check if this is "void"
-			String retType = intDecl.getMethodType(method);
-			if (retType.equals("void"))
-				println(helperMethod + "(IoTRMIObject rmiObj) {");
-			else
-				println(helperMethod + "(IoTRMIObject rmiObj) throws IOException {");
-			// Now, write the helper body of skeleton!
-			writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
-			println("}\n");
+			if (isStructPresent(methParams, methPrmTypes)) {	// Treat struct differently
+				String methodId = intDecl.getMethodId(method);
+				print("public void ___");
+				String helperMethod = methodId;
+				if (uniqueMethodIds.contains(methodId))
+					helperMethod = helperMethod + intDecl.getMethodNumId(method);
+				else
+					uniqueMethodIds.add(methodId);
+				String retType = intDecl.getMethodType(method);
+				print(helperMethod + "(");
+				boolean begin = true;
+				for (int i = 0; i < methParams.size(); i++) { // Print size variables
+					String paramType = methPrmTypes.get(i);
+					String param = methParams.get(i);
+					String simpleType = getSimpleType(paramType);
+					if (isStructClass(simpleType)) {
+						if (!begin) {	// Generate comma for not the beginning variable
+							print(", "); begin = false;
+						}
+						int methodNumId = intDecl.getMethodNumId(method);
+						print("int struct" + methodNumId + "Size" + i);
+					}
+					// TODO: Need to create comma separation
+				}
+				// Check if this is "void"
+				if (retType.equals("void"))
+					println(", IoTRMIObject rmiObj) {");
+				else
+					println(", IoTRMIObject rmiObj) throws IOException {");
+				writeMethodHelperStructJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
+				println("}\n");
+			} else {
+				String methodId = intDecl.getMethodId(method);
+				print("public void ___");
+				String helperMethod = methodId;
+				if (uniqueMethodIds.contains(methodId))
+					helperMethod = helperMethod + intDecl.getMethodNumId(method);
+				else
+					uniqueMethodIds.add(methodId);
+				// Check if this is "void"
+				String retType = intDecl.getMethodType(method);
+				if (retType.equals("void"))
+					println(helperMethod + "(IoTRMIObject rmiObj) {");
+				else
+					println(helperMethod + "(IoTRMIObject rmiObj) throws IOException {");
+				// Now, write the helper body of skeleton!
+				writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
+				println("}\n");
+			}
 		}
+		// Write method helper for structs
+		writeMethodHelperStructSetupJavaCallbackSkeleton(methods, intDecl);
 	}
 
 
@@ -2121,6 +2219,8 @@ public class IoTCompiler {
 		Set<String> uniqueMethodIds = new HashSet<String>();
 		println("public void invokeMethod(IoTRMIObject rmiObj) throws IOException {");
 		// Write variables here if we have callbacks or enums or structs
+		writeCountVarStructJavaSkeleton(methods, intDecl);
+		// Write variables here if we have callbacks or enums or structs
 		println("int methodId = rmiObj.getMethodId();");
 		// TODO: code the permission check here!
 		println("switch (methodId) {");
@@ -2134,7 +2234,11 @@ public class IoTCompiler {
 				helperMethod = helperMethod + methodNumId;
 			else
 				uniqueMethodIds.add(methodId);
-			println(helperMethod + "(rmiObj); break;");
+			print(helperMethod + "(");
+			if (writeInputCountVarStructJavaSkeleton(method, intDecl))
+				println(", rmiObj); break;");
+			else
+				println("rmiObj); break;");
 		}
 		String method = "___initCallBack()";
 		// Print case -9999 (callback handler) if callback exists
@@ -2142,6 +2246,7 @@ public class IoTCompiler {
 			int methodId = intDecl.getHelperMethodNumId(method);
 			println("case " + methodId + ": ___regCB(rmiObj); break;");
 		}
+		writeMethodCallStructJavaCallbackSkeleton(methods, intDecl);
 		println("default: ");
 		println("throw new Error(\"Method Id \" + methodId + \" not recognized!\");");
 		println("}");