Cleaning up drivers/Cpp, Cpp/Lifxtest, virtuals, and iotrmi/C++ (revisiting the C...
[iot2.git] / iotjava / iotruntime / cpp / iotslave / IoTSlave.java
index 61a39bc7abfc616a1eabcf85e0405d1c1bb93616..02678c11343a07f84eb4021500d38ff61676c3eb 100644 (file)
@@ -2,6 +2,7 @@ import java.util.*;
 import java.io.*;
 import java.net.*;
 import java.nio.*;
+import static java.lang.Math.toIntExact;
 
 public class IoTSlave {
 
@@ -13,8 +14,7 @@ public class IoTSlave {
        private static final String STR_LOCALHOST = "localhost";
        private static final String STR_IOTSLAVE_CPP = "./IoTSlave.o";
        private static final String STR_IOTSLAVE_PATH = "~/tmp/iot2/iotjava/iotruntime/cpp/iotslave/";
-       private static final String STR_ACK = "ACK";
-       private static final String STR_END = "END";
+
        //private static final String STR_LOG_FILE_PATH = "./";
        private static int INT_SIZE = 4;        // send length in the size of integer (4 bytes)
 
@@ -42,6 +42,51 @@ public class IoTSlave {
        }
 
 
+       /**
+        * A method to send files from IoTMaster
+        *
+        * @param  filesocket File socket object
+        * @param  sFileName  File name
+        * @param  lFLength   File length
+        * @return            void
+        */
+       private void sendFile(Socket filesocket, String sFileName, long lFLength) throws IOException {
+
+               File file = new File(sFileName);
+               byte[] bytFile = new byte[toIntExact(lFLength)];
+               InputStream inFileStream = new FileInputStream(file);
+
+               OutputStream outFileStream = filesocket.getOutputStream();
+               int iCount;
+               while ((iCount = inFileStream.read(bytFile)) > 0) {
+                       outFileStream.write(bytFile, 0, iCount);
+               }
+               filesocket.close();
+       }
+
+
+       private void sendFile(String sFilePath, String sFileName) throws IOException {
+
+               sendCommCode(IoTCommCode.TRANSFER_FILE);
+               // Send file name
+               sendString(sFileName); recvAck();
+               File file = new File(sFilePath + sFileName);
+               int iFileLen = toIntExact(file.length());
+               System.out.println("IoTSlave: Sending file " + sFileName + " with length " + iFileLen + " bytes...");
+               // Send file length
+               sendInteger(iFileLen); recvAck();
+               byte[] bytFile = new byte[iFileLen];
+               InputStream inFileStream = new FileInputStream(file);
+
+               OutputStream outFileStream = socket.getOutputStream();
+               int iCount;
+               while ((iCount = inFileStream.read(bytFile)) > 0) {
+                       outFileStream.write(bytFile, 0, iCount);
+               }
+               System.out.println("IoTSlave: File sent!");
+               recvAck();
+       }
+
        /**
         * sendInteger() sends an integer in bytes
         */
@@ -392,6 +437,12 @@ public class IoTSlave {
                System.out.println("IoTSlave: Connection established with main!");
                // First contact with C++ IoTSlave
                System.out.println("IoTSlave: IoTSlave.o main is ready: " + iotSlaveMain.recvAck());
+               //iotSlaveMain.sendFile("../", "Lifxtest.so");
+               //iotSlaveMain.sendFile("../", "LightBulbTest_Stub.so");
+               //iotSlaveMain.sendFile("../", "Lifxtest.zip");
+               //iotSlaveMain.sendFile("../resources/", "Lifxtest.jar");
+               //iotSlaveMain.sendFile("../", "unzip.zip");
+               
 
                // =========================================
                // Create IoTSlave for driver object!
@@ -408,6 +459,10 @@ public class IoTSlave {
                System.out.println("IoTSlave: Connection established!");
                // First contact with C++ IoTSlave
                System.out.println("IoTSlave: IoTSlave.o is ready: " + iotSlave.recvAck());
+               //iotSlave.sendFile("../", "LifxLightBulb.so");
+               //iotSlave.sendFile("../", "LightBulb_Skeleton.so");
+               //iotSlave.sendFile("../", "LifxLightBulb.zip");
+               //iotSlave.sendFile("../", "unzip2.zip");
                iotSlave.createObjectCpp();
                //iotSlave.createNewIoTSetCpp();
                iotSlave.createNewIoTSetCpp("lb_addresses");