import java.io.*;
import java.net.*;
import java.nio.*;
+import static java.lang.Math.toIntExact;
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)
}
+ /**
+ * 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
*/
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!
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");