* Class Properties
*/
private IoTRMICall rmiCall;
+ private IoTRMIObject rmiObj;
private String address;
private int[] ports;
- private List<CallBackInterface> listCBObj;
- private IoTRMIObject rmiObj;
/**
* Class Constants
*/
- private final static int NUM_CB_OBJ = 1;
private int objectId = 0; // Default value is 0
+ // This is permission system for callback objects
+ private static Integer[] object0Permission = { 0, 1 }; // object0 is the callback object
+ private List<Integer> set0Allowed;
+
+ /**
+ * Properties and constants for Callbacks!
+ */
+ private List<CallBackInterface> listCBObj;
+ private final static int NUM_CB_OBJ = 1;
private static int objIdCnt = 0; // Counter for callback object Ids
+
/**
* Constructors
*/
address = _address;
ports = _ports;
- //rmiCall = new IoTRMICall(_port, _address, _rev, methodSignatures);
rmiCall = new IoTRMICall(_port, _address, _rev);
+
+ set0Allowed = Arrays.asList(object0Permission);
+
+ // Only for callbacks!!!
listCBObj = new ArrayList<CallBackInterface>();
___initCallBack();
}
Thread thread = new Thread() {
public void run() {
try{
- //String[] methodSignatures = CallBack_CBSkeleton.getMethodSignatures();
- //rmiObj = new IoTRMIObject(ports[0], methodSignatures);
rmiObj = new IoTRMIObject(ports[0]);
- Object retObj = null;
while (true) {
byte[] method = rmiObj.getMethodBytes();
+ // Permission checking
+ int methId = IoTRMIObject.getMethodId(method);
+ if (!set0Allowed.contains(methId))
+ throw new Error("CallBack_CBSkeleton: This object is not allowed to access method " + methId);
+
int objId = IoTRMIObject.getObjectId(method);
CallBack_CBSkeleton skel = (CallBack_CBSkeleton) listCBObj.get(objId);
if (skel != null) {
// port, address, rev
Class<?>[] paramCls = new Class<?>[] { int.class, String.class, int.class };
Object[] paramObj = new Object[] { ports[0], address, 0 };
- //rmiCall.remoteCall(objectId, sign, retType, null, null, paramCls, paramObj);
rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
}
}
- public void handleStruct(StructJ[] data) {
+ public StructJ[] handleStruct(StructJ[] data) {
int methodId = 11;
Class<?> retType = void.class;
rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
int methodId2 = 10;
- Class<?> retType2 = void.class;
+ Class<?> retType2 = int.class; // return type is integer if it is a struct!!!
// Calculate the size of the array
Class<?>[] paramCls2 = new Class<?>[3*data.length];
Object[] paramObj2 = new Object[3*data.length];
paramCls2[pos] = int.class;
paramObj2[pos++] = data[i].year;
}
- System.out.println(Arrays.toString(paramObj2));
- rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
+ //System.out.println(Arrays.toString(paramObj2));
+ Object retObj = rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
+ // RETURN STRUCT
+ // Get the length of the struct first
+ int structsize1 = (int) retObj;
+ // Construct the struct
+ Class<?>[] retCls = new Class<?>[3*structsize1];
+ Class<?>[] retClsVal = new Class<?>[3*structsize1];
+ pos = 0;
+ for(int i=0; i < structsize1; i++) {
+ retCls[pos] = String.class;
+ retClsVal[pos++] = null;
+ retCls[pos] = float.class;
+ retClsVal[pos++] = null;
+ retCls[pos] = int.class;
+ retClsVal[pos++] = null;
+ }
+ Object[] retObj2 = rmiCall.getStructObjects(retCls,
+ retClsVal);
+ StructJ[] dataRet = new StructJ[structsize1];
+ for (int i=0; i < structsize1; i++) {
+ dataRet[i] = new StructJ();
+ }
+ pos = 0;
+ for(int i=0; i < structsize1; i++) {
+ dataRet[i].name = (String) retObj2[pos++];
+ dataRet[i].value = (float) retObj2[pos++];
+ dataRet[i].year = (int) retObj2[pos++];
+ }
+
+ return dataRet;
}
public EnumJ[] handleEnum(EnumJ[] en) {
- // Send length info first
- int methodId = 13;
- Class<?> retType = void.class;
- Class<?>[] paramCls = new Class<?>[] { int.class };
- Object[] paramObj = new Object[] { en.length };
- rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
-
- int methodId2 = 12;
- Class<?> retType2 = int[].class;
- // Calculate the size of the array
- Class<?>[] paramCls2 = new Class<?>[en.length];
- Object[] paramObj2 = new Object[en.length];
+ int methodId = 12;
+ Class<?> retType = int[].class;
// Handle with for loop
+ int paramInt[] = new int[en.length];
for(int i = 0; i < en.length; i++) {
- paramCls2[i] = int.class;
- paramObj2[i] = en[i].ordinal();
+ paramInt[i] = en[i].ordinal();
}
+
+ Class<?>[] paramCls = new Class<?>[] { int[].class };
+ Object[] paramObj = new Object[] { paramInt };
+
// if no return value just
- // rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
+ // rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
// This is with return value:
Object retObj =
- rmiCall.remoteCall(objectId, methodId2, retType2, null, paramCls2, paramObj2);
+ rmiCall.remoteCall(objectId, methodId, retType, null, paramCls, paramObj);
int[] retEnumInt = (int[]) retObj;
int enumsize1 = retEnumInt.length;
// Encoder/decoder
CallBackInterface cb6 = new CallBack(12);
CallBackInterface[] cbt = { cb4, cb5, cb6 };
tcstub.registerCallback(cbt);
- System.out.println("Return value from callback: " + tcstub.callBack());
+ System.out.println("Return value from callback: " + tcstub.callBack());*/
StructJ[] data = new StructJ[2];
for (int i=0; i<2; i++) {
System.out.println("Value: " + str.value);
System.out.println("Year: " + str.year);
}
- tcstub.handleStruct(data);*/
+ StructJ[] strj = tcstub.handleStruct(data);
+ for (StructJ str : strj) {
+ System.out.println("Name: " + str.name);
+ System.out.println("Value: " + str.value);
+ System.out.println("Year: " + str.year);
+ }
+
EnumJ[] en = { EnumJ.APPLE, EnumJ.ORANGE, EnumJ.APPLE, EnumJ.GRAPE };
EnumJ[] res = tcstub.handleEnum(en);