From: rtrimana Date: Fri, 9 Dec 2016 18:38:12 +0000 (-0800) Subject: 1) Making Java socket read/write length in 4 bytes as well; 2) Fixing endianness... X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iot2.git;a=commitdiff_plain;h=889b21ac6831c6a74416d05b667a8ff0904615a5 1) Making Java socket read/write length in 4 bytes as well; 2) Fixing endianness issue when transferring message length between Java and C++ --- diff --git a/iotjava/iotrmi/C++/IoTRMICall.hpp b/iotjava/iotrmi/C++/IoTRMICall.hpp index 99dd56d..a630798 100644 --- a/iotjava/iotrmi/C++/IoTRMICall.hpp +++ b/iotjava/iotrmi/C++/IoTRMICall.hpp @@ -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); - cout << "Length: " << len << endl; - IoTRMIUtil::printBytes(method, len, false); rmiClient->sendBytes(method, len); fflush(NULL); // Receive return value and return it to caller diff --git a/iotjava/iotrmi/C++/IoTSocket.hpp b/iotjava/iotrmi/C++/IoTSocket.hpp index cf62ec1..3bbf74f 100644 --- a/iotjava/iotrmi/C++/IoTSocket.hpp +++ b/iotjava/iotrmi/C++/IoTSocket.hpp @@ -33,6 +33,8 @@ static const int MSG_LEN_SIZE = 4; #include #include +#include "IoTRMIUtil.hpp" + // Duplicated from winsock2.h #define SD_RECEIVE 0x00 #define SD_SEND 0x01 @@ -88,12 +90,12 @@ IoTSocket::~IoTSocket() { // Send bytes over the wire -bool IoTSocket::sendBytes(char* pVals, int _iLen) { +bool IoTSocket::sendBytes(char* pVals, int iLen) { 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!"); @@ -128,8 +130,8 @@ char* IoTSocket::receiveBytes(char* pVals, int* len) 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; @@ -138,7 +140,9 @@ char* IoTSocket::receiveBytes(char* pVals, int* len) 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; diff --git a/iotjava/iotrmi/C++/basics/TestClassAdvanced_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClassAdvanced_Stub.cpp index e224bca..66872c1 100644 --- a/iotjava/iotrmi/C++/basics/TestClassAdvanced_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClassAdvanced_Stub.cpp @@ -66,14 +66,14 @@ int main(int argc, char *argv[]) /* vector 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 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 vecRetStr3 = tcStub->handleStructComplex2(23, 'c', vecStr); for (Struct st : vecRetStr3) { @@ -135,14 +135,14 @@ int main(int argc, char *argv[]) vector 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 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; diff --git a/iotjava/iotrmi/Java/IoTRMIUtil.java b/iotjava/iotrmi/Java/IoTRMIUtil.java index 23385ba..170ff59 100644 --- a/iotjava/iotrmi/Java/IoTRMIUtil.java +++ b/iotjava/iotrmi/Java/IoTRMIUtil.java @@ -1300,45 +1300,4 @@ public class IoTRMIUtil { } 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)); - } } diff --git a/iotjava/iotrmi/Java/IoTSocket.java b/iotjava/iotrmi/Java/IoTSocket.java index 60aac84..66089c9 100644 --- a/iotjava/iotrmi/Java/IoTSocket.java +++ b/iotjava/iotrmi/Java/IoTSocket.java @@ -5,6 +5,7 @@ import java.io.*; import java.net.*; import java.awt.*; import java.util.*; +import java.nio.ByteBuffer; /** Class IoTSocket is the basic class for IoT RMI @@ -33,7 +34,8 @@ public abstract class IoTSocket { /** * 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 @@ -51,8 +53,12 @@ public abstract class IoTSocket { 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(); + // Write the byte array 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); - // 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];