1) Making Java socket read/write length in 4 bytes as well; 2) Fixing endianness...
authorrtrimana <rtrimana@uci.edu>
Fri, 9 Dec 2016 18:38:12 +0000 (10:38 -0800)
committerrtrimana <rtrimana@uci.edu>
Fri, 9 Dec 2016 18:38:12 +0000 (10:38 -0800)
iotjava/iotrmi/C++/IoTRMICall.hpp
iotjava/iotrmi/C++/IoTSocket.hpp
iotjava/iotrmi/C++/basics/TestClassAdvanced_Stub.cpp
iotjava/iotrmi/Java/IoTRMIUtil.java
iotjava/iotrmi/Java/IoTSocket.java

index 99dd56d805027b0467ecc42100b31930f98c7d76..a63079846b4c7797bc9a37adebfe676b227dd3d9 100644 (file)
@@ -91,8 +91,6 @@ void* IoTRMICall::remoteCall(int objectId, int methodId, string retType, string
        methodToBytes(objectId, methodId, paramCls, paramObj, method, numParam);
        // Send bytes
        fflush(NULL);
        methodToBytes(objectId, methodId, paramCls, paramObj, method, numParam);
        // Send bytes
        fflush(NULL);
-       cout << "Length: " << len << endl;
-       IoTRMIUtil::printBytes(method, len, false);
        rmiClient->sendBytes(method, len);
        fflush(NULL);
        // Receive return value and return it to caller
        rmiClient->sendBytes(method, len);
        fflush(NULL);
        // Receive return value and return it to caller
index cf62ec1a2d6af143703d807a888f2254fe615013..3bbf74fa6f4ab2b963a8b8a661c8b212d5c56fdd 100644 (file)
@@ -33,6 +33,8 @@ static const int MSG_LEN_SIZE = 4;
 #include <sys/wait.h>
 #include <unistd.h>
 
 #include <sys/wait.h>
 #include <unistd.h>
 
+#include "IoTRMIUtil.hpp"
+
 // Duplicated from winsock2.h
 #define SD_RECEIVE      0x00
 #define SD_SEND         0x01
 // Duplicated from winsock2.h
 #define SD_RECEIVE      0x00
 #define SD_SEND         0x01
@@ -88,12 +90,12 @@ IoTSocket::~IoTSocket() {
 
 
 // Send bytes over the wire
 
 
 // Send bytes over the wire
-bool IoTSocket::sendBytes(char* pVals, int _iLen) {
+bool IoTSocket::sendBytes(char* pVals, int iLen) {
 
        int i = 0;
 
        int i = 0;
-       int size[1];
-       int iLen = _iLen;
-       size[0] = iLen;
+       char size[MSG_LEN_SIZE];
+       // Convert int to byte array and fix endianness
+       IoTRMIUtil::intToByteArray(iLen, size);
 
        if (send(m_iSock, size, MSG_LEN_SIZE, 0) == -1) {
                perror("IoTSocket: Send size error!");
 
        if (send(m_iSock, size, MSG_LEN_SIZE, 0) == -1) {
                perror("IoTSocket: Send size error!");
@@ -128,8 +130,8 @@ char* IoTSocket::receiveBytes(char* pVals, int* len)
 
        int iTotal = 0;
        int iResult = 0;
 
        int iTotal = 0;
        int iResult = 0;
-       int size[1];
-
+       char size[MSG_LEN_SIZE];
+       
        while ((iTotal < 1) && (iResult != -1)) {
                iResult = recv(m_iSock, size, MSG_LEN_SIZE, 0);         
                iTotal += iResult;
        while ((iTotal < 1) && (iResult != -1)) {
                iResult = recv(m_iSock, size, MSG_LEN_SIZE, 0);         
                iTotal += iResult;
@@ -138,7 +140,9 @@ char* IoTSocket::receiveBytes(char* pVals, int* len)
                perror("IoTSocket: Receive size error!");
                return NULL;
        }
                perror("IoTSocket: Receive size error!");
                return NULL;
        }
-       int iLen = size[0];
+       // Convert byte to int array based on correct endianness
+       int iLen = 0;
+       IoTRMIUtil::byteArrayToInt(&iLen, size);
 
        // To be returned from this method...
        *len = iLen;
 
        // To be returned from this method...
        *len = iLen;
index e224bca4f3e0f9bceb126c02c9252e84a168a1fb..66872c10e757f20937389a056f8713b1608bdcfe 100644 (file)
@@ -66,14 +66,14 @@ int main(int argc, char *argv[])
 /*     vector<Struct> vecRetStr = tcStub->handleStructArray(vecStr);
        for (Struct st : vecRetStr) {
                cout << "Name: " << st.name << endl;
 /*     vector<Struct> vecRetStr = tcStub->handleStructArray(vecStr);
        for (Struct st : vecRetStr) {
                cout << "Name: " << st.name << endl;
-               cout << "Value:" << st.value << endl;
-               cout << "Year" << st.year << endl;
+               cout << "Value: " << st.value << endl;
+               cout << "Year" << st.year << endl;
        }
        vector<Struct> vecRetStr2 = tcStub->handleStructList(vecStr);
        for (Struct st : vecRetStr2) {
                cout << "Name: " << st.name << endl;
        }
        vector<Struct> vecRetStr2 = tcStub->handleStructList(vecStr);
        for (Struct st : vecRetStr2) {
                cout << "Name: " << st.name << endl;
-               cout << "Value:" << st.value << endl;
-               cout << "Year" << st.year << endl;
+               cout << "Value: " << st.value << endl;
+               cout << "Year" << st.year << endl;
        }
        vector<Struct> vecRetStr3 = tcStub->handleStructComplex2(23, 'c', vecStr);
        for (Struct st : vecRetStr3) {
        }
        vector<Struct> vecRetStr3 = tcStub->handleStructComplex2(23, 'c', vecStr);
        for (Struct st : vecRetStr3) {
@@ -135,14 +135,14 @@ int main(int argc, char *argv[])
        vector<Struct> vecRetStr2 = tcStub->handleStructTwo(vecStr, vecStr);
        for (Struct st : vecRetStr2) {
                cout << "Name: " << st.name << endl;
        vector<Struct> vecRetStr2 = tcStub->handleStructTwo(vecStr, vecStr);
        for (Struct st : vecRetStr2) {
                cout << "Name: " << st.name << endl;
-               cout << "Value:" << st.value << endl;
-               cout << "Year" << st.year << endl;
+               cout << "Value: " << st.value << endl;
+               cout << "Year" << st.year << endl;
        }*/
        vector<Struct> vecRetStr3 = tcStub->handleStructThree(vecStr, vecStr, vecStr);
        for (Struct st : vecRetStr3) {
                cout << "Name: " << st.name << endl;
        }*/
        vector<Struct> vecRetStr3 = tcStub->handleStructThree(vecStr, vecStr, vecStr);
        for (Struct st : vecRetStr3) {
                cout << "Name: " << st.name << endl;
-               cout << "Value:" << st.value << endl;
-               cout << "Year" << st.year << endl;
+               cout << "Value: " << st.value << endl;
+               cout << "Year" << st.year << endl;
        }
 
        return 0;
        }
 
        return 0;
index 23385ba5199947039f7d68d398cb95fc4c02b063..170ff590f0680dcd802ad78e97cd19a51bb66038 100644 (file)
@@ -1300,45 +1300,4 @@ public class IoTRMIUtil {
         }
         return obj;
     }
         }
         return obj;
     }
-
-
-       public static void main(String[] args) {
-
-               //boolean data = false;
-               //char data = 'c';
-//             float data = 1234.123f;
-               //double data = 12.51231234;
-               //long data = 1234l;
-               //short data = 1234;
-               //int data = 12345678;
-//             byte[] result = floatToByteArray(data);
-//             System.out.println("Result: " + Arrays.toString(result));
-//             System.out.println("Converted back: " + byteArrayToFloat(result));
-               
-               //String str = "methodA(int,string,float,double,double)";
-               //int hash = str.hashCode();
-               //System.out.println("Hash value: " + hash);
-
-               int[][] multi = new int[][] {
-                       { 1, 2, 3 },
-                       { 6, 5, 4},
-                       { 11, 17, 13}
-               };
-
-               for (int[] inner : multi ) {
-                       System.out.println("New row!");
-                       for (Object i : inner) {
-                               System.out.println("Element i: " + i);
-                       }
-                       System.out.println("Change row!\n");
-               }
-
-               int[] int1 = { 1, 2, 3 };
-               int[] int2 = { 6, 5, 4 };
-               int[] result = new int[int1.length + int2.length];
-               System.arraycopy(int1, 0, result, 0, int1.length);
-               System.arraycopy(int2, 0, result, int1.length, int2.length);
-               
-               System.out.println("Combined array: " + Arrays.toString(result));
-       }
 }
 }
index 60aac8441dee7a878174e04639bf8e87f39651af..66089c95cd34ddfffa05a33602e78e67fd9d358e 100644 (file)
@@ -5,6 +5,7 @@ import java.io.*;
 import java.net.*;
 import java.awt.*;
 import java.util.*;
 import java.net.*;
 import java.awt.*;
 import java.util.*;
+import java.nio.ByteBuffer;
 
 
 /** Class IoTSocket is the basic class for IoT RMI
 
 
 /** Class IoTSocket is the basic class for IoT RMI
@@ -33,7 +34,8 @@ public abstract class IoTSocket {
        /**
         * Class Constant
         */
        /**
         * Class Constant
         */
-       protected static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold 
+       protected static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold
+       protected static int MSG_LEN_SIZE = 4;  // send length in the size of integer (4 bytes)
 
        /**
         * Default constructor
 
        /**
         * Default constructor
@@ -51,8 +53,12 @@ public abstract class IoTSocket {
        public void sendBytes(byte vals[]) throws IOException
        {
                int len = vals.length;
        public void sendBytes(byte vals[]) throws IOException
        {
                int len = vals.length;
-               output.write(len);
+               // Write the length first - convert to array of 4 bytes
+               ByteBuffer bb = ByteBuffer.allocate(MSG_LEN_SIZE);
+               bb.putInt(len);
+               output.write(bb.array(), 0, MSG_LEN_SIZE);
                output.flush();
                output.flush();
+               // Write the byte array
                output.write(vals, 0, len);
                output.flush();
                receiveAck();
                output.write(vals, 0, len);
                output.flush();
                receiveAck();
@@ -70,8 +76,11 @@ public abstract class IoTSocket {
                int numbytes;
                // Wait until input is available
                while(input.available() == 0);
                int numbytes;
                // Wait until input is available
                while(input.available() == 0);
-               // Read the maxlen first
-               int maxlen = (int)input.read();
+               // Read the maxlen first - read 4 bytes here
+               byte[] lenBytes = new byte[MSG_LEN_SIZE];
+               input.read(lenBytes, 0, MSG_LEN_SIZE);
+               int maxlen = ByteBuffer.wrap(lenBytes).getInt();
+               // Receive until maxlen
                if (maxlen>BUFFSIZE)
                        System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!");
                val = new byte[maxlen];
                if (maxlen>BUFFSIZE)
                        System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!");
                val = new byte[maxlen];