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
#include <sys/wait.h>
#include <unistd.h>
+#include "IoTRMIUtil.hpp"
+
// Duplicated from winsock2.h
#define SD_RECEIVE 0x00
#define SD_SEND 0x01
// 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!");
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;
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;
/* 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;
- 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> 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;
- cout << "Value:" << st.value << endl;
- cout << "Year" << st.year << endl;
+ cout << "Value: " << st.value << endl;
+ cout << "Year: " << st.year << endl;
}
return 0;
}
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));
- }
}
import java.net.*;
import java.awt.*;
import java.util.*;
+import java.nio.ByteBuffer;
/** Class IoTSocket is the basic class for IoT RMI
/**
* 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
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();
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];