3 import java_cup.runtime.ComplexSymbolFactory;
4 import java_cup.runtime.ScannerBuffer;
6 import java.util.Arrays;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.List;
17 import iotpolicy.parser.Lexer;
18 import iotpolicy.parser.Parser;
19 import iotpolicy.tree.ParseNode;
20 import iotpolicy.tree.ParseNodeVector;
21 import iotpolicy.tree.ParseTreeHandler;
22 import iotpolicy.tree.Declaration;
23 import iotpolicy.tree.DeclarationHandler;
24 import iotpolicy.tree.CapabilityDecl;
25 import iotpolicy.tree.InterfaceDecl;
26 import iotpolicy.tree.RequiresDecl;
27 import iotpolicy.tree.EnumDecl;
28 import iotpolicy.tree.StructDecl;
30 import iotrmi.Java.IoTRMITypes;
33 /** Class IoTCompiler is the main interface/stub compiler for
34 * files generation. This class calls helper classes
35 * such as Parser, Lexer, InterfaceDecl, CapabilityDecl,
36 * RequiresDecl, ParseTreeHandler, etc.
38 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
42 public class IoTCompiler {
47 // Maps multiple interfaces to multiple objects of ParseTreeHandler
48 private Map<String,ParseTreeHandler> mapIntfacePTH;
49 private Map<String,DeclarationHandler> mapIntDeclHand;
50 private Map<String,Map<String,Set<String>>> mapInt2NewInts;
51 // Data structure to store our types (primitives and non-primitives) for compilation
52 private Map<String,String> mapPrimitives;
53 private Map<String,String> mapNonPrimitivesJava;
54 private Map<String,String> mapNonPrimitivesCplus;
55 // Other data structures
56 private Map<String,Integer> mapIntfaceObjId; // Maps interface name to object Id
57 private Map<String,Integer> mapNewIntfaceObjId; // Maps new interface name to its object Id (keep track of stubs)
58 private PrintWriter pw;
60 private String subdir;
66 private final static String OUTPUT_DIRECTORY = "output_files";
68 private enum ParamCategory {
70 PRIMITIVES, // All the primitive types, e.g. byte, short, int, long, etc.
71 NONPRIMITIVES, // Non-primitive types, e.g. Set, Map, List, etc.
73 STRUCT, // Struct type
74 USERDEFINED // Assumed as driver classes
81 public IoTCompiler() {
83 mapIntfacePTH = new HashMap<String,ParseTreeHandler>();
84 mapIntDeclHand = new HashMap<String,DeclarationHandler>();
85 mapInt2NewInts = new HashMap<String,Map<String,Set<String>>>();
86 mapIntfaceObjId = new HashMap<String,Integer>();
87 mapNewIntfaceObjId = new HashMap<String,Integer>();
88 mapPrimitives = new HashMap<String,String>();
89 arraysToMap(mapPrimitives, IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
90 mapNonPrimitivesJava = new HashMap<String,String>();
91 arraysToMap(mapNonPrimitivesJava, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitiveJavaLibs);
92 mapNonPrimitivesCplus = new HashMap<String,String>();
93 arraysToMap(mapNonPrimitivesCplus, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
95 dir = OUTPUT_DIRECTORY;
101 * setDataStructures() sets parse tree and other data structures based on policy files.
103 * It also generates parse tree (ParseTreeHandler) and
104 * copies useful information from parse tree into
105 * InterfaceDecl, CapabilityDecl, and RequiresDecl
107 * Additionally, the data structure handles are
108 * returned from tree-parsing for further process.
110 public void setDataStructures(String origInt, ParseNode pnPol, ParseNode pnReq) {
112 ParseTreeHandler ptHandler = new ParseTreeHandler(origInt, pnPol, pnReq);
113 DeclarationHandler decHandler = new DeclarationHandler();
114 // Process ParseNode and generate Declaration objects
116 ptHandler.processInterfaceDecl();
117 InterfaceDecl intDecl = ptHandler.getInterfaceDecl();
118 decHandler.addInterfaceDecl(origInt, intDecl);
120 ptHandler.processCapabilityDecl();
121 CapabilityDecl capDecl = ptHandler.getCapabilityDecl();
122 decHandler.addCapabilityDecl(origInt, capDecl);
124 ptHandler.processRequiresDecl();
125 RequiresDecl reqDecl = ptHandler.getRequiresDecl();
126 decHandler.addRequiresDecl(origInt, reqDecl);
128 ptHandler.processEnumDecl();
129 EnumDecl enumDecl = ptHandler.getEnumDecl();
130 decHandler.addEnumDecl(origInt, enumDecl);
132 ptHandler.processStructDecl();
133 StructDecl structDecl = ptHandler.getStructDecl();
134 decHandler.addStructDecl(origInt, structDecl);
136 mapIntfacePTH.put(origInt, ptHandler);
137 mapIntDeclHand.put(origInt, decHandler);
138 // Set object Id counter to 0 for each interface
139 mapIntfaceObjId.put(origInt, new Integer(0));
144 * getMethodsForIntface() reads for methods in the data structure
146 * It is going to give list of methods for a certain interface
147 * based on the declaration of capabilities.
149 public void getMethodsForIntface(String origInt) {
151 ParseTreeHandler ptHandler = mapIntfacePTH.get(origInt);
152 Map<String,Set<String>> mapNewIntMethods = new HashMap<String,Set<String>>();
153 // Get set of new interfaces, e.g. CameraWithCaptureAndData
154 // Generate this new interface with all the methods it needs
155 // from different capabilities it declares
156 DeclarationHandler decHandler = mapIntDeclHand.get(origInt);
157 RequiresDecl reqDecl = (RequiresDecl) decHandler.getRequiresDecl(origInt);
158 Set<String> setIntfaces = reqDecl.getInterfaces();
159 for (String strInt : setIntfaces) {
161 // Initialize a set of methods
162 Set<String> setMethods = new HashSet<String>();
163 // Get list of capabilities, e.g. ImageCapture, VideoRecording, etc.
164 List<String> listCapab = reqDecl.getCapabList(strInt);
165 for (String strCap : listCapab) {
167 // Get list of methods for each capability
168 CapabilityDecl capDecl = (CapabilityDecl) decHandler.getCapabilityDecl(origInt);
169 List<String> listCapabMeth = capDecl.getMethods(strCap);
170 for (String strMeth : listCapabMeth) {
172 // Add methods into setMethods
173 // This is to also handle redundancies (say two capabilities
174 // share the same methods)
175 setMethods.add(strMeth);
178 // Add interface and methods information into map
179 mapNewIntMethods.put(strInt, setMethods);
181 // Map the map of interface-methods to the original interface
182 mapInt2NewInts.put(origInt, mapNewIntMethods);
187 * HELPER: writeMethodJavaLocalInterface() writes the method of the local interface
189 private void writeMethodJavaLocalInterface(Collection<String> methods, InterfaceDecl intDecl) {
191 for (String method : methods) {
193 List<String> methParams = intDecl.getMethodParams(method);
194 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
195 print("public " + intDecl.getMethodType(method) + " " +
196 intDecl.getMethodId(method) + "(");
197 for (int i = 0; i < methParams.size(); i++) {
198 // Check for params with driver class types and exchange it
199 // with its remote interface
200 String paramType = checkAndGetParamClass(methPrmTypes.get(i));
201 print(paramType + " " + methParams.get(i));
202 // Check if this is the last element (don't print a comma)
203 if (i != methParams.size() - 1) {
213 * HELPER: writeMethodJavaInterface() writes the method of the interface
215 private void writeMethodJavaInterface(Collection<String> methods, InterfaceDecl intDecl) {
217 for (String method : methods) {
219 List<String> methParams = intDecl.getMethodParams(method);
220 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
221 print("public " + intDecl.getMethodType(method) + " " +
222 intDecl.getMethodId(method) + "(");
223 for (int i = 0; i < methParams.size(); i++) {
224 // Check for params with driver class types and exchange it
225 // with its remote interface
226 String paramType = methPrmTypes.get(i);
227 print(paramType + " " + methParams.get(i));
228 // Check if this is the last element (don't print a comma)
229 if (i != methParams.size() - 1) {
239 * HELPER: generateEnumJava() writes the enumeration declaration
241 private void generateEnumJava() throws IOException {
243 // Create a new directory
244 createDirectory(dir);
245 for (String intface : mapIntfacePTH.keySet()) {
246 // Get the right EnumDecl
247 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
248 EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
249 Set<String> enumTypes = enumDecl.getEnumDeclarations();
250 // Iterate over enum declarations
251 for (String enType : enumTypes) {
252 // Open a new file to write into
253 FileWriter fw = new FileWriter(dir + "/" + enType + ".java");
254 pw = new PrintWriter(new BufferedWriter(fw));
255 println("public enum " + enType + " {");
256 List<String> enumMembers = enumDecl.getMembers(enType);
257 for (int i = 0; i < enumMembers.size(); i++) {
259 String member = enumMembers.get(i);
261 // Check if this is the last element (don't print a comma)
262 if (i != enumMembers.size() - 1)
269 System.out.println("IoTCompiler: Generated enum class " + enType + ".java...");
276 * HELPER: generateStructJava() writes the struct declaration
278 private void generateStructJava() throws IOException {
280 // Create a new directory
281 createDirectory(dir);
282 for (String intface : mapIntfacePTH.keySet()) {
283 // Get the right StructDecl
284 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
285 StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
286 List<String> structTypes = structDecl.getStructTypes();
287 // Iterate over enum declarations
288 for (String stType : structTypes) {
289 // Open a new file to write into
290 FileWriter fw = new FileWriter(dir + "/" + stType + ".java");
291 pw = new PrintWriter(new BufferedWriter(fw));
292 println("public class " + stType + " {");
293 List<String> structMemberTypes = structDecl.getMemberTypes(stType);
294 List<String> structMembers = structDecl.getMembers(stType);
295 for (int i = 0; i < structMembers.size(); i++) {
297 String memberType = structMemberTypes.get(i);
298 String member = structMembers.get(i);
299 println("public static " + memberType + " " + member + ";");
303 System.out.println("IoTCompiler: Generated struct class " + stType + ".java...");
310 * generateJavaLocalInterface() writes the local interface and provides type-checking.
312 * It needs to rewrite and exchange USERDEFINED types in input parameters of stub
313 * and original interfaces, e.g. exchange Camera and CameraWithVideoAndRecording.
314 * The local interface has to be the input parameter for the stub and the stub
315 * interface has to be the input parameter for the local class.
317 public void generateJavaLocalInterfaces() throws IOException {
319 // Create a new directory
320 createDirectory(dir);
321 for (String intface : mapIntfacePTH.keySet()) {
322 // Open a new file to write into
323 FileWriter fw = new FileWriter(dir + "/" + intface + ".java");
324 pw = new PrintWriter(new BufferedWriter(fw));
325 // Pass in set of methods and get import classes
326 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
327 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
328 List<String> methods = intDecl.getMethods();
329 Set<String> importClasses = getImportClasses(methods, intDecl);
330 List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
331 List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
332 printImportStatements(allImportClasses);
333 // Write interface header
335 println("public interface " + intface + " {");
337 writeMethodJavaLocalInterface(methods, intDecl);
340 System.out.println("IoTCompiler: Generated local interface " + intface + ".java...");
346 * generateJavaInterfaces() generate stub interfaces based on the methods list in Java
348 public void generateJavaInterfaces() throws IOException {
350 // Create a new directory
351 String path = createDirectories(dir, subdir);
352 for (String intface : mapIntfacePTH.keySet()) {
354 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
355 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
357 // Open a new file to write into
358 String newIntface = intMeth.getKey();
359 FileWriter fw = new FileWriter(path + "/" + newIntface + ".java");
360 pw = new PrintWriter(new BufferedWriter(fw));
361 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
362 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
363 // Pass in set of methods and get import classes
364 List<String> methods = intDecl.getMethods();
365 Set<String> importClasses = getImportClasses(methods, intDecl);
366 List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
367 List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
368 printImportStatements(allImportClasses);
369 // Write interface header
371 println("public interface " + newIntface + " {\n");
373 writeMethodJavaInterface(methods, intDecl);
376 System.out.println("IoTCompiler: Generated interface " + newIntface + ".java...");
383 * HELPER: writePropertiesJavaPermission() writes the permission in properties
385 private void writePropertiesJavaPermission(String intface, InterfaceDecl intDecl) {
387 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
388 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
389 String newIntface = intMeth.getKey();
390 int newObjectId = getNewIntfaceObjectId(newIntface);
391 println("private final static int object" + newObjectId + "Id = " +
392 newObjectId + ";\t//" + newIntface);
393 Set<String> methodIds = intMeth.getValue();
394 print("private static Integer[] object" + newObjectId + "Permission = { ");
396 for (String methodId : methodIds) {
397 int methodNumId = intDecl.getMethodNumId(methodId);
398 print(Integer.toString(methodNumId));
399 // Check if this is the last element (don't print a comma)
400 if (i != methodIds.size() - 1) {
406 println("private static List<Integer> set" + newObjectId + "Allowed;");
412 * HELPER: writePropertiesJavaStub() writes the properties of the stub class
414 private void writePropertiesJavaStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
416 println("private IoTRMICall rmiCall;");
417 println("private String callbackAddress;");
418 println("private int[] ports;\n");
420 Integer objId = mapIntfaceObjId.get(intface);
421 println("private final static int objectId = " + objId + ";");
422 mapNewIntfaceObjId.put(newIntface, objId);
423 mapIntfaceObjId.put(intface, objId++);
425 // We assume that each class only has one callback interface for now
426 Iterator it = callbackClasses.iterator();
427 String callbackType = (String) it.next();
428 println("// Callback properties");
429 println("private IoTRMIObject rmiObj;");
430 println("List<" + callbackType + "> listCallbackObj;");
431 println("private static int objIdCnt = 0;");
432 // Generate permission stuff for callback stubs
433 DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
434 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
435 writePropertiesJavaPermission(callbackType, intDecl);
442 * HELPER: writeConstructorJavaPermission() writes the permission in constructor
444 private void writeConstructorJavaPermission(String intface) {
446 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
447 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
448 String newIntface = intMeth.getKey();
449 int newObjectId = getNewIntfaceObjectId(newIntface);
450 println("set" + newObjectId + "Allowed = new ArrayList<Integer>(Arrays.asList(object" + newObjectId +"Permission));");
456 * HELPER: writeConstructorJavaStub() writes the constructor of the stub class
458 private void writeConstructorJavaStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
460 println("public " + newStubClass + "(int _port, String _skeletonAddress, String _callbackAddress, int _rev, int[] _ports) throws Exception {");
461 println("callbackAddress = _callbackAddress;");
462 println("ports = _ports;");
463 println("rmiCall = new IoTRMICall(_port, _skeletonAddress, _rev);");
465 Iterator it = callbackClasses.iterator();
466 String callbackType = (String) it.next();
467 writeConstructorJavaPermission(intface);
468 println("listCallbackObj = new ArrayList<" + callbackType + ">();");
469 println("___initCallBack();");
476 * HELPER: writeJavaMethodCallbackPermission() writes permission checks in stub for callbacks
478 private void writeJavaMethodCallbackPermission(String intface) {
480 println("int methodId = IoTRMIObject.getMethodId(method);");
481 // Get all the different stubs
482 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
483 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
484 String newIntface = intMeth.getKey();
485 int newObjectId = getNewIntfaceObjectId(newIntface);
486 println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
487 println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);");
494 * HELPER: writeJavaInitCallbackPermission() writes the permission for callback
496 private void writeJavaInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) {
499 String method = "___initCallBack()";
500 int methodNumId = intDecl.getHelperMethodNumId(method);
501 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
502 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
503 String newIntface = intMeth.getKey();
504 int newObjectId = getNewIntfaceObjectId(newIntface);
505 println("set" + newObjectId + "Allowed.add(" + methodNumId + ");");
512 * HELPER: writeInitCallbackJavaStub() writes callback initialization in stub
514 private void writeInitCallbackJavaStub(String intface, InterfaceDecl intDecl) {
516 println("public void ___initCallBack() {");
517 // Generate main thread for callbacks
518 println("Thread thread = new Thread() {");
519 println("public void run() {");
521 println("rmiObj = new IoTRMIObject(ports[0]);");
522 println("while (true) {");
523 println("byte[] method = rmiObj.getMethodBytes();");
524 writeJavaMethodCallbackPermission(intface);
525 println("int objId = IoTRMIObject.getObjectId(method);");
526 println(intface + "_CallbackSkeleton skel = (" + intface + "_CallbackSkeleton) listCallbackObj.get(objId);");
527 println("if (skel != null) {");
528 println("skel.invokeMethod(rmiObj);");
531 println("throw new Error(\"" + intface + ": Object with Id \" + objId + \" not found!\");");
535 println(" catch (Exception ex) {");
536 println("ex.printStackTrace();");
537 println("throw new Error(\"Error instantiating class " + intface + "_CallbackSkeleton!\");");
541 println("thread.start();\n");
542 // Generate info sending part
543 String method = "___initCallBack()";
544 int methodNumId = intDecl.getHelperMethodNumId(method);
545 println("int methodId = " + methodNumId + ";");
546 println("Class<?> retType = void.class;");
547 println("Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };");
548 println("Object[] paramObj = new Object[] { ports[0], callbackAddress, 0 };");
549 println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
555 * HELPER: checkAndWriteEnumTypeJavaStub() writes the enum type (convert from enum to int)
557 private void checkAndWriteEnumTypeJavaStub(List<String> methParams, List<String> methPrmTypes) {
559 // Iterate and find enum declarations
560 for (int i = 0; i < methParams.size(); i++) {
561 String paramType = methPrmTypes.get(i);
562 String param = methParams.get(i);
563 String simpleType = getGenericType(paramType);
564 if (isEnumClass(simpleType)) {
565 // Check if this is enum type
566 if (isArray(param)) { // An array
567 println("int len" + i + " = " + getSimpleIdentifier(param) + ".length;");
568 println("int paramEnum" + i + "[] = new int[len" + i + "];");
569 println("for (int i = 0; i < len" + i + "; i++) {");
570 println("paramEnum" + i + "[i] = " + getSimpleIdentifier(param) + "[i].ordinal();");
572 } else if (isList(paramType)) { // A list
573 println("int len" + i + " = " + getSimpleIdentifier(param) + ".size();");
574 println("int paramEnum" + i + "[] = new int[len" + i + "];");
575 println("for (int i = 0; i < len" + i + "; i++) {");
576 println("paramEnum" + i + "[i] = " + getSimpleIdentifier(param) + ".get(i).ordinal();");
578 } else { // Just one element
579 println("int paramEnum" + i + "[] = new int[1];");
580 println("paramEnum" + i + "[0] = " + param + ".ordinal();");
588 * HELPER: checkAndWriteEnumRetTypeJavaStub() writes the enum return type (convert from enum to int)
590 private void checkAndWriteEnumRetTypeJavaStub(String retType) {
592 // Strips off array "[]" for return type
593 String pureType = getSimpleArrayType(getGenericType(retType));
594 // Take the inner type of generic
595 if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
596 pureType = getGenericType(retType);
597 if (isEnumClass(pureType)) {
598 // Check if this is enum type
600 println("int[] retEnum = (int[]) retObj;");
601 println(pureType + "[] enumVals = " + pureType + ".values();");
602 if (isArray(retType)) { // An array
603 println("int retLen = retEnum.length;");
604 println(pureType + "[] enumRetVal = new " + pureType + "[retLen];");
605 println("for (int i = 0; i < retLen; i++) {");
606 println("enumRetVal[i] = enumVals[retEnum[i]];");
608 } else if (isList(retType)) { // A list
609 println("int retLen = retEnum.length;");
610 println("List<" + pureType + "> enumRetVal = new ArrayList<" + pureType + ">();");
611 println("for (int i = 0; i < retLen; i++) {");
612 println("enumRetVal.add(enumVals[retEnum[i]]);");
614 } else { // Just one element
615 println(pureType + " enumRetVal = enumVals[retEnum[0]];");
617 println("return enumRetVal;");
623 * HELPER: checkAndWriteStructSetupJavaStub() writes the struct type setup
625 private void checkAndWriteStructSetupJavaStub(List<String> methParams, List<String> methPrmTypes,
626 InterfaceDecl intDecl, String method) {
628 // Iterate and find struct declarations
629 for (int i = 0; i < methParams.size(); i++) {
630 String paramType = methPrmTypes.get(i);
631 String param = methParams.get(i);
632 String simpleType = getGenericType(paramType);
633 if (isStructClass(simpleType)) {
634 // Check if this is enum type
635 int methodNumId = intDecl.getMethodNumId(method);
636 String helperMethod = methodNumId + "struct" + i;
637 println("int methodIdStruct" + i + " = " + intDecl.getHelperMethodNumId(helperMethod) + ";");
638 println("Class<?> retTypeStruct" + i + " = void.class;");
639 println("Class<?>[] paramClsStruct" + i + " = new Class<?>[] { int.class };");
640 if (isArray(param)) { // An array
641 println("Object[] paramObjStruct" + i + " = new Object[] { " + getSimpleArrayType(param) + ".length };");
642 } else if (isList(paramType)) { // A list
643 println("Object[] paramObjStruct" + i + " = new Object[] { " + getSimpleArrayType(param) + ".size() };");
644 } else { // Just one element
645 println("Object[] paramObjStruct" + i + " = new Object[] { new Integer(1) };");
647 println("rmiCall.remoteCall(objectId, methodIdStruct" + i +
648 ", retTypeStruct" + i + ", null, paramClsStruct" + i +
649 ", paramObjStruct" + i + ");\n");
656 * HELPER: isStructPresent() checks presence of struct
658 private boolean isStructPresent(List<String> methParams, List<String> methPrmTypes) {
660 // Iterate and find enum declarations
661 for (int i = 0; i < methParams.size(); i++) {
662 String paramType = methPrmTypes.get(i);
663 String param = methParams.get(i);
664 String simpleType = getGenericType(paramType);
665 if (isStructClass(simpleType))
673 * HELPER: writeLengthStructParamClassJavaStub() writes lengths of parameters
675 private void writeLengthStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes) {
677 // Iterate and find struct declarations - count number of params
678 for (int i = 0; i < methParams.size(); i++) {
679 String paramType = methPrmTypes.get(i);
680 String param = methParams.get(i);
681 String simpleType = getGenericType(paramType);
682 if (isStructClass(simpleType)) {
683 int members = getNumOfMembers(simpleType);
684 if (isArray(param)) { // An array
685 String structLen = getSimpleArrayType(param) + ".length";
686 print(members + "*" + structLen);
687 } else if (isList(paramType)) { // A list
688 String structLen = getSimpleArrayType(param) + ".size()";
689 print(members + "*" + structLen);
691 print(Integer.toString(members));
694 if (i != methParams.size() - 1) {
702 * HELPER: writeStructMembersJavaStub() writes parameters of struct
704 private void writeStructMembersJavaStub(String simpleType, String paramType, String param) {
706 // Get the struct declaration for this struct and generate initialization code
707 StructDecl structDecl = getStructDecl(simpleType);
708 List<String> memTypes = structDecl.getMemberTypes(simpleType);
709 List<String> members = structDecl.getMembers(simpleType);
710 if (isArray(param)) { // An array
711 println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".length; i++) {");
712 for (int i = 0; i < members.size(); i++) {
713 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
714 println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
715 print("paramObj[pos++] = " + getSimpleIdentifier(param) + "[i].");
716 print(getSimpleIdentifier(members.get(i)));
720 } else if (isList(paramType)) { // A list
721 println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".size(); i++) {");
722 for (int i = 0; i < members.size(); i++) {
723 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
724 println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
725 print("paramObj[pos++] = " + getSimpleIdentifier(param) + ".get(i).");
726 print(getSimpleIdentifier(members.get(i)));
730 } else { // Just one struct element
731 for (int i = 0; i < members.size(); i++) {
732 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
733 println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
734 print("paramObj[pos++] = " + getSimpleIdentifier(param) + ".");
735 print(getSimpleIdentifier(members.get(i)));
743 * HELPER: writeStructParamClassJavaStub() writes parameters if struct is present
745 private void writeStructParamClassJavaStub(List<String> methParams, List<String> methPrmTypes, String callbackType) {
747 print("int paramLen = ");
748 writeLengthStructParamClassJavaStub(methParams, methPrmTypes);
750 println("Object[] paramObj = new Object[paramLen];");
751 println("Class<?>[] paramCls = new Class<?>[paramLen];");
752 println("int pos = 0;");
753 // Iterate again over the parameters
754 for (int i = 0; i < methParams.size(); i++) {
755 String paramType = methPrmTypes.get(i);
756 String param = methParams.get(i);
757 String simpleType = getGenericType(paramType);
758 if (isStructClass(simpleType)) {
759 writeStructMembersJavaStub(simpleType, paramType, param);
760 } else if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
761 println("paramCls[pos] = int.class;");
762 print("paramObj[pos++] = ");
763 if (isArray(methParams.get(i)))
764 print(getSimpleIdentifier(methParams.get(i)) + ".length");
765 else if (isList(methPrmTypes.get(i)))
766 print(getSimpleIdentifier(methParams.get(i)) + ".size()");
768 print("new Integer(1)");
771 String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
772 println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
773 print("paramObj[pos++] = ");
774 print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
783 * HELPER: writeStructRetMembersJavaStub() writes parameters of struct for return statement
785 private void writeStructRetMembersJavaStub(String simpleType, String retType) {
787 // Get the struct declaration for this struct and generate initialization code
788 StructDecl structDecl = getStructDecl(simpleType);
789 List<String> memTypes = structDecl.getMemberTypes(simpleType);
790 List<String> members = structDecl.getMembers(simpleType);
791 if (isArrayOrList(retType, retType)) { // An array or list
792 println("for(int i = 0; i < retLen; i++) {");
794 if (isArray(retType)) { // An array
795 for (int i = 0; i < members.size(); i++) {
796 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
797 print("structRet[i]." + getSimpleIdentifier(members.get(i)));
798 println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];");
801 } else if (isList(retType)) { // A list
802 println(simpleType + " structRetMem = new " + simpleType + "();");
803 for (int i = 0; i < members.size(); i++) {
804 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
805 print("structRetMem." + getSimpleIdentifier(members.get(i)));
806 println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];");
808 println("structRet.add(structRetMem);");
810 } else { // Just one struct element
811 for (int i = 0; i < members.size(); i++) {
812 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
813 print("structRet." + getSimpleIdentifier(members.get(i)));
814 println(" = (" + getSimpleType(getEnumType(prmType)) + ") retObj[retObjPos++];");
817 println("return structRet;");
822 * HELPER: writeStructReturnJavaStub() writes parameters if struct is present for return statement
824 private void writeStructReturnJavaStub(String simpleType, String retType) {
826 // Handle the returned struct!!!
827 println("Object retLenObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
828 // Minimum retLen is 1 if this is a single struct object
829 println("int retLen = (int) retLenObj;");
830 int numMem = getNumOfMembers(simpleType);
831 println("Class<?>[] retCls = new Class<?>[" + numMem + "*retLen];");
832 println("Class<?>[] retClsVal = new Class<?>[" + numMem + "*retLen];");
833 println("int retPos = 0;");
834 // Get the struct declaration for this struct and generate initialization code
835 StructDecl structDecl = getStructDecl(simpleType);
836 List<String> memTypes = structDecl.getMemberTypes(simpleType);
837 List<String> members = structDecl.getMembers(simpleType);
838 if (isArrayOrList(retType, retType)) { // An array or list
839 println("for(int i = 0; i < retLen; i++) {");
840 for (int i = 0; i < members.size(); i++) {
841 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
842 println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
843 println("retClsVal[retPos++] = null;");
846 } else { // Just one struct element
847 for (int i = 0; i < members.size(); i++) {
848 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
849 println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
850 println("retClsVal[retPos++] = null;");
853 println("Object[] retObj = rmiCall.getStructObjects(retCls, retClsVal);");
854 if (isArray(retType)) { // An array
855 println(simpleType + "[] structRet = new " + simpleType + "[retLen];");
856 println("for(int i = 0; i < retLen; i++) {");
857 println("structRet[i] = new " + simpleType + "();");
859 } else if (isList(retType)) { // A list
860 println("List<" + simpleType + "> structRet = new ArrayList<" + simpleType + ">();");
862 println(simpleType + " structRet = new " + simpleType + "();");
863 println("int retObjPos = 0;");
864 writeStructRetMembersJavaStub(simpleType, retType);
869 * HELPER: writeStdMethodBodyJavaStub() writes the standard method body in the stub class
871 private void writeStdMethodBodyJavaStub(InterfaceDecl intDecl, List<String> methParams,
872 List<String> methPrmTypes, String method, String callbackType) {
874 checkAndWriteStructSetupJavaStub(methParams, methPrmTypes, intDecl, method);
875 println("int methodId = " + intDecl.getMethodNumId(method) + ";");
876 String retType = intDecl.getMethodType(method);
877 println("Class<?> retType = " + getSimpleType(getStructType(getEnumType(retType))) + ".class;");
878 checkAndWriteEnumTypeJavaStub(methParams, methPrmTypes);
879 // Generate array of parameter types
880 if (isStructPresent(methParams, methPrmTypes)) {
881 writeStructParamClassJavaStub(methParams, methPrmTypes, callbackType);
883 print("Class<?>[] paramCls = new Class<?>[] { ");
884 for (int i = 0; i < methParams.size(); i++) {
885 String prmType = methPrmTypes.get(i);
886 if (checkCallbackType(prmType, callbackType)) { // Check if this has callback object
888 } else { // Generate normal classes if it's not a callback object
889 String paramType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
890 print(getSimpleType(getEnumType(paramType)) + ".class");
892 // Check if this is the last element (don't print a comma)
893 if (i != methParams.size() - 1) {
898 // Generate array of parameter objects
899 print("Object[] paramObj = new Object[] { ");
900 for (int i = 0; i < methParams.size(); i++) {
901 String paramType = methPrmTypes.get(i);
902 if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
903 //if (isArray(methPrmTypes.get(i), methParams.get(i)))
904 if (isArray(methParams.get(i)))
905 print(getSimpleIdentifier(methParams.get(i)) + ".length");
906 else if (isList(methPrmTypes.get(i)))
907 print(getSimpleIdentifier(methParams.get(i)) + ".size()");
909 print("new Integer(1)");
911 print(getEnumParam(methPrmTypes.get(i), getSimpleIdentifier(methParams.get(i)), i));
912 // Check if this is the last element (don't print a comma)
913 if (i != methParams.size() - 1) {
919 // Check if this is "void"
920 if (retType.equals("void")) {
921 println("rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
922 } else { // We do have a return value
923 // Generate array of parameter types
924 if (isStructClass(getGenericType(getSimpleArrayType(retType)))) {
925 writeStructReturnJavaStub(getGenericType(getSimpleArrayType(retType)), retType);
927 // This is an enum type
928 if (getParamCategory(getGenericType(getSimpleArrayType(retType))) == ParamCategory.ENUM) {
929 println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
930 checkAndWriteEnumRetTypeJavaStub(retType);
931 } else if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES) {
932 // Check if the return value NONPRIMITIVES
933 String retGenValType = getGenericType(retType);
934 println("Class<?> retGenValType = " + retGenValType + ".class;");
935 println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, retGenValType, paramCls, paramObj);");
936 println("return (" + retType + ")retObj;");
938 println("Object retObj = rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);");
939 println("return (" + retType + ")retObj;");
947 * HELPER: returnGenericCallbackType() returns the callback type
949 private String returnGenericCallbackType(String paramType) {
951 if (getParamCategory(paramType) == ParamCategory.NONPRIMITIVES)
952 return getGenericType(paramType);
959 * HELPER: checkCallbackType() checks the callback type
961 private boolean checkCallbackType(String paramType, String callbackType) {
963 String prmType = returnGenericCallbackType(paramType);
964 if (callbackType == null) // If there is no callbackType it means not a callback method
967 return callbackType.equals(prmType);
972 * HELPER: writeCallbackMethodBodyJavaStub() writes the callback method of the stub class
974 private void writeCallbackMethodBodyJavaStub(InterfaceDecl intDecl, List<String> methParams,
975 List<String> methPrmTypes, String method, String callbackType) {
978 // Check if this is single object, array, or list of objects
979 for (int i = 0; i < methParams.size(); i++) {
980 String paramType = methPrmTypes.get(i);
981 if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
982 String param = methParams.get(i);
983 if (isArrayOrList(paramType, param)) { // Generate loop
984 println("for (" + getGenericType(paramType) + " cb : " + getSimpleIdentifier(param) + ") {");
985 println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(cb, objIdCnt++);");
987 println(callbackType + "_CallbackSkeleton skel" + i + " = new " + callbackType + "_CallbackSkeleton(" +
988 getSimpleIdentifier(param) + ", objIdCnt++);");
989 println("listCallbackObj.add(skel" + i + ");");
990 if (isArrayOrList(paramType, param))
995 println(" catch (Exception ex) {");
996 println("ex.printStackTrace();");
997 println("throw new Error(\"Exception when generating skeleton objects!\");");
1003 * HELPER: writeMethodJavaStub() writes the methods of the stub class
1005 private void writeMethodJavaStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
1007 boolean isDefined = false;
1008 for (String method : methods) {
1010 List<String> methParams = intDecl.getMethodParams(method);
1011 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1012 print("public " + intDecl.getMethodType(method) + " " +
1013 intDecl.getMethodId(method) + "(");
1014 boolean isCallbackMethod = false;
1015 String callbackType = null;
1016 for (int i = 0; i < methParams.size(); i++) {
1018 String paramType = returnGenericCallbackType(methPrmTypes.get(i));
1019 // Check if this has callback object
1020 if (callbackClasses.contains(paramType)) {
1021 isCallbackMethod = true;
1022 callbackType = paramType;
1023 // Even if there're 2 callback arguments, we expect them to be of the same interface
1025 print(methPrmTypes.get(i) + " " + methParams.get(i));
1026 // Check if this is the last element (don't print a comma)
1027 if (i != methParams.size() - 1) {
1032 // Now, write the body of stub!
1033 if (isCallbackMethod)
1034 writeCallbackMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
1036 writeStdMethodBodyJavaStub(intDecl, methParams, methPrmTypes, method, callbackType);
1038 // Write the init callback helper method
1039 if (isCallbackMethod && !isDefined) {
1040 writeInitCallbackJavaStub(callbackType, intDecl);
1048 * generateJavaStubClasses() generate stubs based on the methods list in Java
1050 public void generateJavaStubClasses() throws IOException {
1052 // Create a new directory
1053 String path = createDirectories(dir, subdir);
1054 for (String intface : mapIntfacePTH.keySet()) {
1056 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
1057 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
1059 // Open a new file to write into
1060 String newIntface = intMeth.getKey();
1061 String newStubClass = newIntface + "_Stub";
1062 FileWriter fw = new FileWriter(path + "/" + newStubClass + ".java");
1063 pw = new PrintWriter(new BufferedWriter(fw));
1064 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
1065 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
1066 // Pass in set of methods and get import classes
1067 Set<String> methods = intMeth.getValue();
1068 Set<String> importClasses = getImportClasses(methods, intDecl);
1069 List<String> stdImportClasses = getStandardJavaImportClasses();
1070 List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
1071 printImportStatements(allImportClasses); println("");
1072 // Find out if there are callback objects
1073 Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
1074 boolean callbackExist = !callbackClasses.isEmpty();
1075 // Write class header
1076 println("public class " + newStubClass + " implements " + newIntface + " {\n");
1078 writePropertiesJavaStub(intface, newIntface, callbackExist, callbackClasses);
1079 // Write constructor
1080 writeConstructorJavaStub(intface, newStubClass, callbackExist, callbackClasses);
1082 writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
1085 System.out.println("IoTCompiler: Generated stub class " + newStubClass + ".java...");
1092 * HELPER: writePropertiesJavaCallbackStub() writes the properties of the callback stub class
1094 private void writePropertiesJavaCallbackStub(String intface, String newIntface, boolean callbackExist, Set<String> callbackClasses) {
1096 println("private IoTRMICall rmiCall;");
1097 println("private String address;");
1098 println("private int[] ports;\n");
1099 // Get the object Id
1100 println("private int objectId = 0;");
1101 if (callbackExist) {
1102 // We assume that each class only has one callback interface for now
1103 Iterator it = callbackClasses.iterator();
1104 String callbackType = (String) it.next();
1105 println("// Callback properties");
1106 println("private IoTRMIObject rmiObj;");
1107 println("List<" + callbackType + "> listCallbackObj;");
1108 println("private static int objIdCnt = 0;");
1109 // Generate permission stuff for callback stubs
1110 DeclarationHandler decHandler = mapIntDeclHand.get(callbackType);
1111 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(callbackType);
1112 writePropertiesJavaPermission(callbackType, intDecl);
1119 * HELPER: writeConstructorJavaCallbackStub() writes the constructor of the callback stub class
1121 private void writeConstructorJavaCallbackStub(String intface, String newStubClass, boolean callbackExist, Set<String> callbackClasses) {
1123 // TODO: If we want callback in callback, then we need to add address and port initializations
1124 println("public " + newStubClass + "(IoTRMICall _rmiCall, int _objectId) throws Exception {");
1125 println("objectId = _objectId;");
1126 println("rmiCall = _rmiCall;");
1127 if (callbackExist) {
1128 Iterator it = callbackClasses.iterator();
1129 String callbackType = (String) it.next();
1130 writeConstructorJavaPermission(intface);
1131 println("listCallbackObj = new ArrayList<" + callbackType + ">();");
1132 println("___initCallBack();");
1133 println("// TODO: Add address and port initialization here if we want callback in callback!");
1140 * generateJavaCallbackStubClasses() generate callback stubs based on the methods list in Java
1142 * Callback stubs gets the IoTRMICall objects from outside of the class as contructor input
1143 * because all these stubs are populated by the class that takes in this object as a callback
1144 * object. In such a class, we only use one socket, hence one IoTRMICall, for all callback objects.
1146 public void generateJavaCallbackStubClasses() throws IOException {
1148 // Create a new directory
1149 String path = createDirectories(dir, subdir);
1150 for (String intface : mapIntfacePTH.keySet()) {
1152 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
1153 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
1155 // Open a new file to write into
1156 String newIntface = intMeth.getKey();
1157 String newStubClass = newIntface + "_CallbackStub";
1158 FileWriter fw = new FileWriter(path + "/" + newStubClass + ".java");
1159 pw = new PrintWriter(new BufferedWriter(fw));
1160 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
1161 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
1162 // Pass in set of methods and get import classes
1163 Set<String> methods = intMeth.getValue();
1164 Set<String> importClasses = getImportClasses(methods, intDecl);
1165 List<String> stdImportClasses = getStandardJavaImportClasses();
1166 List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
1167 printImportStatements(allImportClasses); println("");
1168 // Find out if there are callback objects
1169 Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
1170 boolean callbackExist = !callbackClasses.isEmpty();
1171 // Write class header
1172 println("public class " + newStubClass + " implements " + newIntface + " {\n");
1174 writePropertiesJavaCallbackStub(intface, newIntface, callbackExist, callbackClasses);
1175 // Write constructor
1176 writeConstructorJavaCallbackStub(intface, newStubClass, callbackExist, callbackClasses);
1178 // TODO: perhaps need to generate callback for callback
1179 writeMethodJavaStub(intMeth.getValue(), intDecl, callbackClasses);
1182 System.out.println("IoTCompiler: Generated callback stub class " + newStubClass + ".java...");
1189 * HELPER: writePropertiesJavaSkeleton() writes the properties of the skeleton class
1191 private void writePropertiesJavaSkeleton(String intface, boolean callbackExist, InterfaceDecl intDecl) {
1193 println("private " + intface + " mainObj;");
1194 //println("private int ports;");
1195 println("private IoTRMIObject rmiObj;\n");
1197 if (callbackExist) {
1198 println("private static int objIdCnt = 0;");
1199 println("private IoTRMICall rmiCall;");
1201 writePropertiesJavaPermission(intface, intDecl);
1207 * HELPER: writeStructPermissionJavaSkeleton() writes permission for struct helper
1209 private void writeStructPermissionJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, String intface) {
1211 // Use this set to handle two same methodIds
1212 for (String method : methods) {
1213 List<String> methParams = intDecl.getMethodParams(method);
1214 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1215 // Check for params with structs
1216 for (int i = 0; i < methParams.size(); i++) {
1217 String paramType = methPrmTypes.get(i);
1218 String param = methParams.get(i);
1219 String simpleType = getGenericType(paramType);
1220 if (isStructClass(simpleType)) {
1221 int methodNumId = intDecl.getMethodNumId(method);
1222 String helperMethod = methodNumId + "struct" + i;
1223 int methodHelperNumId = intDecl.getHelperMethodNumId(helperMethod);
1224 // Iterate over interfaces to give permissions to
1225 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
1226 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
1227 String newIntface = intMeth.getKey();
1228 int newObjectId = getNewIntfaceObjectId(newIntface);
1229 println("set" + newObjectId + "Allowed.add(" + methodHelperNumId + ");");
1238 * HELPER: writeConstructorJavaSkeleton() writes the constructor of the skeleton class
1240 private void writeConstructorJavaSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods, boolean callbackExist) {
1242 println("public " + newSkelClass + "(" + intface + " _mainObj, int _port) throws Exception {");
1243 println("mainObj = _mainObj;");
1244 println("rmiObj = new IoTRMIObject(_port);");
1245 // Generate permission control initialization
1246 writeConstructorJavaPermission(intface);
1247 writeJavaInitCallbackPermission(intface, intDecl, callbackExist);
1248 writeStructPermissionJavaSkeleton(methods, intDecl, intface);
1249 println("___waitRequestInvokeMethod();");
1255 * HELPER: writeStdMethodBodyJavaSkeleton() writes the standard method body in the skeleton class
1257 private void writeStdMethodBodyJavaSkeleton(List<String> methParams, String methodId, String methodType) {
1259 if (methodType.equals("void"))
1260 print("mainObj." + methodId + "(");
1262 print("return mainObj." + methodId + "(");
1263 for (int i = 0; i < methParams.size(); i++) {
1265 print(getSimpleIdentifier(methParams.get(i)));
1266 // Check if this is the last element (don't print a comma)
1267 if (i != methParams.size() - 1) {
1276 * HELPER: writeInitCallbackJavaSkeleton() writes the init callback method for skeleton class
1278 private void writeInitCallbackJavaSkeleton(boolean callbackSkeleton) {
1280 // This is a callback skeleton generation
1281 if (callbackSkeleton)
1282 println("public void ___regCB(IoTRMIObject rmiObj) throws IOException {");
1284 println("public void ___regCB() throws IOException {");
1285 print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class, String.class, int.class },");
1286 println("new Class<?>[] { null, null, null });");
1287 println("rmiCall = new IoTRMICall((int) paramObj[0], (String) paramObj[1], (int) paramObj[2]);");
1293 * HELPER: writeMethodJavaSkeleton() writes the method of the skeleton class
1295 private void writeMethodJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses,
1296 boolean callbackSkeleton) {
1298 boolean isDefined = false;
1299 for (String method : methods) {
1301 List<String> methParams = intDecl.getMethodParams(method);
1302 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1303 String methodId = intDecl.getMethodId(method);
1304 print("public " + intDecl.getMethodType(method) + " " + methodId + "(");
1305 boolean isCallbackMethod = false;
1306 String callbackType = null;
1307 for (int i = 0; i < methParams.size(); i++) {
1309 String origParamType = methPrmTypes.get(i);
1310 String paramType = checkAndGetParamClass(origParamType);
1311 if (callbackClasses.contains(origParamType)) { // Check if this has callback object
1312 isCallbackMethod = true;
1313 callbackType = origParamType;
1315 print(paramType + " " + methParams.get(i));
1316 // Check if this is the last element (don't print a comma)
1317 if (i != methParams.size() - 1) {
1322 // Now, write the body of skeleton!
1323 writeStdMethodBodyJavaSkeleton(methParams, methodId, intDecl.getMethodType(method));
1325 if (isCallbackMethod && !isDefined) { // Make sure that this function is only defined once!
1326 writeInitCallbackJavaSkeleton(callbackSkeleton);
1334 * HELPER: writeCallbackJavaStubGeneration() writes the callback stub generation part
1336 private Map<Integer,String> writeCallbackJavaStubGeneration(List<String> methParams, List<String> methPrmTypes,
1337 String callbackType, boolean isStructMethod) {
1339 Map<Integer,String> mapStubParam = new HashMap<Integer,String>();
1340 String offsetPfx = "";
1342 offsetPfx = "offset";
1343 // Iterate over callback objects
1344 for (int i = 0; i < methParams.size(); i++) {
1345 String paramType = methPrmTypes.get(i);
1346 String param = methParams.get(i);
1347 if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
1348 String exchParamType = checkAndGetParamClass(getGenericType(paramType));
1349 // Print array if this is array or list if this is a list of callback objects
1350 if (isArray(param)) {
1351 println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];");
1352 println(exchParamType + "[] stub" + i + " = new " + exchParamType + "[numStubs" + i + "];");
1353 } else if (isList(paramType)) {
1354 println("int numStubs" + i + " = (int) paramObj[" + offsetPfx + i + "];");
1355 println("List<" + exchParamType + "> stub" + i + " = new ArrayList<" + exchParamType + ">();");
1357 println(exchParamType + " stub" + i + " = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
1358 println("objIdCnt++;");
1361 // Generate a loop if needed
1362 if (checkCallbackType(paramType, callbackType)) { // Check if this has callback object
1363 String exchParamType = checkAndGetParamClass(getGenericType(paramType));
1364 if (isArray(param)) {
1365 println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
1366 println("stub" + i + "[objId] = new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt);");
1367 println("objIdCnt++;");
1369 } else if (isList(paramType)) {
1370 println("for (int objId = 0; objId < numStubs" + i + "; objId++) {");
1371 println("stub" + i + ".add(new " + exchParamType + "_CallbackStub(rmiCall, objIdCnt));");
1372 println("objIdCnt++;");
1375 mapStubParam.put(i, "stub" + i); // List of all stub parameters
1378 return mapStubParam;
1383 * HELPER: checkAndWriteEnumTypeJavaSkeleton() writes the enum type (convert from enum to int)
1385 private void checkAndWriteEnumTypeJavaSkeleton(List<String> methParams, List<String> methPrmTypes, boolean isStructMethod) {
1387 String offsetPfx = "";
1389 offsetPfx = "offset";
1390 // Iterate and find enum declarations
1391 boolean printed = false;
1392 for (int i = 0; i < methParams.size(); i++) {
1393 String paramType = methPrmTypes.get(i);
1394 String param = methParams.get(i);
1395 String simpleType = getGenericType(paramType);
1396 if (isEnumClass(simpleType)) {
1397 // Check if this is enum type
1398 println("int paramInt" + i + "[] = (int[]) paramObj[" + offsetPfx + i + "];");
1400 println(simpleType + "[] enumVals = " + simpleType + ".values();");
1403 if (isArray(param)) { // An array
1404 println("int len" + i + " = paramInt" + i + ".length;");
1405 println(simpleType + "[] paramEnum" + i + " = new " + simpleType + "[len" + i + "];");
1406 println("for (int i = 0; i < len" + i + "; i++) {");
1407 println("paramEnum" + i + "[i] = enumVals[paramInt" + i + "[i]];");
1409 } else if (isList(paramType)) { // A list
1410 println("int len" + i + " = paramInt" + i + ".length;");
1411 println("List<" + simpleType + "> paramEnum" + i + " = new ArrayList<" + simpleType + ">();");
1412 println("for (int i = 0; i < len" + i + "; i++) {");
1413 println("paramEnum" + i + ".add(enumVals[paramInt" + i + "[i]]);");
1415 } else { // Just one element
1416 println(simpleType + " paramEnum" + i + " = enumVals[paramInt" + i + "[0]];");
1424 * HELPER: checkAndWriteEnumRetTypeJavaSkeleton() writes the enum return type (convert from enum to int)
1426 private void checkAndWriteEnumRetTypeJavaSkeleton(String retType, String methodId) {
1428 // Strips off array "[]" for return type
1429 String pureType = getSimpleArrayType(getGenericType(retType));
1430 // Take the inner type of generic
1431 if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
1432 pureType = getGenericType(retType);
1433 if (isEnumClass(pureType)) {
1434 // Check if this is enum type
1436 if (isArray(retType)) { // An array
1437 print(pureType + "[] retEnum = " + methodId + "(");
1438 } else if (isList(retType)) { // A list
1439 print("List<" + pureType + "> retEnum = " + methodId + "(");
1440 } else { // Just one element
1441 print(pureType + " retEnum = " + methodId + "(");
1448 * HELPER: checkAndWriteEnumRetConvJavaSkeleton() writes the enum return type (convert from enum to int)
1450 private void checkAndWriteEnumRetConvJavaSkeleton(String retType) {
1452 // Strips off array "[]" for return type
1453 String pureType = getSimpleArrayType(getGenericType(retType));
1454 // Take the inner type of generic
1455 if (getParamCategory(retType) == ParamCategory.NONPRIMITIVES)
1456 pureType = getGenericType(retType);
1457 if (isEnumClass(pureType)) {
1458 // Check if this is enum type
1459 if (isArray(retType)) { // An array
1460 println("int retLen = retEnum.length;");
1461 println("int[] retEnumVal = new int[retLen];");
1462 println("for (int i = 0; i < retLen; i++) {");
1463 println("retEnumVal[i] = retEnum[i].ordinal();");
1465 } else if (isList(retType)) { // A list
1466 println("int retLen = retEnum.size();");
1467 println("int[] retEnumVal = new int[retLen];");
1468 println("for (int i = 0; i < retLen; i++) {");
1469 println("retEnumVal[i] = retEnum.get(i).ordinal();");
1471 } else { // Just one element
1472 println("int[] retEnumVal = new int[1];");
1473 println("retEnumVal[0] = retEnum.ordinal();");
1475 println("Object retObj = retEnumVal;");
1481 * HELPER: writeLengthStructParamClassSkeleton() writes lengths of params
1483 private void writeLengthStructParamClassSkeleton(List<String> methParams, List<String> methPrmTypes,
1484 String method, InterfaceDecl intDecl) {
1486 // Iterate and find struct declarations - count number of params
1487 for (int i = 0; i < methParams.size(); i++) {
1488 String paramType = methPrmTypes.get(i);
1489 String param = methParams.get(i);
1490 String simpleType = getGenericType(paramType);
1491 if (isStructClass(simpleType)) {
1492 int members = getNumOfMembers(simpleType);
1493 print(Integer.toString(members) + "*");
1494 int methodNumId = intDecl.getMethodNumId(method);
1495 print("struct" + methodNumId + "Size" + i);
1498 if (i != methParams.size() - 1) {
1506 * HELPER: writeStructMembersJavaSkeleton() writes member parameters of struct
1508 private void writeStructMembersJavaSkeleton(String simpleType, String paramType,
1509 String param, String method, InterfaceDecl intDecl, int iVar) {
1511 // Get the struct declaration for this struct and generate initialization code
1512 StructDecl structDecl = getStructDecl(simpleType);
1513 List<String> memTypes = structDecl.getMemberTypes(simpleType);
1514 List<String> members = structDecl.getMembers(simpleType);
1515 if (isArrayOrList(paramType, param)) { // An array or list
1516 int methodNumId = intDecl.getMethodNumId(method);
1517 String counter = "struct" + methodNumId + "Size" + iVar;
1518 println("for(int i = 0; i < " + counter + "; i++) {");
1520 if (isArrayOrList(paramType, param)) { // An array or list
1521 for (int i = 0; i < members.size(); i++) {
1522 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
1523 println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
1524 println("paramClsGen[pos++] = null;");
1527 } else { // Just one struct element
1528 for (int i = 0; i < members.size(); i++) {
1529 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
1530 println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
1531 println("paramClsGen[pos++] = null;");
1538 * HELPER: writeStructMembersInitJavaSkeleton() writes member parameters initialization of struct
1540 private void writeStructMembersInitJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
1541 List<String> methPrmTypes, String method) {
1543 println("int objPos = 0;");
1544 for (int i = 0; i < methParams.size(); i++) {
1545 String paramType = methPrmTypes.get(i);
1546 String param = methParams.get(i);
1547 String simpleType = getGenericType(paramType);
1548 if (isStructClass(simpleType)) {
1549 int methodNumId = intDecl.getMethodNumId(method);
1550 String counter = "struct" + methodNumId + "Size" + i;
1552 if (isArray(param)) { // An array
1553 println(simpleType + "[] paramStruct" + i + " = new " + simpleType + "[" + counter + "];");
1554 println("for(int i = 0; i < " + counter + "; i++) {");
1555 println("paramStruct" + i + "[i] = new " + simpleType + "();");
1557 } else if (isList(paramType)) { // A list
1558 println("List<" + simpleType + "> paramStruct" + i + " = new ArrayList<" + simpleType + ">();");
1560 println(simpleType + " paramStruct" + i + " = new " + simpleType + "();");
1561 // Initialize members
1562 StructDecl structDecl = getStructDecl(simpleType);
1563 List<String> members = structDecl.getMembers(simpleType);
1564 List<String> memTypes = structDecl.getMemberTypes(simpleType);
1565 if (isArrayOrList(paramType, param)) { // An array or list
1566 println("for(int i = 0; i < " + counter + "; i++) {");
1568 if (isArray(param)) { // An array
1569 for (int j = 0; j < members.size(); j++) {
1570 String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
1571 print("paramStruct" + i + "[i]." + getSimpleIdentifier(members.get(j)));
1572 println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
1575 } else if (isList(paramType)) { // A list
1576 println(simpleType + " paramStructMem = new " + simpleType + "();");
1577 for (int j = 0; j < members.size(); j++) {
1578 String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
1579 print("paramStructMem." + getSimpleIdentifier(members.get(j)));
1580 println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
1582 println("paramStruct" + i + ".add(paramStructMem);");
1584 } else { // Just one struct element
1585 for (int j = 0; j < members.size(); j++) {
1586 String prmType = checkAndGetArray(memTypes.get(j), members.get(j));
1587 print("paramStruct" + i + "." + getSimpleIdentifier(members.get(j)));
1588 println(" = (" + getSimpleType(getEnumType(prmType)) + ") paramObj[objPos++];");
1592 // Take offsets of parameters
1593 println("int offset" + i +" = objPos++;");
1600 * HELPER: writeStructReturnJavaSkeleton() writes struct for return statement
1602 private void writeStructReturnJavaSkeleton(String simpleType, String retType) {
1604 // Minimum retLen is 1 if this is a single struct object
1605 if (isArray(retType))
1606 println("int retLen = retStruct.length;");
1607 else if (isList(retType))
1608 println("int retLen = retStruct.size();");
1609 else // Just single struct object
1610 println("int retLen = 1;");
1611 println("Object retLenObj = retLen;");
1612 println("rmiObj.sendReturnObj(retLenObj);");
1613 int numMem = getNumOfMembers(simpleType);
1614 println("Class<?>[] retCls = new Class<?>[" + numMem + "*retLen];");
1615 println("Object[] retObj = new Object[" + numMem + "*retLen];");
1616 println("int retPos = 0;");
1617 // Get the struct declaration for this struct and generate initialization code
1618 StructDecl structDecl = getStructDecl(simpleType);
1619 List<String> memTypes = structDecl.getMemberTypes(simpleType);
1620 List<String> members = structDecl.getMembers(simpleType);
1621 if (isArray(retType)) { // An array or list
1622 println("for(int i = 0; i < retLen; i++) {");
1623 for (int i = 0; i < members.size(); i++) {
1624 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
1625 println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
1626 print("retObj[retPos++] = retStruct[i].");
1627 print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
1631 } else if (isList(retType)) { // An array or list
1632 println("for(int i = 0; i < retLen; i++) {");
1633 for (int i = 0; i < members.size(); i++) {
1634 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
1635 println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
1636 print("retObj[retPos++] = retStruct.get(i).");
1637 print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
1641 } else { // Just one struct element
1642 for (int i = 0; i < members.size(); i++) {
1643 String prmType = checkAndGetArray(memTypes.get(i), members.get(i));
1644 println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;");
1645 print("retObj[retPos++] = retStruct.");
1646 print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i));
1655 * HELPER: writeMethodHelperReturnJavaSkeleton() writes return statement part in skeleton
1657 private void writeMethodHelperReturnJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
1658 List<String> methPrmTypes, String method, boolean isCallbackMethod, String callbackType,
1659 boolean isStructMethod) {
1661 checkAndWriteEnumTypeJavaSkeleton(methParams, methPrmTypes, isStructMethod);
1662 Map<Integer,String> mapStubParam = null;
1663 if (isCallbackMethod) {
1665 mapStubParam = writeCallbackJavaStubGeneration(methParams, methPrmTypes, callbackType, isStructMethod);
1667 // Check if this is "void"
1668 String retType = intDecl.getMethodType(method);
1669 if (retType.equals("void")) {
1670 print(intDecl.getMethodId(method) + "(");
1671 } else if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) { // Enum type
1672 checkAndWriteEnumRetTypeJavaSkeleton(retType, intDecl.getMethodId(method));
1673 } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) { // Struct type
1674 print(retType + " retStruct = " + intDecl.getMethodId(method) + "(");
1675 } else { // We do have a return value
1676 print("Object retObj = " + intDecl.getMethodId(method) + "(");
1678 for (int i = 0; i < methParams.size(); i++) {
1680 String paramType = methPrmTypes.get(i);
1681 if (isCallbackMethod && checkCallbackType(paramType, callbackType)) {
1682 print(mapStubParam.get(i)); // Get the callback parameter
1683 } else if (isEnumClass(getGenericType(paramType))) { // Enum class
1684 print(getEnumParam(paramType, methParams.get(i), i));
1685 } else if (isStructClass(getGenericType(paramType))) {
1686 print("paramStruct" + i);
1688 String prmType = checkAndGetArray(paramType, methParams.get(i));
1690 print("(" + prmType + ") paramObj[offset" + i + "]");
1692 print("(" + prmType + ") paramObj[" + i + "]");
1694 if (i != methParams.size() - 1)
1698 if (!retType.equals("void")) {
1699 if (isEnumClass(getSimpleArrayType(getGenericType(retType)))) { // Enum type
1700 checkAndWriteEnumRetConvJavaSkeleton(retType);
1701 println("rmiObj.sendReturnObj(retObj);");
1702 } else if (isStructClass(getSimpleArrayType(getGenericType(retType)))) { // Struct type
1703 writeStructReturnJavaSkeleton(getSimpleArrayType(getGenericType(retType)), retType);
1704 println("rmiObj.sendReturnObj(retCls, retObj);");
1706 println("rmiObj.sendReturnObj(retObj);");
1708 if (isCallbackMethod) { // Catch exception if this is callback
1710 println(" catch(Exception ex) {");
1711 println("ex.printStackTrace();");
1712 println("throw new Error(\"Exception from callback object instantiation!\");");
1719 * HELPER: writeMethodHelperStructJavaSkeleton() writes the struct in skeleton
1721 private void writeMethodHelperStructJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
1722 List<String> methPrmTypes, String method, Set<String> callbackClasses) {
1724 // Generate array of parameter objects
1725 boolean isCallbackMethod = false;
1726 String callbackType = null;
1727 print("int paramLen = ");
1728 writeLengthStructParamClassSkeleton(methParams, methPrmTypes, method, intDecl);
1730 println("Class<?>[] paramCls = new Class<?>[paramLen];");
1731 println("Class<?>[] paramClsGen = new Class<?>[paramLen];");
1732 println("int pos = 0;");
1733 // Iterate again over the parameters
1734 for (int i = 0; i < methParams.size(); i++) {
1735 String paramType = methPrmTypes.get(i);
1736 String param = methParams.get(i);
1737 String simpleType = getGenericType(paramType);
1738 if (isStructClass(simpleType)) {
1739 writeStructMembersJavaSkeleton(simpleType, paramType, param, method, intDecl, i);
1741 String prmType = returnGenericCallbackType(methPrmTypes.get(i));
1742 if (callbackClasses.contains(prmType)) {
1743 isCallbackMethod = true;
1744 callbackType = prmType;
1745 println("paramCls[pos] = int.class;");
1746 println("paramClsGen[pos++] = null;");
1747 } else { // Generate normal classes if it's not a callback object
1748 String paramTypeOth = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
1749 println("paramCls[pos] = " + getSimpleType(getEnumType(paramTypeOth)) + ".class;");
1750 print("paramClsGen[pos++] = ");
1751 String prmTypeOth = methPrmTypes.get(i);
1752 if (getParamCategory(prmTypeOth) == ParamCategory.NONPRIMITIVES)
1753 println(getTypeOfGeneric(prmType)[0] + ".class;");
1759 println("Object[] paramObj = rmiObj.getMethodParams(paramCls, paramClsGen);");
1760 writeStructMembersInitJavaSkeleton(intDecl, methParams, methPrmTypes, method);
1761 // Write the return value part
1762 writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, true);
1767 * HELPER: writeStdMethodHelperBodyJavaSkeleton() writes the standard method body helper in the skeleton class
1769 private void writeStdMethodHelperBodyJavaSkeleton(InterfaceDecl intDecl, List<String> methParams,
1770 List<String> methPrmTypes, String method, Set<String> callbackClasses) {
1772 // Generate array of parameter objects
1773 boolean isCallbackMethod = false;
1774 String callbackType = null;
1775 print("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { ");
1776 for (int i = 0; i < methParams.size(); i++) {
1778 String paramType = returnGenericCallbackType(methPrmTypes.get(i));
1779 if (callbackClasses.contains(paramType)) {
1780 isCallbackMethod = true;
1781 callbackType = paramType;
1783 } else { // Generate normal classes if it's not a callback object
1784 String prmType = checkAndGetArray(methPrmTypes.get(i), methParams.get(i));
1785 print(getSimpleType(getEnumType(prmType)) + ".class");
1787 if (i != methParams.size() - 1)
1791 // Generate generic class if it's a generic type.. null otherwise
1792 print("new Class<?>[] { ");
1793 for (int i = 0; i < methParams.size(); i++) {
1794 String prmType = methPrmTypes.get(i);
1795 if ((getParamCategory(prmType) == ParamCategory.NONPRIMITIVES) &&
1796 !isEnumClass(getGenericType(prmType)) &&
1797 !callbackClasses.contains(getGenericType(prmType)))
1798 print(getGenericType(prmType) + ".class");
1801 if (i != methParams.size() - 1)
1805 // Write the return value part
1806 writeMethodHelperReturnJavaSkeleton(intDecl, methParams, methPrmTypes, method, isCallbackMethod, callbackType, false);
1811 * HELPER: writeMethodHelperJavaSkeleton() writes the method helper of the skeleton class
1813 private void writeMethodHelperJavaSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
1815 // Use this set to handle two same methodIds
1816 Set<String> uniqueMethodIds = new HashSet<String>();
1817 for (String method : methods) {
1819 List<String> methParams = intDecl.getMethodParams(method);
1820 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1821 if (isStructPresent(methParams, methPrmTypes)) { // Treat struct differently
1822 String methodId = intDecl.getMethodId(method);
1823 print("public void ___");
1824 String helperMethod = methodId;
1825 if (uniqueMethodIds.contains(methodId))
1826 helperMethod = helperMethod + intDecl.getMethodNumId(method);
1828 uniqueMethodIds.add(methodId);
1829 String retType = intDecl.getMethodType(method);
1830 print(helperMethod + "(");
1831 boolean begin = true;
1832 for (int i = 0; i < methParams.size(); i++) { // Print size variables
1833 String paramType = methPrmTypes.get(i);
1834 String param = methParams.get(i);
1835 String simpleType = getGenericType(paramType);
1836 if (isStructClass(simpleType)) {
1837 if (!begin) // Generate comma for not the beginning variable
1841 int methodNumId = intDecl.getMethodNumId(method);
1842 print("int struct" + methodNumId + "Size" + i);
1845 // Check if this is "void"
1846 if (retType.equals("void"))
1849 println(") throws IOException {");
1850 writeMethodHelperStructJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
1853 String methodId = intDecl.getMethodId(method);
1854 print("public void ___");
1855 String helperMethod = methodId;
1856 if (uniqueMethodIds.contains(methodId))
1857 helperMethod = helperMethod + intDecl.getMethodNumId(method);
1859 uniqueMethodIds.add(methodId);
1860 // Check if this is "void"
1861 String retType = intDecl.getMethodType(method);
1862 if (retType.equals("void"))
1863 println(helperMethod + "() {");
1865 println(helperMethod + "() throws IOException {");
1866 // Now, write the helper body of skeleton!
1867 writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
1871 // Write method helper for structs
1872 writeMethodHelperStructSetupJavaSkeleton(methods, intDecl);
1877 * HELPER: writeMethodHelperStructSetupJavaSkeleton() writes the method helper of struct setup in skeleton class
1879 private void writeMethodHelperStructSetupJavaSkeleton(Collection<String> methods,
1880 InterfaceDecl intDecl) {
1882 // Use this set to handle two same methodIds
1883 for (String method : methods) {
1885 List<String> methParams = intDecl.getMethodParams(method);
1886 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1887 // Check for params with structs
1888 for (int i = 0; i < methParams.size(); i++) {
1889 String paramType = methPrmTypes.get(i);
1890 String param = methParams.get(i);
1891 String simpleType = getGenericType(paramType);
1892 if (isStructClass(simpleType)) {
1893 int methodNumId = intDecl.getMethodNumId(method);
1894 print("public int ___");
1895 String helperMethod = methodNumId + "struct" + i;
1896 println(helperMethod + "() {");
1897 // Now, write the helper body of skeleton!
1898 println("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null });");
1899 println("return (int) paramObj[0];");
1908 * HELPER: writeMethodHelperStructSetupJavaCallbackSkeleton() writes the method helper of struct setup in callback skeleton class
1910 private void writeMethodHelperStructSetupJavaCallbackSkeleton(Collection<String> methods,
1911 InterfaceDecl intDecl) {
1913 // Use this set to handle two same methodIds
1914 for (String method : methods) {
1916 List<String> methParams = intDecl.getMethodParams(method);
1917 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1918 // Check for params with structs
1919 for (int i = 0; i < methParams.size(); i++) {
1920 String paramType = methPrmTypes.get(i);
1921 String param = methParams.get(i);
1922 String simpleType = getGenericType(paramType);
1923 if (isStructClass(simpleType)) {
1924 int methodNumId = intDecl.getMethodNumId(method);
1925 print("public int ___");
1926 String helperMethod = methodNumId + "struct" + i;
1927 println(helperMethod + "(IoTRMIObject rmiObj) {");
1928 // Now, write the helper body of skeleton!
1929 println("Object[] paramObj = rmiObj.getMethodParams(new Class<?>[] { int.class }, new Class<?>[] { null });");
1930 println("return (int) paramObj[0];");
1939 * HELPER: writeCountVarStructSkeleton() writes counter variable of struct for skeleton
1941 private void writeCountVarStructSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
1943 // Use this set to handle two same methodIds
1944 for (String method : methods) {
1946 List<String> methParams = intDecl.getMethodParams(method);
1947 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1948 // Check for params with structs
1949 for (int i = 0; i < methParams.size(); i++) {
1950 String paramType = methPrmTypes.get(i);
1951 String param = methParams.get(i);
1952 String simpleType = getGenericType(paramType);
1953 if (isStructClass(simpleType)) {
1954 int methodNumId = intDecl.getMethodNumId(method);
1955 println("int struct" + methodNumId + "Size" + i + " = 0;");
1963 * HELPER: writeInputCountVarStructSkeleton() writes input counter variable of struct for skeleton
1965 private boolean writeInputCountVarStructSkeleton(String method, InterfaceDecl intDecl) {
1967 List<String> methParams = intDecl.getMethodParams(method);
1968 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
1969 boolean structExist = false;
1970 boolean begin = true;
1971 // Check for params with structs
1972 for (int i = 0; i < methParams.size(); i++) {
1973 String paramType = methPrmTypes.get(i);
1974 String param = methParams.get(i);
1975 String simpleType = getGenericType(paramType);
1976 if (isStructClass(simpleType)) {
1982 int methodNumId = intDecl.getMethodNumId(method);
1983 print("struct" + methodNumId + "Size" + i);
1991 * HELPER: writeMethodCallStructSkeleton() writes method call for wait invoke in skeleton
1993 private void writeMethodCallStructSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
1995 // Use this set to handle two same methodIds
1996 for (String method : methods) {
1998 List<String> methParams = intDecl.getMethodParams(method);
1999 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
2000 // Check for params with structs
2001 for (int i = 0; i < methParams.size(); i++) {
2002 String paramType = methPrmTypes.get(i);
2003 String param = methParams.get(i);
2004 String simpleType = getGenericType(paramType);
2005 if (isStructClass(simpleType)) {
2006 int methodNumId = intDecl.getMethodNumId(method);
2008 String helperMethod = methodNumId + "struct" + i;
2009 String tempVar = "struct" + methodNumId + "Size" + i;
2010 print(intDecl.getHelperMethodNumId(helperMethod) + ": ");
2011 print(tempVar + " = ___");
2012 println(helperMethod + "(); break;");
2020 * HELPER: writeMethodCallStructCallbackSkeleton() writes method call for wait invoke in skeleton
2022 private void writeMethodCallStructCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl) {
2024 // Use this set to handle two same methodIds
2025 for (String method : methods) {
2027 List<String> methParams = intDecl.getMethodParams(method);
2028 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
2029 // Check for params with structs
2030 for (int i = 0; i < methParams.size(); i++) {
2031 String paramType = methPrmTypes.get(i);
2032 String param = methParams.get(i);
2033 String simpleType = getGenericType(paramType);
2034 if (isStructClass(simpleType)) {
2035 int methodNumId = intDecl.getMethodNumId(method);
2037 String helperMethod = methodNumId + "struct" + i;
2038 String tempVar = "struct" + methodNumId + "Size" + i;
2039 print(intDecl.getHelperMethodNumId(helperMethod) + ": ");
2040 print(tempVar + " = ___");
2041 println(helperMethod + "(rmiObj); break;");
2049 * HELPER: writeJavaMethodPermission() writes permission checks in skeleton
2051 private void writeJavaMethodPermission(String intface) {
2053 // Get all the different stubs
2054 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
2055 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
2056 String newIntface = intMeth.getKey();
2057 int newObjectId = getNewIntfaceObjectId(newIntface);
2058 println("if (_objectId == object" + newObjectId + "Id) {");
2059 println("if (!set" + newObjectId + "Allowed.contains(methodId)) {");
2060 println("throw new Error(\"Object with object Id: \" + _objectId + \" is not allowed to access method: \" + methodId);");
2064 println("throw new Error(\"Object Id: \" + _objectId + \" not recognized!\");");
2071 * HELPER: writeJavaWaitRequestInvokeMethod() writes the main loop of the skeleton class
2073 private void writeJavaWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist, String intface) {
2075 // Use this set to handle two same methodIds
2076 Set<String> uniqueMethodIds = new HashSet<String>();
2077 println("private void ___waitRequestInvokeMethod() throws IOException {");
2078 // Write variables here if we have callbacks or enums or structs
2079 writeCountVarStructSkeleton(methods, intDecl);
2080 println("while (true) {");
2081 println("rmiObj.getMethodBytes();");
2082 println("int _objectId = rmiObj.getObjectId();");
2083 println("int methodId = rmiObj.getMethodId();");
2084 // Generate permission check
2085 writeJavaMethodPermission(intface);
2086 println("switch (methodId) {");
2087 // Print methods and method Ids
2088 for (String method : methods) {
2089 String methodId = intDecl.getMethodId(method);
2090 int methodNumId = intDecl.getMethodNumId(method);
2091 print("case " + methodNumId + ": ___");
2092 String helperMethod = methodId;
2093 if (uniqueMethodIds.contains(methodId))
2094 helperMethod = helperMethod + methodNumId;
2096 uniqueMethodIds.add(methodId);
2097 print(helperMethod + "(");
2098 writeInputCountVarStructSkeleton(method, intDecl);
2099 println("); break;");
2101 String method = "___initCallBack()";
2102 // Print case -9999 (callback handler) if callback exists
2103 if (callbackExist) {
2104 int methodId = intDecl.getHelperMethodNumId(method);
2105 println("case " + methodId + ": ___regCB(); break;");
2107 writeMethodCallStructSkeleton(methods, intDecl);
2108 println("default: ");
2109 println("throw new Error(\"Method Id \" + methodId + \" not recognized!\");");
2117 * generateJavaSkeletonClass() generate skeletons based on the methods list in Java
2119 public void generateJavaSkeletonClass() throws IOException {
2121 // Create a new directory
2122 String path = createDirectories(dir, subdir);
2123 for (String intface : mapIntfacePTH.keySet()) {
2124 // Open a new file to write into
2125 String newSkelClass = intface + "_Skeleton";
2126 FileWriter fw = new FileWriter(path + "/" + newSkelClass + ".java");
2127 pw = new PrintWriter(new BufferedWriter(fw));
2128 // Pass in set of methods and get import classes
2129 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
2130 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
2131 List<String> methods = intDecl.getMethods();
2132 Set<String> importClasses = getImportClasses(methods, intDecl);
2133 List<String> stdImportClasses = getStandardJavaImportClasses();
2134 List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
2135 printImportStatements(allImportClasses);
2136 // Find out if there are callback objects
2137 Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
2138 boolean callbackExist = !callbackClasses.isEmpty();
2139 // Write class header
2141 println("public class " + newSkelClass + " implements " + intface + " {\n");
2143 writePropertiesJavaSkeleton(intface, callbackExist, intDecl);
2144 // Write constructor
2145 writeConstructorJavaSkeleton(newSkelClass, intface, intDecl, methods, callbackExist);
2147 writeMethodJavaSkeleton(methods, intDecl, callbackClasses, false);
2148 // Write method helper
2149 writeMethodHelperJavaSkeleton(methods, intDecl, callbackClasses);
2150 // Write waitRequestInvokeMethod() - main loop
2151 writeJavaWaitRequestInvokeMethod(methods, intDecl, callbackExist, intface);
2154 System.out.println("IoTCompiler: Generated skeleton class " + newSkelClass + ".java...");
2160 * HELPER: writePropertiesJavaCallbackSkeleton() writes the properties of the callback skeleton class
2162 private void writePropertiesJavaCallbackSkeleton(String intface, boolean callbackExist) {
2164 println("private " + intface + " mainObj;");
2165 // For callback skeletons, this is its own object Id
2166 println("private int objectId = 0;");
2168 if (callbackExist) {
2169 println("private static int objIdCnt = 0;");
2170 println("private IoTRMICall rmiCall;");
2177 * HELPER: writeConstructorJavaCallbackSkeleton() writes the constructor of the skeleton class
2179 private void writeConstructorJavaCallbackSkeleton(String newSkelClass, String intface, InterfaceDecl intDecl, Collection<String> methods) {
2181 println("public " + newSkelClass + "(" + intface + " _mainObj, int _objectId) throws Exception {");
2182 println("mainObj = _mainObj;");
2183 println("objectId = _objectId;");
2189 * HELPER: writeMethodHelperJavaCallbackSkeleton() writes the method helper of the callback skeleton class
2191 private void writeMethodHelperJavaCallbackSkeleton(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
2193 // Use this set to handle two same methodIds
2194 Set<String> uniqueMethodIds = new HashSet<String>();
2195 for (String method : methods) {
2197 List<String> methParams = intDecl.getMethodParams(method);
2198 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
2199 if (isStructPresent(methParams, methPrmTypes)) { // Treat struct differently
2200 String methodId = intDecl.getMethodId(method);
2201 print("public void ___");
2202 String helperMethod = methodId;
2203 if (uniqueMethodIds.contains(methodId))
2204 helperMethod = helperMethod + intDecl.getMethodNumId(method);
2206 uniqueMethodIds.add(methodId);
2207 String retType = intDecl.getMethodType(method);
2208 print(helperMethod + "(");
2209 boolean begin = true;
2210 for (int i = 0; i < methParams.size(); i++) { // Print size variables
2211 String paramType = methPrmTypes.get(i);
2212 String param = methParams.get(i);
2213 String simpleType = getGenericType(paramType);
2214 if (isStructClass(simpleType)) {
2215 if (!begin) // Generate comma for not the beginning variable
2219 int methodNumId = intDecl.getMethodNumId(method);
2220 print("int struct" + methodNumId + "Size" + i);
2223 // Check if this is "void"
2224 if (retType.equals("void"))
2225 println(", IoTRMIObject rmiObj) {");
2227 println(", IoTRMIObject rmiObj) throws IOException {");
2228 writeMethodHelperStructJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
2231 String methodId = intDecl.getMethodId(method);
2232 print("public void ___");
2233 String helperMethod = methodId;
2234 if (uniqueMethodIds.contains(methodId))
2235 helperMethod = helperMethod + intDecl.getMethodNumId(method);
2237 uniqueMethodIds.add(methodId);
2238 // Check if this is "void"
2239 String retType = intDecl.getMethodType(method);
2240 if (retType.equals("void"))
2241 println(helperMethod + "(IoTRMIObject rmiObj) {");
2243 println(helperMethod + "(IoTRMIObject rmiObj) throws IOException {");
2244 // Now, write the helper body of skeleton!
2245 writeStdMethodHelperBodyJavaSkeleton(intDecl, methParams, methPrmTypes, method, callbackClasses);
2249 // Write method helper for structs
2250 writeMethodHelperStructSetupJavaCallbackSkeleton(methods, intDecl);
2255 * HELPER: writeJavaCallbackWaitRequestInvokeMethod() writes the request invoke method of the callback skeleton class
2257 private void writeJavaCallbackWaitRequestInvokeMethod(Collection<String> methods, InterfaceDecl intDecl, boolean callbackExist) {
2259 // Use this set to handle two same methodIds
2260 Set<String> uniqueMethodIds = new HashSet<String>();
2261 println("public void invokeMethod(IoTRMIObject rmiObj) throws IOException {");
2262 // Write variables here if we have callbacks or enums or structs
2263 writeCountVarStructSkeleton(methods, intDecl);
2264 // Write variables here if we have callbacks or enums or structs
2265 println("int methodId = rmiObj.getMethodId();");
2266 // TODO: code the permission check here!
2267 println("switch (methodId) {");
2268 // Print methods and method Ids
2269 for (String method : methods) {
2270 String methodId = intDecl.getMethodId(method);
2271 int methodNumId = intDecl.getMethodNumId(method);
2272 print("case " + methodNumId + ": ___");
2273 String helperMethod = methodId;
2274 if (uniqueMethodIds.contains(methodId))
2275 helperMethod = helperMethod + methodNumId;
2277 uniqueMethodIds.add(methodId);
2278 print(helperMethod + "(");
2279 if (writeInputCountVarStructSkeleton(method, intDecl))
2280 println(", rmiObj); break;");
2282 println("rmiObj); break;");
2284 String method = "___initCallBack()";
2285 // Print case -9999 (callback handler) if callback exists
2286 if (callbackExist) {
2287 int methodId = intDecl.getHelperMethodNumId(method);
2288 println("case " + methodId + ": ___regCB(rmiObj); break;");
2290 writeMethodCallStructCallbackSkeleton(methods, intDecl);
2291 println("default: ");
2292 println("throw new Error(\"Method Id \" + methodId + \" not recognized!\");");
2299 * generateJavaCallbackSkeletonClass() generate callback skeletons based on the methods list in Java
2301 public void generateJavaCallbackSkeletonClass() throws IOException {
2303 // Create a new directory
2304 String path = createDirectories(dir, subdir);
2305 for (String intface : mapIntfacePTH.keySet()) {
2306 // Open a new file to write into
2307 String newSkelClass = intface + "_CallbackSkeleton";
2308 FileWriter fw = new FileWriter(path + "/" + newSkelClass + ".java");
2309 pw = new PrintWriter(new BufferedWriter(fw));
2310 // Pass in set of methods and get import classes
2311 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
2312 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
2313 List<String> methods = intDecl.getMethods();
2314 Set<String> importClasses = getImportClasses(methods, intDecl);
2315 List<String> stdImportClasses = getStandardJavaImportClasses();
2316 List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
2317 printImportStatements(allImportClasses);
2318 // Find out if there are callback objects
2319 Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
2320 boolean callbackExist = !callbackClasses.isEmpty();
2321 // Write class header
2323 println("public class " + newSkelClass + " implements " + intface + " {\n");
2325 writePropertiesJavaCallbackSkeleton(intface, callbackExist);
2326 // Write constructor
2327 writeConstructorJavaCallbackSkeleton(newSkelClass, intface, intDecl, methods);
2329 writeMethodJavaSkeleton(methods, intDecl, callbackClasses, true);
2330 // Write method helper
2331 writeMethodHelperJavaCallbackSkeleton(methods, intDecl, callbackClasses);
2332 // Write waitRequestInvokeMethod() - main loop
2333 writeJavaCallbackWaitRequestInvokeMethod(methods, intDecl, callbackExist);
2336 System.out.println("IoTCompiler: Generated callback skeleton class " + newSkelClass + ".java...");
2342 * HELPER: writeMethodCplusLocalInterface() writes the method of the local interface
2344 private void writeMethodCplusLocalInterface(Collection<String> methods, InterfaceDecl intDecl) {
2346 for (String method : methods) {
2348 List<String> methParams = intDecl.getMethodParams(method);
2349 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
2350 print("virtual " + checkAndGetCplusType(intDecl.getMethodType(method)) + " " +
2351 intDecl.getMethodId(method) + "(");
2352 for (int i = 0; i < methParams.size(); i++) {
2353 // Check for params with driver class types and exchange it
2354 // with its remote interface
2355 String paramType = checkAndGetParamClass(methPrmTypes.get(i));
2356 paramType = checkAndGetCplusType(paramType);
2357 // Check for arrays - translate into vector in C++
2358 String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
2359 print(paramComplete);
2360 // Check if this is the last element (don't print a comma)
2361 if (i != methParams.size() - 1) {
2371 * HELPER: writeMethodCplusInterface() writes the method of the interface
2373 private void writeMethodCplusInterface(Collection<String> methods, InterfaceDecl intDecl) {
2375 for (String method : methods) {
2377 List<String> methParams = intDecl.getMethodParams(method);
2378 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
2379 print("virtual " + checkAndGetCplusType(intDecl.getMethodType(method)) + " " +
2380 intDecl.getMethodId(method) + "(");
2381 for (int i = 0; i < methParams.size(); i++) {
2382 // Check for params with driver class types and exchange it
2383 // with its remote interface
2384 String paramType = methPrmTypes.get(i);
2385 paramType = checkAndGetCplusType(paramType);
2386 // Check for arrays - translate into vector in C++
2387 String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
2388 print(paramComplete);
2389 // Check if this is the last element (don't print a comma)
2390 if (i != methParams.size() - 1) {
2400 * HELPER: generateEnumCplus() writes the enumeration declaration
2402 public void generateEnumCplus() throws IOException {
2404 // Create a new directory
2405 createDirectory(dir);
2406 for (String intface : mapIntfacePTH.keySet()) {
2407 // Get the right StructDecl
2408 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
2409 EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
2410 Set<String> enumTypes = enumDecl.getEnumDeclarations();
2411 // Iterate over enum declarations
2412 for (String enType : enumTypes) {
2413 // Open a new file to write into
2414 FileWriter fw = new FileWriter(dir + "/" + enType + ".hpp");
2415 pw = new PrintWriter(new BufferedWriter(fw));
2416 // Write file headers
2417 println("#ifndef _" + enType.toUpperCase() + "_HPP__");
2418 println("#define _" + enType.toUpperCase() + "_HPP__");
2419 println("enum " + enType + " {");
2420 List<String> enumMembers = enumDecl.getMembers(enType);
2421 for (int i = 0; i < enumMembers.size(); i++) {
2423 String member = enumMembers.get(i);
2425 // Check if this is the last element (don't print a comma)
2426 if (i != enumMembers.size() - 1)
2434 System.out.println("IoTCompiler: Generated enum " + enType + ".hpp...");
2441 * HELPER: generateStructCplus() writes the struct declaration
2443 public void generateStructCplus() throws IOException {
2445 // Create a new directory
2446 createDirectory(dir);
2447 for (String intface : mapIntfacePTH.keySet()) {
2448 // Get the right StructDecl
2449 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
2450 StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
2451 List<String> structTypes = structDecl.getStructTypes();
2452 // Iterate over enum declarations
2453 for (String stType : structTypes) {
2454 // Open a new file to write into
2455 FileWriter fw = new FileWriter(dir + "/" + stType + ".hpp");
2456 pw = new PrintWriter(new BufferedWriter(fw));
2457 // Write file headers
2458 println("#ifndef _" + stType.toUpperCase() + "_HPP__");
2459 println("#define _" + stType.toUpperCase() + "_HPP__");
2460 println("using namespace std;");
2461 println("struct " + stType + " {");
2462 List<String> structMemberTypes = structDecl.getMemberTypes(stType);
2463 List<String> structMembers = structDecl.getMembers(stType);
2464 for (int i = 0; i < structMembers.size(); i++) {
2466 String memberType = structMemberTypes.get(i);
2467 String member = structMembers.get(i);
2468 String structTypeC = checkAndGetCplusType(memberType);
2469 String structComplete = checkAndGetCplusArray(structTypeC, member);
2470 println(structComplete + ";");
2475 System.out.println("IoTCompiler: Generated struct " + stType + ".hpp...");
2482 * generateCplusLocalInterfaces() writes the local interfaces and provides type-checking.
2484 * It needs to rewrite and exchange USERDEFINED types in input parameters of stub
2485 * and original interfaces, e.g. exchange Camera and CameraWithVideoAndRecording.
2486 * The local interface has to be the input parameter for the stub and the stub
2487 * interface has to be the input parameter for the local class.
2489 public void generateCplusLocalInterfaces() throws IOException {
2491 // Create a new directory
2492 createDirectory(dir);
2493 for (String intface : mapIntfacePTH.keySet()) {
2494 // Open a new file to write into
2495 FileWriter fw = new FileWriter(dir + "/" + intface + ".hpp");
2496 pw = new PrintWriter(new BufferedWriter(fw));
2497 // Write file headers
2498 println("#ifndef _" + intface.toUpperCase() + "_HPP__");
2499 println("#define _" + intface.toUpperCase() + "_HPP__");
2500 println("#include <iostream>");
2501 // Pass in set of methods and get include classes
2502 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
2503 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
2504 List<String> methods = intDecl.getMethods();
2505 Set<String> includeClasses = getIncludeClasses(methods, intDecl, intface, true);
2506 printIncludeStatements(includeClasses); println("");
2507 println("using namespace std;\n");
2508 //writeStructCplus(structDecl);
2509 println("class " + intface); println("{");
2512 writeMethodCplusLocalInterface(methods, intDecl);
2516 System.out.println("IoTCompiler: Generated local interface " + intface + ".hpp...");
2522 * generateCPlusInterfaces() generate stub interfaces based on the methods list in C++
2524 * For C++ we use virtual classe as interface
2526 public void generateCPlusInterfaces() throws IOException {
2528 // Create a new directory
2529 String path = createDirectories(dir, subdir);
2530 for (String intface : mapIntfacePTH.keySet()) {
2532 Map<String,Set<String>> mapNewIntMethods = mapInt2NewInts.get(intface);
2533 for (Map.Entry<String,Set<String>> intMeth : mapNewIntMethods.entrySet()) {
2535 // Open a new file to write into
2536 String newIntface = intMeth.getKey();
2537 FileWriter fw = new FileWriter(path + "/" + newIntface + ".hpp");
2538 pw = new PrintWriter(new BufferedWriter(fw));
2539 DeclarationHandler decHandler = mapIntDeclHand.get(intface);
2540 InterfaceDecl intDecl = (InterfaceDecl) decHandler.getInterfaceDecl(intface);
2541 // Write file headers
2542 println("#ifndef _" + newIntface.toUpperCase() + "_HPP__");
2543 println("#define _" + newIntface.toUpperCase() + "_HPP__");
2544 println("#include <iostream>");
2545 // Pass in set of methods and get import classes
2546 Set<String> includeClasses = getIncludeClasses(intMeth.getValue(), intDecl, intface, false);
2547 List<String> stdIncludeClasses = getStandardCplusIncludeClasses();
2548 List<String> allIncludeClasses = getAllLibClasses(stdIncludeClasses, includeClasses);
2549 printIncludeStatements(allIncludeClasses); println("");
2550 println("using namespace std;\n");
2551 println("class " + newIntface);
2555 writeMethodCplusInterface(intMeth.getValue(), intDecl);
2559 System.out.println("IoTCompiler: Generated interface " + newIntface + ".hpp...");
2566 * HELPER: writeMethodCplusStub() writes the methods of the stub
2568 private void writeMethodCplusStub(Collection<String> methods, InterfaceDecl intDecl, Set<String> callbackClasses) {
2570 boolean isDefined = false;
2571 for (String method : methods) {
2573 List<String> methParams = intDecl.getMethodParams(method);
2574 List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
2575 print(checkAndGetCplusType(intDecl.getMethodType(method)) + " " +
2576 intDecl.getMethodId(method) + "(");
2577 boolean isCallbackMethod = false;
2578 String callbackType = null;
2579 for (int i = 0; i < methParams.size(); i++) {
2581 String paramType = returnGenericCallbackType(methPrmTypes.get(i));
2582 // Check if this has callback object
2583 if (callbackClasses.contains(paramType)) {
2584 isCallbackMethod = true;
2585 callbackType = paramType;
2586 // Even if there're 2 callback arguments, we expect them to be of the same interface
2588 String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
2589 String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
2590 print(methParamComplete);
2591 // Check if this is the last element (don't print a comma)
2592 if (i != methParams.size() - 1) {
2597 if (isCallbackMethod)
2598 writeCallbackMethodBodyCplusStub(intDecl, methParams, methPrmTypes, method, callbackType);