Working C++ IoTSlave for both IoTSet and IoTRelation; threading works for C++ LifxLig...
[iot2.git] / iotjava / iotruntime / master / IoTMaster.java
index 154f36a5ad9811993c9512bcad92dfe35e8fe3a0..7578f3b332b1450790f48e23f1735a886237af2b 100644 (file)
@@ -885,7 +885,7 @@ public class IoTMaster {
                RuntimeOutput.print("IoTMaster: Executing: " + strCmdSend, BOOL_VERBOSE);
                // Unzip file
                String strCmdUnzip = STR_SSH + " " + STR_USERNAME + strIoTSlaveObjectHostAdd + " cd " +
-                                       STR_SLAVE_DIR + " sudo unzip " + sFileName + ";";
+                                       STR_SLAVE_DIR + " sudo unzip -o " + sFileName + ";";
                runCommand(strCmdUnzip);
                RuntimeOutput.print("IoTMaster: Executing: " + strCmdUnzip, BOOL_VERBOSE);
 
@@ -1051,9 +1051,10 @@ public class IoTMaster {
                if(STR_LANGUAGE.equals(STR_JAVA)) {
                        ObjectOutputStream oStream = (ObjectOutputStream) outStream;
                        oStream.writeObject(new MessageSimple(IoTCommCode.END_SESSION));
-               } else  // C++ side for now will be running continuously because it's an infinite loop (not in a separate thread)
+               } else {        // C++ side for now will be running continuously because it's an infinite loop (not in a separate thread)
+                       createDriverObjectCpp(outStream, inStream);
                        //endSessionCpp(outStream);
-                       ;
+               }
 
                // PROFILING
                result = System.currentTimeMillis()-start;
@@ -1276,10 +1277,9 @@ public class IoTMaster {
                                List<ObjectInitInfo> listObject = objInitHand.getListObjectInitInfo(str);
                                for (ObjectInitInfo objInitInfo : listObject) {
                                        // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTSET
-                                       getIoTSetRelationObjectCpp(objInitInfo.getIoTSlaveObjectHostAdd(), objInitInfo.getObjectName(), 
-                                               objInitInfo.getObjectClassName(), objInitInfo.getObjectClassInterfaceName(), 
-                                               objInitInfo.getObjectStubClassInterfaceName(), objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(),
-                                               objInitInfo.getRMICallbackPorts(), outStream, inStream);
+                                       getIoTSetRelationObjectCpp(IoTCommCode.GET_IOTSET_OBJECT, objInitInfo.getIoTSlaveObjectHostAdd(), objInitInfo.getObjectName(), 
+                                               objInitInfo.getObjectClassName(), objInitInfo.getObjectClassInterfaceName(), objInitInfo.getObjectStubClassInterfaceName(),
+                                               objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(), objInitInfo.getRMICallbackPorts(), outStream, inStream);
                                }
                                // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO REINITIALIZE IOTSET FIELD
                                reinitializeIoTSetFieldCpp(outStream, inStream);
@@ -1292,16 +1292,14 @@ public class IoTMaster {
                                Iterator it = listSecondObject.iterator();
                                for (ObjectInitInfo objInitInfo : listObject) {
                                        // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTRELATION (FIRST OBJECT)
-                                       getIoTSetRelationObjectCpp(objInitInfo.getIoTSlaveObjectHostAdd(), objInitInfo.getObjectName(), 
-                                               objInitInfo.getObjectClassName(), objInitInfo.getObjectClassInterfaceName(), 
-                                               objInitInfo.getObjectStubClassInterfaceName(), objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(),
-                                               objInitInfo.getRMICallbackPorts(), outStream, inStream);
+                                       getIoTSetRelationObjectCpp(IoTCommCode.GET_IOTRELATION_FIRST_OBJECT, objInitInfo.getIoTSlaveObjectHostAdd(), objInitInfo.getObjectName(), 
+                                               objInitInfo.getObjectClassName(), objInitInfo.getObjectClassInterfaceName(), objInitInfo.getObjectStubClassInterfaceName(),
+                                               objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(), objInitInfo.getRMICallbackPorts(), outStream, inStream);
                                        ObjectInitInfo objSecObj = (ObjectInitInfo) it.next();
                                        // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTRELATION (SECOND OBJECT)
-                                       getIoTSetRelationObjectCpp(objSecObj.getIoTSlaveObjectHostAdd(), objSecObj.getObjectName(), 
-                                               objSecObj.getObjectClassName(), objSecObj.getObjectClassInterfaceName(), 
-                                               objSecObj.getObjectStubClassInterfaceName(), objSecObj.getRMIRegistryPort(), objSecObj.getRMIStubPort(),
-                                               objSecObj.getRMICallbackPorts(), outStream, inStream);
+                                       getIoTSetRelationObjectCpp(IoTCommCode.GET_IOTRELATION_SECOND_OBJECT, objSecObj.getIoTSlaveObjectHostAdd(), objSecObj.getObjectName(), 
+                                               objSecObj.getObjectClassName(), objSecObj.getObjectClassInterfaceName(), objSecObj.getObjectStubClassInterfaceName(),
+                                               objSecObj.getRMIRegistryPort(), objSecObj.getRMIStubPort(), objSecObj.getRMICallbackPorts(), outStream, inStream);
                                }
                                // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO REINITIALIZE IOTRELATION FIELD
                                reinitializeIoTRelationFieldCpp(outStream, inStream);
@@ -1456,21 +1454,21 @@ public class IoTMaster {
                sendString(sFileName, outStream); recvAck(inStream);
                File file = new File(sFilePath + sFileName);
                int iFileLen = toIntExact(file.length());
-               RuntimeOutput.print("IoTSlave: Sending file " + sFileName + " with length " + iFileLen + " bytes...", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Sending file " + sFileName + " with length " + iFileLen + " bytes...", BOOL_VERBOSE);
                // Send file length
                sendInteger(iFileLen, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Sent file size!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Sent file size!", BOOL_VERBOSE);
                byte[] bytFile = new byte[iFileLen];
                InputStream inFileStream = new FileInputStream(file);
-               RuntimeOutput.print("IoTSlave: Opened file!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Opened file!", BOOL_VERBOSE);
 
                OutputStream outFileStream = fileSocket.getOutputStream();
-               RuntimeOutput.print("IoTSlave: Got output stream!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Got output stream!", BOOL_VERBOSE);
                int iCount;
                while ((iCount = inFileStream.read(bytFile)) > 0) {
                        outFileStream.write(bytFile, 0, iCount);
                }
-               RuntimeOutput.print("IoTSlave: File sent!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: File sent!", BOOL_VERBOSE);
                recvAck(inStream);
        }
 
@@ -1490,7 +1488,7 @@ public class IoTMaster {
                RuntimeOutput.print("IoTMaster: Executing: " + strCmdSend, BOOL_VERBOSE);
                // Unzip file
                String strCmdUnzip = STR_SSH + " " + STR_USERNAME + strIoTSlaveControllerHostAdd + " cd " +
-                                       STR_SLAVE_DIR + " sudo unzip " + sFileName + ";";
+                                       STR_SLAVE_DIR + " sudo unzip -o " + sFileName + ";";
                runCommand(strCmdUnzip);
                RuntimeOutput.print("IoTMaster: Executing: " + strCmdUnzip, BOOL_VERBOSE);
        }
@@ -1674,7 +1672,7 @@ public class IoTMaster {
                sendCommCode(IoTCommCode.CREATE_MAIN_OBJECT, outStream, inStream);
                String strMainObjName = strObjControllerName;
                sendString(strMainObjName, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Create a main object: " + strMainObjName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Create a main object: " + strMainObjName, BOOL_VERBOSE);
        }
 
 
@@ -1723,27 +1721,27 @@ public class IoTMaster {
                OutputStream outStream, InputStream inStream) throws IOException {
 
                sendCommCode(IoTCommCode.CREATE_OBJECT, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Send request to create a driver object... ", BOOL_VERBOSE);
-               RuntimeOutput.print("IoTSlave: Driver object name: " + strObjName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send request to create a driver object... ", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object name: " + strObjName, BOOL_VERBOSE);
                sendString(strObjName, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object class name: " + strObjClassName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object class name: " + strObjClassName, BOOL_VERBOSE);
                sendString(strObjClassName, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object interface name: " + strObjClassInterfaceName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object interface name: " + strObjClassInterfaceName, BOOL_VERBOSE);
                sendString(strObjStubClsIntfaceName, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object skeleton class name: " + strObjClassInterfaceName + STR_SKEL_CLASS_SUFFIX, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object skeleton class name: " + strObjClassInterfaceName + STR_SKEL_CLASS_SUFFIX, BOOL_VERBOSE);
                sendString(strObjClassInterfaceName + STR_SKEL_CLASS_SUFFIX, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object registry port: " + iRMIRegistryPort, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object registry port: " + iRMIRegistryPort, BOOL_VERBOSE);
                sendInteger(iRMIRegistryPort, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object stub port: " + iRMIStubPort, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object stub port: " + iRMIStubPort, BOOL_VERBOSE);
                sendInteger(iRMIStubPort, outStream); recvAck(inStream);
                int numOfArgs = arrFieldValues.length;
-               RuntimeOutput.print("IoTSlave: Send constructor arguments! Number of arguments: " + numOfArgs, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send constructor arguments! Number of arguments: " + numOfArgs, BOOL_VERBOSE);
                sendInteger(numOfArgs, outStream); recvAck(inStream);
                for(Object obj : arrFieldValues) {
                        String str = getObjectConverted(obj);
                        sendString(str, outStream); recvAck(inStream);
                }
-               RuntimeOutput.print("IoTSlave: Send constructor argument classes!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send constructor argument classes!", BOOL_VERBOSE);
                for(Class cls : arrFieldClasses) {
                        String str = getClassConverted(cls);
                        sendString(str, outStream); recvAck(inStream);
@@ -1757,8 +1755,8 @@ public class IoTMaster {
        public void createNewIoTSetCpp(String strObjFieldName, OutputStream outStream, InputStream inStream) throws IOException {
 
                sendCommCode(IoTCommCode.CREATE_NEW_IOTSET, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Creating new IoTSet...", BOOL_VERBOSE);
-               RuntimeOutput.print("IoTSlave: Send object field name: " + strObjFieldName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Creating new IoTSet...", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send object field name: " + strObjFieldName, BOOL_VERBOSE);
                sendString(strObjFieldName, outStream); recvAck(inStream);
        }
 
@@ -1769,8 +1767,8 @@ public class IoTMaster {
        public void createNewIoTRelationCpp(String strObjFieldName, OutputStream outStream, InputStream inStream) throws IOException {
 
                sendCommCode(IoTCommCode.CREATE_NEW_IOTRELATION, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Creating new IoTRelation...", BOOL_VERBOSE);
-               RuntimeOutput.print("IoTSlave: Send object field name: " + strObjFieldName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Creating new IoTRelation...", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send object field name: " + strObjFieldName, BOOL_VERBOSE);
                sendString(strObjFieldName, outStream); recvAck(inStream);
        }
 
@@ -1782,7 +1780,7 @@ public class IoTMaster {
                        String strDeviceAddress, int iSourcePort, int iDestPort, boolean bSourceWildCard, boolean bDestWildCard) throws IOException {
 
                sendCommCode(IoTCommCode.GET_DEVICE_IOTSET_OBJECT, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Getting IoTDeviceAddress...", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Getting IoTDeviceAddress...", BOOL_VERBOSE);
                sendString(strDeviceAddress, outStream); recvAck(inStream);
                sendInteger(iSourcePort, outStream); recvAck(inStream);
                sendInteger(iDestPort, outStream); recvAck(inStream);
@@ -1790,33 +1788,33 @@ public class IoTMaster {
                sendInteger(iSourceWildCard, outStream); recvAck(inStream);
                int iDestWildCard = (bDestWildCard ? 1 : 0);
                sendInteger(iDestWildCard, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Send device address: " + strDeviceAddress, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send device address: " + strDeviceAddress, BOOL_VERBOSE);
        }
 
 
        /**
         * Get a IoTSet content object for C++
         */
-       public void getIoTSetRelationObjectCpp(String strIoTSlaveHostAddress, String strObjectName, String strObjectClassName, 
+       public void getIoTSetRelationObjectCpp(IoTCommCode iotCommCode, String strIoTSlaveHostAddress, String strObjectName, String strObjectClassName, 
                        String strObjectClassInterfaceName, String strObjectStubClassInterfaceName, int iRMIRegistryPort, int iRMIStubPort, 
                        Integer[] iCallbackPorts, OutputStream outStream, InputStream inStream) throws IOException {
 
-               sendCommCode(IoTCommCode.GET_IOTSET_OBJECT, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Getting IoTSet object content...", BOOL_VERBOSE);
+               sendCommCode(iotCommCode, outStream, inStream);
+               RuntimeOutput.print("IoTMaster: Getting IoTSet object content...", BOOL_VERBOSE);
                // Send info
-               RuntimeOutput.print("IoTSlave: Send host address: " + strIoTSlaveHostAddress, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send host address: " + strIoTSlaveHostAddress, BOOL_VERBOSE);
                sendString(strIoTSlaveHostAddress, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object name: " + strObjectName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object name: " + strObjectName, BOOL_VERBOSE);
                sendString(strObjectName, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object class name: " + strObjectClassName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object class name: " + strObjectClassName, BOOL_VERBOSE);
                sendString(strObjectClassName, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object interface name: " + strObjectClassInterfaceName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object interface name: " + strObjectClassInterfaceName, BOOL_VERBOSE);
                sendString(strObjectClassInterfaceName, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object stub class name: " + strObjectStubClassInterfaceName + STR_STUB_CLASS_SUFFIX, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object stub class name: " + strObjectStubClassInterfaceName + STR_STUB_CLASS_SUFFIX, BOOL_VERBOSE);
                sendString(strObjectStubClassInterfaceName + STR_STUB_CLASS_SUFFIX, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object registry port: " + iRMIRegistryPort, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object registry port: " + iRMIRegistryPort, BOOL_VERBOSE);
                sendInteger(iRMIRegistryPort, outStream); recvAck(inStream);
-               RuntimeOutput.print("IoTSlave: Driver object stub port: " + iRMIStubPort, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Driver object stub port: " + iRMIStubPort, BOOL_VERBOSE);
                sendInteger(iRMIStubPort, outStream); recvAck(inStream);
                sendInteger(iCallbackPorts.length, outStream); recvAck(inStream);
                for(Integer i : iCallbackPorts) {
@@ -1830,9 +1828,9 @@ public class IoTMaster {
         */
        private void reinitializeIoTRelationFieldCpp(OutputStream outStream, InputStream inStream) throws IOException {
 
-               RuntimeOutput.print("IoTSlave: About to Reinitialize IoTRelation field!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: About to Reinitialize IoTRelation field!", BOOL_VERBOSE);
                sendCommCode(IoTCommCode.REINITIALIZE_IOTRELATION_FIELD, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Reinitialize IoTRelation field!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Reinitialize IoTRelation field!", BOOL_VERBOSE);
        }
 
 
@@ -1841,9 +1839,19 @@ public class IoTMaster {
         */
        private void reinitializeIoTSetFieldCpp(OutputStream outStream, InputStream inStream) throws IOException {
 
-               RuntimeOutput.print("IoTSlave: About to Reinitialize IoTSet field!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: About to Reinitialize IoTSet field!", BOOL_VERBOSE);
                sendCommCode(IoTCommCode.REINITIALIZE_IOTSET_FIELD, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Reinitialize IoTSet field!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Reinitialize IoTSet field!", BOOL_VERBOSE);
+       }
+
+
+       /**
+        * Create driver object for C++
+        */
+       private void createDriverObjectCpp(OutputStream outStream, InputStream inStream) throws IOException {
+
+               sendCommCode(IoTCommCode.CREATE_DRIVER_OBJECT, outStream, inStream);
+               RuntimeOutput.print("IoTMaster: Send command to create driver object!", BOOL_VERBOSE);
        }
 
 
@@ -1853,7 +1861,7 @@ public class IoTMaster {
        private void invokeInitMethodCpp(OutputStream outStream, InputStream inStream) throws IOException {
 
                sendCommCode(IoTCommCode.INVOKE_INIT_METHOD, outStream, inStream);
-               RuntimeOutput.print("IoTSlave: Invoke init method!", BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Invoke init method!", BOOL_VERBOSE);
        }
 
 
@@ -1866,8 +1874,8 @@ public class IoTMaster {
                IoTCommCode endSessionCode = IoTCommCode.END_SESSION;
                int intCode = endSessionCode.ordinal();
                sendInteger(intCode, outStream);
-               //RuntimeOutput.print("IoTSlave: Send request to create a main object: " + strObjName, BOOL_VERBOSE);
-               RuntimeOutput.print("IoTSlave: Send request to end session!", BOOL_VERBOSE);
+               //RuntimeOutput.print("IoTMaster: Send request to create a main object: " + strObjName, BOOL_VERBOSE);
+               RuntimeOutput.print("IoTMaster: Send request to end session!", BOOL_VERBOSE);
        }