private Socket socket;
private ObjectOutputStream outStream;
private ObjectInputStream inStream;
+ private Map<String,Object> mapObjNameStub;
/**
* IoTSet object, e.g. IoTSet<ProximitySensor> proximity_sensors;
socket = null;
outStream = null;
inStream = null;
+ mapObjNameStub = new HashMap<String,Object>();
STR_JAR_FILE_PATH = null;
STR_OBJ_CLS_PFX = null;
Class<?> clsSkel = Class.forName(strObjSkelName);
Class<?> clsInt = Class.forName(STR_OBJ_CLS_PFX + "." + STR_INTERFACE_PFX +
"." + sMessage.getObjectInterfaceName());
- Class[] clsSkelParams = { clsInt, String.class, int.class }; // Port number is integer
+ Class[] clsSkelParams = { clsInt, int.class, int.class }; // Port number is integer
Constructor<?> objSkelCons = clsSkel.getDeclaredConstructor(clsSkelParams);
- String callbackAddress = InetAddress.getLocalHost().getHostAddress(); // Callback address is this machine's address
- Object objSkelParams[] = { objMainCls, callbackAddress, iRMIStubPort };
+ Object objSkelParams[] = { objMainCls, iRMIStubPort, iRMIRegPort };
// Create a new thread for each skeleton
Thread objectThread = new Thread(new Runnable() {
public void run() {
// Translating into the actual Message class
MessageGetObject sMessage = (MessageGetObject) sIoTMasterMsg;
- // Instantiate the stub and put in the object
- String strObjStubName = sMainObjectName + "." + sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX;
- RuntimeOutput.print("IoTSlave: Stub object: " + strObjStubName, BOOL_VERBOSE);
- Class<?> clsStub = Class.forName(strObjStubName); // Port number is integer
- Class[] clsStubParams = { int.class, String.class, String.class, int.class, int[].class };
- Constructor<?> objStubCons = clsStub.getDeclaredConstructor(clsStubParams);
- Integer[] portsInteger = sMessage.getRMICallbackPorts();
- int[] ports = Arrays.stream(portsInteger).mapToInt(Integer::intValue).toArray();
- int rev = 0;
- String callbackAddress = InetAddress.getLocalHost().getHostAddress(); // Callback address is this machine's address
- Object objStubParams[] = { sMessage.getRMIStubPort(), sMessage.getHostAddress(), callbackAddress,
- rev, ports };
- RuntimeOutput.print("IoTSlave: Creating RMI stub: " +
- sMessage.getHostAddress() + ":" + sMessage.getRMIRegPort() +
- " with callback address: " + callbackAddress + " and RMI stub port: " + sMessage.getRMIStubPort(), BOOL_VERBOSE);
- Object stubObj = objStubCons.newInstance(objStubParams);
- RuntimeOutput.print("IoTSlave: Object name: " + sMessage.getObjectName(), BOOL_VERBOSE);
- RuntimeOutput.print("IoTSlave: Stub address: " + callbackAddress, BOOL_VERBOSE);
- // Class conversion to interface class of this class,
- // e.g. ProximitySensorImpl has ProximitySensor interface
+ Object stubObjConv = null;
+ String strObjectName = sMessage.getObjectName();
String strObjClassInterfaceName = STR_OBJ_CLS_PFX + "." + STR_INTERFACE_PFX + "." +
sMessage.getObjectStubInterfaceName();
Class<?> clsInf = Class.forName(strObjClassInterfaceName);
- Object stubObjConv = clsInf.cast(stubObj);
+ if (mapObjNameStub.containsKey(strObjectName)) {
+ RuntimeOutput.print("IoTSlave: Getting back object on slave: " + strObjectName, BOOL_VERBOSE);
+ stubObjConv = clsInf.cast(mapObjNameStub.get(strObjectName));
+ } else {
+ // Instantiate the stub and put in the object
+ String strObjStubName = sMainObjectName + "." + sMessage.getObjectStubInterfaceName() + STUB_CLASS_SUFFIX;
+ Class<?> clsStub = Class.forName(strObjStubName); // Port number is integer
+ Class[] clsStubParams = { int.class, int.class, int.class, int.class, String.class, int.class };
+ Constructor<?> objStubCons = clsStub.getDeclaredConstructor(clsStubParams);
+
+ int rev = 0;
+ Object objStubParams[] = { 0, 0, sMessage.getRMIStubPort(), sMessage.getRMIRegPort(), sMessage.getHostAddress(), rev };
+ RuntimeOutput.print("IoTSlave: Creating RMI stub: " +
+ sMessage.getHostAddress() + ":" + sMessage.getRMIRegPort() +
+ " and RMI stub port: " + sMessage.getRMIStubPort(), BOOL_VERBOSE);
+ Object stubObj = objStubCons.newInstance(objStubParams);
+ // Class conversion to interface class of this class,
+ // e.g. ProximitySensorImpl has ProximitySensor interface
+ RuntimeOutput.print("IoTSlave: Registering new stub object: " + strObjectName, BOOL_VERBOSE);
+ mapObjNameStub.put(strObjectName, stubObj);
+ stubObjConv = clsInf.cast(stubObj);
+ }
return stubObjConv;
}
private void getIoTSetObject() throws IOException,
ClassNotFoundException, RemoteException, NotBoundException, NoSuchMethodException,
InstantiationException, IllegalAccessException, InvocationTargetException {
-
Object objRegistry = null;
if (CAPAB_BASED_RMI)
objRegistry = getObjectFromStub();
private void getIoTRelationFirstObject() throws IOException,
ClassNotFoundException, RemoteException, NotBoundException, NoSuchMethodException,
InstantiationException, IllegalAccessException, InvocationTargetException {
-
Object objRegistry = null;
if (CAPAB_BASED_RMI)
objRegistry = getObjectFromStub();
private void getIoTRelationSecondObject() throws IOException,
ClassNotFoundException, RemoteException, NotBoundException, NoSuchMethodException,
InstantiationException, IllegalAccessException, InvocationTargetException {
-
Object objRegistry = null;
if (CAPAB_BASED_RMI)
objRegistry = getObjectFromStub();
// Reinitialize IoTSet field after getting all the objects
iotsetObject = new IoTSet<Object>(isetObject.values());
-
// Private fields need getDeclaredField(), while public fields use getField()
Field fld = clsMain.getDeclaredField(strFieldName);
boolean bAccess = fld.isAccessible();
// Reinitialize IoTSet field after getting all the objects
iotrelObject = new IoTRelation<Object,Object>(irelObject.relationMap(), irelObject.size());
-
// Private fields need getDeclaredField(), while public fields use getField()
Field fld = clsMain.getDeclaredField(strFieldName);
boolean bAccess = fld.isAccessible();
// Translating into the actual Message class
MessageGetDeviceObject sMessage = (MessageGetDeviceObject) sIoTMasterMsg;
-
// Get IoTSet objects for IP address set on device driver/controller
IoTDeviceAddress objDeviceAddress = new IoTDeviceAddress(sMessage.getHostAddress(),
sMessage.getSourceDeviceDriverPort(),
// Translating into the actual Message class
MessageGetSimpleDeviceObject sMessage = (MessageGetSimpleDeviceObject) sIoTMasterMsg;
-
// Get IoTSet objects for IP address set on device driver/controller
IoTZigbeeAddress objZBDevAddress = new IoTZigbeeAddress(sMessage.getHostAddress());
RuntimeOutput.print("IoTSlave: Device address transferred: " + sMessage.getHostAddress(), BOOL_VERBOSE);
// Translating into the actual Message class
MessageGetSimpleDeviceObject sMessage = (MessageGetSimpleDeviceObject) sIoTMasterMsg;
-
// Get IoTSet objects for IP address set on device driver/controller
IoTAddress objAddress = new IoTAddress(sMessage.getHostAddress());
RuntimeOutput.print("IoTSlave: Address transferred: " + sMessage.getHostAddress(), BOOL_VERBOSE);