Modified and tested IoTMaster for IoTSet in C++; completed IoTMaster and IoTSlave...
[iot2.git] / iotjava / iotruntime / master / IoTMaster.java
1 package iotruntime.master;
2
3 import iotruntime.*;
4 import iotruntime.slave.IoTAddress;
5 import iotruntime.slave.IoTDeviceAddress;
6 import iotruntime.messages.*;
7
8 // ASM packages
9 import org.objectweb.asm.ClassReader;
10 import org.objectweb.asm.ClassWriter;
11 import org.objectweb.asm.ClassVisitor;
12
13 // Java packages
14 import java.io.*;
15 import java.util.*;
16 import java.io.BufferedReader;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.OutputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.IOException;
26 import java.lang.ClassNotFoundException;
27 import java.lang.Class;
28 import java.lang.reflect.*;
29 import java.net.Socket;
30 import java.net.ServerSocket;
31 import java.nio.ByteBuffer;
32 import java.util.*;
33 import static java.lang.Math.toIntExact;
34
35 /** Class IoTMaster is responsible to use ClassRuntimeInstrumenterMaster
36  *  to instrument the controller/device bytecode and starts multiple
37  *  IoTSlave running on different JVM's in a distributed fashion.
38  *
39  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
40  * @version     1.0
41  * @since       2016-06-16
42  */
43 public class IoTMaster {
44
45         /**
46          * IoTMaster class properties
47          * <p>
48          * CommunicationHandler maintains the data structure for hostnames and ports
49          * LoadBalancer assigns a job onto a host based on certain metrics
50          */
51         private CommunicationHandler commHan;
52         private LoadBalancer lbIoT;
53         private RouterConfig routerConfig;
54         private ObjectInitHandler objInitHand;
55         private ObjectAddressInitHandler objAddInitHand;
56         private String[] strObjectNames;
57         // Now this can be either ClassRuntimeInstrumenterMaster or CRuntimeInstrumenterMaster
58         private Map<String,Object> mapClassNameToCrim;
59
60         /**
61          * These properties hold information of a certain object
62          * at a certain time
63          */
64         private String strObjName;
65         private String strObjClassName;
66         private String strObjClassInterfaceName;
67         private String strObjStubClsIntfaceName;
68         private String strIoTMasterHostAdd;
69         private String strIoTSlaveControllerHostAdd;
70         private String strIoTSlaveObjectHostAdd;
71         private Class[] arrFieldClasses;
72         private Object[] arrFieldValues;
73         private Socket filesocket;
74
75         /**
76          * For connection with C++ IoTSlave
77          */
78         private ServerSocket serverSocketCpp;
79         private Socket socketCpp;
80         private BufferedInputStream inputCpp;
81         private BufferedOutputStream outputCpp;
82
83         // Constants that are to be extracted from config file
84         private static String STR_MASTER_MAC_ADD;
85         private static String STR_IOT_CODE_PATH;
86         private static String STR_CONT_PATH;
87         private static String STR_RUNTIME_DIR;
88         private static String STR_SLAVE_DIR;
89         private static String STR_CLS_PATH;
90         private static String STR_RMI_PATH;
91         private static String STR_RMI_HOSTNAME;
92         private static String STR_LOG_FILE_PATH;
93         private static String STR_USERNAME;
94         private static String STR_ROUTER_ADD;
95         private static String STR_MONITORING_HOST;
96         private static String STR_ZB_GATEWAY_ADDRESS;
97         private static String STR_ZB_GATEWAY_PORT;
98         private static String STR_ZB_IOTMASTER_PORT;
99         private static String STR_NUM_CALLBACK_PORTS;
100         private static String STR_JVM_INIT_HEAP_SIZE;
101         private static String STR_JVM_MAX_HEAP_SIZE;
102         private static String STR_LANGUAGE;
103         private static String STR_SKEL_CLASS_SUFFIX;
104         private static String STR_STUB_CLASS_SUFFIX;
105         private static boolean BOOL_VERBOSE;
106
107         /**
108          * IoTMaster class constants
109          * <p>
110          * Name constants - not to be configured by users
111          */
112         private static final String STR_IOT_MASTER_NAME = "IoTMaster";
113         private static final String STR_CFG_FILE_EXT = ".config";
114         private static final String STR_CLS_FILE_EXT = ".class";
115         private static final String STR_JAR_FILE_EXT = ".jar";
116         private static final String STR_SO_FILE_EXT = ".so";
117         private static final String STR_ZIP_FILE_EXT = ".zip";
118         private static final String STR_TCP_PROTOCOL = "tcp";
119         private static final String STR_UDP_PROTOCOL = "udp";
120         private static final String STR_TCPGW_PROTOCOL = "tcpgw";
121         private static final String STR_NO_PROTOCOL = "nopro";
122         private static final String STR_SELF_MAC_ADD = "00:00:00:00:00:00";
123         private static final String STR_INTERFACE_CLS_CFG = "INTERFACE_CLASS";
124         private static final String STR_INT_STUB_CLS_CFG = "INTERFACE_STUB_CLASS";
125         private static final String STR_FILE_TRF_CFG = "ADDITIONAL_ZIP_FILE";
126         private static final String STR_YES = "Yes";
127         private static final String STR_NO = "No";
128         private static final String STR_JAVA = "Java";
129         private static final String STR_CPP = "C++";
130         private static final String STR_SSH = "ssh";
131         private static final String STR_SCP = "scp";
132         private static final String STR_IOTSLAVE_CPP = "./IoTSlave.o";
133
134         private static int INT_SIZE = 4;        // send length in the size of integer (4 bytes)
135
136         /**
137          * Runtime class name constants - not to be configured by users
138          */
139         private static final String STR_REL_INSTRUMENTER_CLS = "iotruntime.master.RelationInstrumenter";
140         private static final String STR_SET_INSTRUMENTER_CLS = "iotruntime.master.SetInstrumenter";
141         private static final String STR_IOT_SLAVE_CLS = "iotruntime.slave.IoTSlave";
142         private static final String STR_IOT_DEV_ADD_CLS = "IoTDeviceAddress";
143         private static final String STR_IOT_ZB_ADD_CLS = "IoTZigbeeAddress";
144         private static final String STR_IOT_ADD_CLS = "IoTAddress";
145         
146         /**
147          * Class constructor
148          *
149          */
150         public IoTMaster(String[] argObjNms) {
151
152                 commHan = null;
153                 lbIoT = null;
154                 routerConfig = null;
155                 objInitHand = null;
156                 objAddInitHand = null;
157                 strObjectNames = argObjNms;
158                 strObjName = null;
159                 strObjClassName = null;
160                 strObjClassInterfaceName = null;
161                 strObjStubClsIntfaceName = null;
162                 strIoTMasterHostAdd = null;
163                 strIoTSlaveControllerHostAdd = null;
164                 strIoTSlaveObjectHostAdd = null;
165                 arrFieldClasses = null;
166                 arrFieldValues = null;
167                 filesocket = null;
168                 mapClassNameToCrim = null;
169                 // Connection with C++ IoTSlave
170                 serverSocketCpp = null;
171                 socketCpp = null;
172                 inputCpp = null;
173                 outputCpp = null;
174
175                 STR_MASTER_MAC_ADD = null;
176                 STR_IOT_CODE_PATH = null;
177                 STR_CONT_PATH = null;
178                 STR_RUNTIME_DIR = null;
179                 STR_SLAVE_DIR = null;
180                 STR_CLS_PATH = null;
181                 STR_RMI_PATH = null;
182                 STR_RMI_HOSTNAME = null;
183                 STR_LOG_FILE_PATH = null;
184                 STR_USERNAME = null;
185                 STR_ROUTER_ADD = null;
186                 STR_MONITORING_HOST = null;
187                 STR_ZB_GATEWAY_ADDRESS = null;
188                 STR_ZB_GATEWAY_PORT = null;
189                 STR_ZB_IOTMASTER_PORT = null;
190                 STR_NUM_CALLBACK_PORTS = null;
191                 STR_JVM_INIT_HEAP_SIZE = null;
192                 STR_JVM_MAX_HEAP_SIZE = null;
193                 STR_LANGUAGE = null;
194                 BOOL_VERBOSE = false;
195         }
196
197         /**
198          * A method to initialize CommunicationHandler, LoadBalancer, RouterConfig and ObjectInitHandler
199          *
200          * @return void
201          */
202         private void initLiveDataStructure() {
203
204                 commHan = new CommunicationHandler(BOOL_VERBOSE);
205                 lbIoT = new LoadBalancer(BOOL_VERBOSE);
206                 lbIoT.setupLoadBalancer();
207                 routerConfig = new RouterConfig();
208                 routerConfig.getAddressList(STR_ROUTER_ADD);
209                 objInitHand = new ObjectInitHandler(BOOL_VERBOSE);
210                 objAddInitHand = new ObjectAddressInitHandler(BOOL_VERBOSE);
211                 mapClassNameToCrim = new HashMap<String,Object>();
212         }
213
214         /**
215          * A method to initialize constants from config file
216          *
217          * @return void
218          */
219         private void parseIoTMasterConfigFile() {
220                 // Parse configuration file
221                 Properties prop = new Properties();
222                 String strCfgFileName = STR_IOT_MASTER_NAME + STR_CFG_FILE_EXT;
223                 File file = new File(strCfgFileName);
224                 FileInputStream fis = null;
225                 try {
226                         fis = new FileInputStream(file);
227                         prop.load(fis);
228                         fis.close();
229                 } catch (IOException ex) {
230                         System.out.println("IoTMaster: Error reading config file: " + strCfgFileName);
231                         ex.printStackTrace();
232                 }
233                 // Initialize constants from config file
234                 STR_MASTER_MAC_ADD = prop.getProperty("MAC_ADDRESS");
235                 STR_IOT_CODE_PATH = prop.getProperty("IOT_CODE_PATH");
236                 STR_CONT_PATH = prop.getProperty("CONTROLLERS_CODE_PATH");
237                 STR_RUNTIME_DIR = prop.getProperty("RUNTIME_DIR");
238                 STR_SLAVE_DIR = prop.getProperty("SLAVE_DIR");
239                 STR_CLS_PATH = prop.getProperty("CLASS_PATH");
240                 STR_RMI_PATH = prop.getProperty("RMI_PATH");
241                 STR_RMI_HOSTNAME = prop.getProperty("RMI_HOSTNAME");
242                 STR_LOG_FILE_PATH = prop.getProperty("LOG_FILE_PATH");
243                 STR_USERNAME = prop.getProperty("USERNAME");
244                 STR_ROUTER_ADD = prop.getProperty("ROUTER_ADD");
245                 STR_MONITORING_HOST = prop.getProperty("MONITORING_HOST");
246                 STR_ZB_GATEWAY_ADDRESS = prop.getProperty("ZIGBEE_GATEWAY_ADDRESS");
247                 STR_ZB_GATEWAY_PORT = prop.getProperty("ZIGBEE_GATEWAY_PORT");
248                 STR_ZB_IOTMASTER_PORT = prop.getProperty("ZIGBEE_IOTMASTER_PORT");
249                 STR_NUM_CALLBACK_PORTS = prop.getProperty("NUMBER_CALLBACK_PORTS");
250                 STR_JVM_INIT_HEAP_SIZE = prop.getProperty("JVM_INIT_HEAP_SIZE");
251                 STR_JVM_MAX_HEAP_SIZE = prop.getProperty("JVM_MAX_HEAP_SIZE");
252                 STR_LANGUAGE = prop.getProperty("LANGUAGE");
253                 STR_SKEL_CLASS_SUFFIX = prop.getProperty("SKEL_CLASS_SUFFIX");
254                 STR_STUB_CLASS_SUFFIX = prop.getProperty("STUB_CLASS_SUFFIX");
255                 if(prop.getProperty("VERBOSE").equals(STR_YES)) {
256                         BOOL_VERBOSE = true;
257                 }
258
259                 RuntimeOutput.print("IoTMaster: Extracting information from config file: " + strCfgFileName, BOOL_VERBOSE);
260                 RuntimeOutput.print("STR_MASTER_MAC_ADD=" + STR_MASTER_MAC_ADD, BOOL_VERBOSE);
261                 RuntimeOutput.print("STR_IOT_CODE_PATH=" + STR_IOT_CODE_PATH, BOOL_VERBOSE);
262                 RuntimeOutput.print("STR_CONT_PATH=" + STR_CONT_PATH, BOOL_VERBOSE);
263                 RuntimeOutput.print("STR_RUNTIME_DIR=" + STR_RUNTIME_DIR, BOOL_VERBOSE);
264                 RuntimeOutput.print("STR_SLAVE_DIR=" + STR_SLAVE_DIR, BOOL_VERBOSE);
265                 RuntimeOutput.print("STR_CLS_PATH=" + STR_CLS_PATH, BOOL_VERBOSE);
266                 RuntimeOutput.print("STR_RMI_PATH=" + STR_RMI_PATH, BOOL_VERBOSE);
267                 RuntimeOutput.print("STR_RMI_HOSTNAME=" + STR_RMI_HOSTNAME, BOOL_VERBOSE);
268                 RuntimeOutput.print("STR_LOG_FILE_PATH=" + STR_LOG_FILE_PATH, BOOL_VERBOSE);
269                 RuntimeOutput.print("STR_USERNAME=" + STR_USERNAME, BOOL_VERBOSE);
270                 RuntimeOutput.print("STR_ROUTER_ADD=" + STR_ROUTER_ADD, BOOL_VERBOSE);
271                 RuntimeOutput.print("STR_MONITORING_HOST=" + STR_MONITORING_HOST, BOOL_VERBOSE);
272                 RuntimeOutput.print("STR_ZB_GATEWAY_ADDRESS=" + STR_ZB_GATEWAY_ADDRESS, BOOL_VERBOSE);
273                 RuntimeOutput.print("STR_ZB_GATEWAY_PORT=" + STR_ZB_GATEWAY_PORT, BOOL_VERBOSE);
274                 RuntimeOutput.print("STR_ZB_IOTMASTER_PORT=" + STR_ZB_IOTMASTER_PORT, BOOL_VERBOSE);
275                 RuntimeOutput.print("STR_NUM_CALLBACK_PORTS=" + STR_NUM_CALLBACK_PORTS, BOOL_VERBOSE);
276                 RuntimeOutput.print("STR_JVM_INIT_HEAP_SIZE=" + STR_JVM_INIT_HEAP_SIZE, BOOL_VERBOSE);
277                 RuntimeOutput.print("STR_JVM_MAX_HEAP_SIZE=" + STR_JVM_MAX_HEAP_SIZE, BOOL_VERBOSE);
278                 RuntimeOutput.print("STR_LANGUAGE=" + STR_LANGUAGE, BOOL_VERBOSE);
279                 RuntimeOutput.print("STR_SKEL_CLASS_SUFFIX=" + STR_SKEL_CLASS_SUFFIX, BOOL_VERBOSE);
280                 RuntimeOutput.print("STR_STUB_CLASS_SUFFIX=" + STR_STUB_CLASS_SUFFIX, BOOL_VERBOSE);
281                 RuntimeOutput.print("BOOL_VERBOSE=" + BOOL_VERBOSE, BOOL_VERBOSE);
282                 RuntimeOutput.print("IoTMaster: Information extracted successfully!", BOOL_VERBOSE);
283         }
284
285         /**
286          * A method to parse information from a config file
287          *
288          * @param       strCfgFileName  Config file name
289          * @param       strCfgField             Config file field name
290          * @return      String
291          */
292         private String parseConfigFile(String strCfgFileName, String strCfgField) {
293                 // Parse configuration file
294                 Properties prop = new Properties();
295                 File file = new File(strCfgFileName);
296                 FileInputStream fis = null;
297                 try {
298                         fis = new FileInputStream(file);
299                         prop.load(fis);
300                         fis.close();
301                 } catch (IOException ex) {
302                         System.out.println("IoTMaster: Error reading config file: " + strCfgFileName);
303                         ex.printStackTrace();
304                 }
305                 System.out.println("IoTMaster: Reading " + strCfgField +
306                         " from config file: " + strCfgFileName + " with value: " + 
307                         prop.getProperty(strCfgField, null));
308                 // NULL is returned if the property isn't found
309                 return prop.getProperty(strCfgField, null);
310         }
311
312         /**
313          * A method to send files from IoTMaster
314          *
315          * @param  filesocket File socket object
316          * @param  sFileName  File name
317          * @param  lFLength   File length
318          * @return            void
319          */
320         private void sendFile(Socket filesocket, String sFileName, long lFLength) throws IOException {
321
322                 File file = new File(sFileName);
323                 byte[] bytFile = new byte[toIntExact(lFLength)];
324                 InputStream inFileStream = new FileInputStream(file);
325
326                 OutputStream outFileStream = filesocket.getOutputStream();
327                 int iCount;
328                 while ((iCount = inFileStream.read(bytFile)) > 0) {
329                         outFileStream.write(bytFile, 0, iCount);
330                 }
331                 filesocket.close();
332                 RuntimeOutput.print("IoTMaster: File sent!", BOOL_VERBOSE);
333         }
334
335         /**
336          * A method to create a thread
337          *
338          * @param  sSSHCmd    SSH command
339          * @return            void
340          */
341         private void createThread(String sSSHCmd) throws IOException {
342
343                 // Start a new thread to start a new JVM
344                 new Thread() {
345                         Runtime runtime = Runtime.getRuntime();
346                         Process process = runtime.exec(sSSHCmd);
347                 }.start();
348                 RuntimeOutput.print("Executing: " + sSSHCmd, BOOL_VERBOSE);
349         }
350
351         /**
352          * A method to send command from master and receive reply from slave
353          *
354          * @params  msgSend     Message object
355          * @params  strPurpose  String that prints purpose message
356          * @params  inStream    Input stream
357          * @params  outStream   Output stream
358          * @return  void
359          */
360         private void commMasterToSlave(Message msgSend, String strPurpose,
361                 InputStream _inStream, OutputStream _outStream)  
362                         throws IOException, ClassNotFoundException {
363
364                 // Send message/command from master
365                 ObjectOutputStream outStream = (ObjectOutputStream) _outStream;
366                 outStream.writeObject(msgSend);
367                 RuntimeOutput.print("IoTMaster: Send message: " + strPurpose, BOOL_VERBOSE);
368
369                 // Get reply from slave as acknowledgment
370                 ObjectInputStream inStream = (ObjectInputStream) _inStream;
371                 Message msgReply = (Message) inStream.readObject();
372                 RuntimeOutput.print("IoTMaster: Reply message: " + msgReply.getMessage(), BOOL_VERBOSE);
373         }
374
375         /**
376          * A private method to instrument IoTSet device
377          *
378          * @params  strFieldIdentifier        String field name + object ID
379          * @params  strFieldName              String field name
380          * @params  strIoTSlaveObjectHostAdd  String slave host address
381          * @params  inStream                  ObjectInputStream communication
382          * @params  inStream                  ObjectOutputStream communication
383          * @return  void
384          */
385         private void instrumentIoTSetDevice(String strFieldIdentifier, String strObjName, String strFieldName, String strIoTSlaveObjectHostAdd,
386                 InputStream inStream, OutputStream outStream)  
387                         throws IOException, ClassNotFoundException, InterruptedException {
388
389                 // Get information from the set
390                 List<Object[]> listObject = objAddInitHand.getFields(strFieldIdentifier);
391                 // Create a new IoTSet
392                 if(STR_LANGUAGE.equals(STR_JAVA)) {
393                         Message msgCrtIoTSet = new MessageCreateSetRelation(IoTCommCode.CREATE_NEW_IOTSET, strFieldName);
394                         commMasterToSlave(msgCrtIoTSet, "Create new IoTSet for IoTDeviceAddress!", inStream, outStream);
395                 } else
396                         createNewIoTSetCpp(strFieldName, outStream, inStream);
397                 int iRows = listObject.size();
398                 RuntimeOutput.print("IoTMaster: Number of rows for IoTDeviceAddress: " + iRows, BOOL_VERBOSE);
399                 // Transfer the address
400                 for(int iRow=0; iRow<iRows; iRow++) {
401                         arrFieldValues = listObject.get(iRow);
402                         // Get device address - if 00:00:00:00:00:00 that means it needs the driver object address (self)
403                         String strDeviceAddress = null;
404                         String strDeviceAddressKey = null;
405                         if (arrFieldValues[0].equals(STR_SELF_MAC_ADD)) {
406                                 strDeviceAddress = strIoTSlaveObjectHostAdd;
407                                 strDeviceAddressKey = strObjName + "-" + strIoTSlaveObjectHostAdd;
408                         } else {
409                                 strDeviceAddress = routerConfig.getIPFromMACAddress((String) arrFieldValues[0]);
410                                 strDeviceAddressKey = strObjName + "-" + strDeviceAddress;
411                         }
412                         int iDestDeviceDriverPort = (int) arrFieldValues[1];
413                         String strProtocol = (String) arrFieldValues[2];
414                         // Check for wildcard feature                   
415                         boolean bSrcPortWildCard = false;
416                         boolean bDstPortWildCard = false;
417                         if (arrFieldValues.length > 3) {
418                                 bSrcPortWildCard = (boolean) arrFieldValues[3];
419                                 bDstPortWildCard = (boolean) arrFieldValues[4];
420                         }
421                         // Add the port connection into communication handler - if it's not assigned yet
422                         if (commHan.getComPort(strDeviceAddressKey) == null) {
423                                 commHan.addPortConnection(strIoTSlaveObjectHostAdd, strDeviceAddressKey);
424                         }
425
426                         // TODO: DEBUG!!!
427                         System.out.println("\n\n DEBUG: InstrumentSetDevice: Object Name: " + strObjName);
428                         System.out.println("DEBUG: InstrumentSetDevice: Port number: " + commHan.getComPort(strDeviceAddressKey));
429                         System.out.println("DEBUG: InstrumentSetDevice: Device address: " + strDeviceAddressKey + "\n\n");
430
431                         // Send address one by one
432                         if(STR_LANGUAGE.equals(STR_JAVA)) {
433                                 Message msgGetIoTSetObj = null;
434                                 if (bDstPortWildCard) {
435                                         String strUniqueDev = strDeviceAddressKey + ":" + iRow;
436                                         msgGetIoTSetObj = new MessageGetDeviceObject(IoTCommCode.GET_DEVICE_IOTSET_OBJECT,
437                                                 strDeviceAddress, commHan.getAdditionalPort(strUniqueDev), iDestDeviceDriverPort, bSrcPortWildCard, bDstPortWildCard);
438                                 } else
439                                         msgGetIoTSetObj = new MessageGetDeviceObject(IoTCommCode.GET_DEVICE_IOTSET_OBJECT,
440                                                 strDeviceAddress, commHan.getComPort(strDeviceAddressKey), iDestDeviceDriverPort, bSrcPortWildCard, bDstPortWildCard);
441                                 commMasterToSlave(msgGetIoTSetObj, "Get IoTSet objects!", inStream, outStream);
442                         } else
443                                 getDeviceIoTSetObjectCpp(outStream, inStream, strDeviceAddress, commHan.getComPort(strDeviceAddressKey), iDestDeviceDriverPort, 
444                                         bSrcPortWildCard, bDstPortWildCard);
445                 }
446                 // Reinitialize IoTSet on device object
447                 if(STR_LANGUAGE.equals(STR_JAVA))
448                         commMasterToSlave(new MessageSimple(IoTCommCode.REINITIALIZE_IOTSET_FIELD), "Reinitialize IoTSet fields!", inStream, outStream);
449                 else
450                         reinitializeIoTSetFieldCpp(outStream, inStream);
451         }
452
453
454         /**
455          * A private method to instrument IoTSet Zigbee device
456          *
457          * @params  Map.Entry<String,Object>  Entry of map IoTSet instrumentation
458          * @params  strFieldName              String field name
459          * @params  strIoTSlaveObjectHostAdd  String slave host address
460          * @params  inStream                  ObjectInputStream communication
461          * @params  inStream                  ObjectOutputStream communication
462          * @return  void
463          */
464         private void instrumentIoTSetZBDevice(Map.Entry<String,Object> map, String strObjName, String strFieldName, String strIoTSlaveObjectHostAdd,
465                 InputStream inStream, OutputStream outStream)  
466                         throws IOException, ClassNotFoundException, InterruptedException {
467
468                 // Get information from the set
469                 SetInstrumenter setInstrumenter = (SetInstrumenter) map.getValue();
470                 // Create a new IoTSet
471                 if(STR_LANGUAGE.equals(STR_JAVA)) {
472                         Message msgCrtIoTSet = new MessageCreateSetRelation(IoTCommCode.CREATE_NEW_IOTSET, strFieldName);
473                         commMasterToSlave(msgCrtIoTSet, "Create new IoTSet for IoTZigbeeAddress!", inStream, outStream);
474                 } else  // TODO: will need to implement IoTSet Zigbee for C++ later
475                         ;
476                 // Prepare ZigbeeConfig
477                 String strZigbeeGWAddress = routerConfig.getIPFromMACAddress(STR_ZB_GATEWAY_ADDRESS);
478                 String strZigbeeGWAddressKey = strObjName + "-" + strZigbeeGWAddress;
479                 int iZigbeeGWPort = Integer.parseInt(STR_ZB_GATEWAY_PORT);
480                 int iZigbeeIoTMasterPort = Integer.parseInt(STR_ZB_IOTMASTER_PORT);
481                 commHan.addDevicePort(iZigbeeIoTMasterPort);
482                 ZigbeeConfig zbConfig = new ZigbeeConfig(strZigbeeGWAddress, iZigbeeGWPort, iZigbeeIoTMasterPort, 
483                         BOOL_VERBOSE);
484                 // Add the port connection into communication handler - if it's not assigned yet
485                 if (commHan.getComPort(strZigbeeGWAddressKey) == null) {
486                         commHan.addPortConnection(strIoTSlaveObjectHostAdd, strZigbeeGWAddressKey);
487                 }               
488                 int iRows = setInstrumenter.numberOfRows();
489                 RuntimeOutput.print("IoTMaster: Number of rows for IoTZigbeeAddress: " + iRows, BOOL_VERBOSE);
490
491                 // TODO: DEBUG!!!
492                 System.out.println("\n\n DEBUG: InstrumentZigbeeDevice: Object Name: " + strObjName);
493                 System.out.println("DEBUG: InstrumentZigbeeDevice: Port number: " + commHan.getComPort(strZigbeeGWAddressKey));
494                 System.out.println("DEBUG: InstrumentZigbeeDevice: Device address: " + strZigbeeGWAddress + "\n\n");
495
496                 // Transfer the address
497                 for(int iRow=0; iRow<iRows; iRow++) {
498                         arrFieldValues = setInstrumenter.fieldValues(iRow);
499                         // Get device address
500                         String strZBDevAddress = (String) arrFieldValues[0];
501                         // Send policy to Zigbee gateway - TODO: Need to clear policy first?
502                         zbConfig.setPolicy(strIoTSlaveObjectHostAdd, commHan.getComPort(strZigbeeGWAddressKey), strZBDevAddress);
503                         // Send address one by one
504                         if(STR_LANGUAGE.equals(STR_JAVA)) {
505                                 Message msgGetIoTSetZBObj = new MessageGetSimpleDeviceObject(IoTCommCode.GET_ZB_DEV_IOTSET_OBJECT, strZBDevAddress);
506                                 commMasterToSlave(msgGetIoTSetZBObj, "Get IoTSet objects!", inStream, outStream);
507                         } else  // TODO: Implement IoTSet Zigbee for C++
508                                 ;
509                 }
510                 zbConfig.closeConnection();
511                 // Reinitialize IoTSet on device object
512                 commMasterToSlave(new MessageSimple(IoTCommCode.REINITIALIZE_IOTSET_FIELD), "Reinitialize IoTSet fields!", inStream, outStream);
513         }
514
515         
516         /**
517          * A private method to instrument IoTSet of addresses
518          *
519          * @params  strFieldIdentifier        String field name + object ID
520          * @params  strFieldName              String field name
521          * @params  inStream                  ObjectInputStream communication
522          * @params  inStream                  ObjectOutputStream communication
523          * @return  void
524          */
525         private void instrumentIoTSetAddress(String strFieldIdentifier, String strFieldName,
526                 InputStream inStream, OutputStream outStream)  
527                         throws IOException, ClassNotFoundException, InterruptedException {
528
529                 // Get information from the set
530                 List<Object[]> listObject = objAddInitHand.getFields(strFieldIdentifier);
531                 // Create a new IoTSet
532                 if(STR_LANGUAGE.equals(STR_JAVA)) {
533                         Message msgCrtIoTSet = new MessageCreateSetRelation(IoTCommCode.CREATE_NEW_IOTSET, strFieldName);
534                         commMasterToSlave(msgCrtIoTSet, "Create new IoTSet for IoTAddress!", inStream, outStream);
535                 } else
536                         ;
537                 int iRows = listObject.size();
538                 RuntimeOutput.print("IoTMaster: Number of rows for IoTAddress: " + iRows, BOOL_VERBOSE);
539                 // Transfer the address
540                 for(int iRow=0; iRow<iRows; iRow++) {
541                         arrFieldValues = listObject.get(iRow);
542                         // Get device address
543                         String strAddress = (String) arrFieldValues[0];
544                         // Send address one by one
545                         if(STR_LANGUAGE.equals(STR_JAVA)) {
546                                 Message msgGetIoTSetAddObj = new MessageGetSimpleDeviceObject(IoTCommCode.GET_ADD_IOTSET_OBJECT, strAddress);
547                                 commMasterToSlave(msgGetIoTSetAddObj, "Get IoTSet objects!", inStream, outStream);
548                         } else  // TODO: Implement IoTSet Address for C++
549                                 ;
550                 }
551                 // Reinitialize IoTSet on device object
552                 commMasterToSlave(new MessageSimple(IoTCommCode.REINITIALIZE_IOTSET_FIELD),
553                                                                                         "Reinitialize IoTSet fields!", inStream, outStream);
554         }
555
556
557         /**
558          * A private method to instrument an object on a specific machine and setting up policies
559          *
560          * @params  strFieldObjectID  String field object ID
561          * @return  void
562          */
563         private void instrumentObject(String strFieldObjectID) throws IOException {
564
565                 // Extract the interface name for RMI
566                 // e.g. ProximitySensorInterface, TempSensorInterface, etc.
567                 
568                 String strObjCfgFile = STR_IOT_CODE_PATH + strObjClassName + "/" + strObjClassName + STR_CFG_FILE_EXT;
569                 strObjClassInterfaceName = parseConfigFile(strObjCfgFile, STR_INTERFACE_CLS_CFG);
570                 strObjStubClsIntfaceName = parseConfigFile(strObjCfgFile, STR_INT_STUB_CLS_CFG);
571                 // Create an object name, e.g. ProximitySensorImplPS1
572                 strObjName = strObjClassName + strFieldObjectID;
573                 // Check first if host exists
574                 if(commHan.objectExists(strObjName)) {
575                         // If this object exists already ...
576                         // Re-read IoTSlave object hostname for further reference
577                         strIoTSlaveObjectHostAdd = commHan.getHostAddress(strObjName);
578                         RuntimeOutput.print("IoTMaster: Object with name: " + strObjName + " has existed!", BOOL_VERBOSE);
579                 } else {
580                         // If this is a new object ... then create one
581                         // Get host address for IoTSlave from LoadBalancer
582                         //strIoTSlaveObjectHostAdd = lbIoT.selectHost();
583                         strIoTSlaveObjectHostAdd = routerConfig.getIPFromMACAddress(lbIoT.selectHost());
584                         if (strIoTSlaveControllerHostAdd == null)
585                                 throw new Error("IoTMaster: Could not translate MAC to IP address! Please check the router's /tmp/dhcp.leases!");
586                         RuntimeOutput.print("IoTMaster: Object name: " + strObjName, BOOL_VERBOSE);
587                         // Add port connection and get port numbers
588                         // Naming for objects ProximitySensor becomes ProximitySensor0, ProximitySensor1, etc.
589                         commHan.addPortConnection(strIoTSlaveObjectHostAdd, strObjName);
590                         commHan.addActiveControllerObject(strFieldObjectID, strObjName, strObjClassName, strObjClassInterfaceName, 
591                                 strObjStubClsIntfaceName, strIoTSlaveObjectHostAdd, arrFieldValues, arrFieldClasses);
592                         // ROUTING POLICY: IoTMaster and device/controller object
593                         // Master-slave communication
594                         routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTMasterHostAdd,
595                                 strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL, commHan.getComPort(strObjName));
596                         // ROUTING POLICY: Send the same routing policy to both the hosts
597                         routerConfig.configureHostMainPolicies(strIoTMasterHostAdd, strIoTMasterHostAdd,
598                                 strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL, commHan.getComPort(strObjName));
599                         routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTMasterHostAdd,
600                                 strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL, commHan.getComPort(strObjName));
601                         // Need to accommodate callback functions here - open ports for TCP
602                         routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTSlaveControllerHostAdd,
603                                 strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
604                         routerConfig.configureHostMainPolicies(strIoTSlaveControllerHostAdd, strIoTSlaveControllerHostAdd,
605                                 strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
606                         routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveControllerHostAdd,
607                                 strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
608                         // Instrument the IoTSet declarations inside the class file
609                         instrumentObjectIoTSet(strFieldObjectID);
610                 }
611                 // Send routing policy to router for controller object
612                 // ROUTING POLICY: RMI communication - RMI registry and stub ports
613                 routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
614                         STR_TCP_PROTOCOL, commHan.getRMIRegPort(strObjName));
615                 routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
616                         STR_TCP_PROTOCOL, commHan.getRMIStubPort(strObjName));
617                 // Send the same set of routing policies to compute nodes
618                 routerConfig.configureHostMainPolicies(strIoTSlaveControllerHostAdd, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
619                         STR_TCP_PROTOCOL, commHan.getRMIRegPort(strObjName));
620                 routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
621                         STR_TCP_PROTOCOL, commHan.getRMIRegPort(strObjName));
622                 routerConfig.configureHostMainPolicies(strIoTSlaveControllerHostAdd, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
623                         STR_TCP_PROTOCOL, commHan.getRMIStubPort(strObjName));
624                 routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
625                         STR_TCP_PROTOCOL, commHan.getRMIStubPort(strObjName));
626                 // Send the same set of routing policies for callback ports
627                 setCallbackPortsPolicy(strObjName, STR_ROUTER_ADD, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
628         }
629
630         /**
631          * A private method to set router policies for callback ports
632          *
633          * @params  strRouterAdd                                String router address
634          * @params  strIoTSlaveControllerHostAdd        String slave controller host address
635          * @params  strIoTSlaveObjectHostAdd            String slave object host address
636          * @params      strProtocol                                             String protocol
637          * @return  iPort                                                       Integer port number
638          */
639         private void setCallbackPortsPolicy(String strObjName, String strRouterAdd, String strIoTSlaveControllerHostAdd, 
640                 String strIoTSlaveObjectHostAdd, String strProtocol) {
641
642                 int iNumCallbackPorts = Integer.parseInt(STR_NUM_CALLBACK_PORTS);
643                 Integer[] rmiCallbackPorts = commHan.getCallbackPorts(strObjName, iNumCallbackPorts);
644
645                 // Iterate over port numbers and set up policies
646                 for (int i=0; i<iNumCallbackPorts; i++) {
647                         routerConfig.configureRouterMainPolicies(strRouterAdd, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
648                                 strProtocol, rmiCallbackPorts[i]);
649                         routerConfig.configureHostMainPolicies(strIoTSlaveControllerHostAdd, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
650                                 strProtocol, rmiCallbackPorts[i]);
651                         routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveControllerHostAdd, strIoTSlaveObjectHostAdd,
652                                 strProtocol, rmiCallbackPorts[i]);
653                 }
654         }
655
656         /**
657          * A private method to set router policies for IoTDeviceAddress objects
658          *
659          * @params  strFieldIdentifier        String field name + object ID
660          * @params  Map.Entry<String,Object>  Entry of map IoTSet instrumentation
661          * @params  strIoTSlaveObjectHostAdd  String slave host address
662          * @return  void
663          */
664         private void setRouterPolicyIoTSetDevice(String strFieldIdentifier, Map.Entry<String,Object> map, 
665                 String strIoTSlaveObjectHostAdd) {
666
667                 // Get information from the set
668                 SetInstrumenter setInstrumenter = (SetInstrumenter) map.getValue();
669                 int iRows = setInstrumenter.numberOfRows();
670                 RuntimeOutput.print("IoTMaster: Number of rows for IoTDeviceAddress: " + iRows, BOOL_VERBOSE);
671                 // Transfer the address
672                 for(int iRow=0; iRow<iRows; iRow++) {
673                         arrFieldValues = setInstrumenter.fieldValues(iRow);
674                         objAddInitHand.addField(strFieldIdentifier, arrFieldValues);    // Save this for object instantiation
675                         // Get device address - if 00:00:00:00:00:00 that means it needs the driver object address (self)
676                         String strDeviceAddress = null;
677                         String strDeviceAddressKey = null;
678                         if (arrFieldValues[0].equals(STR_SELF_MAC_ADD)) {
679                                 strDeviceAddress = strIoTSlaveObjectHostAdd;
680                                 strDeviceAddressKey = strObjName + "-" + strIoTSlaveObjectHostAdd;
681                         } else {        // Concatenate object name and IP address to give unique key - for a case where there is one device for multiple drivers
682                                 strDeviceAddress = routerConfig.getIPFromMACAddress((String) arrFieldValues[0]);
683                                 strDeviceAddressKey = strObjName + "-" + strDeviceAddress;
684                         }
685                         int iDestDeviceDriverPort = (int) arrFieldValues[1];
686                         String strProtocol = (String) arrFieldValues[2];
687                         // Add the port connection into communication handler - if it's not assigned yet
688                         if (commHan.getComPort(strDeviceAddressKey) == null)
689                                 commHan.addPortConnection(strIoTSlaveObjectHostAdd, strDeviceAddressKey);
690                         boolean bDstPortWildCard = false;
691                         // Recognize this and allocate different ports for it
692                         if (arrFieldValues.length > 3) {
693                                 bDstPortWildCard = (boolean) arrFieldValues[4];
694                                 if (bDstPortWildCard) { // This needs a unique source port
695                                         String strUniqueDev = strDeviceAddressKey + ":" + iRow; 
696                                         commHan.addAdditionalPort(strUniqueDev);
697                                 }
698                         }
699
700                         // TODO: DEBUG!!!
701                         System.out.println("\n\n DEBUG: InstrumentPolicySetDevice: Object Name: " + strObjName);
702                         System.out.println("DEBUG: InstrumentPolicySetDevice: Port number: " + commHan.getComPort(strDeviceAddressKey));
703                         System.out.println("DEBUG: InstrumentPolicySetDevice: Device address: " + strDeviceAddressKey + "\n\n");
704
705                         // Send routing policy to router for device drivers and devices
706                         // ROUTING POLICY: RMI communication - RMI registry and stub ports
707                         if((iDestDeviceDriverPort == -1) && (!strProtocol.equals(STR_NO_PROTOCOL))) {
708                                 // Port number -1 means that we don't set the policy strictly to port number level
709                                 // "nopro" = no protocol specified for just TCP or just UDP (can be both used as well)
710                                 // ROUTING POLICY: Device driver and device
711                                 routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTSlaveObjectHostAdd, strDeviceAddress, strProtocol);
712                                 // ROUTING POLICY: Send to the compute node where the device driver is
713                                 routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveObjectHostAdd, strDeviceAddress, strProtocol);
714                         } else if((iDestDeviceDriverPort == -1) && (strProtocol.equals(STR_NO_PROTOCOL))) {
715                                 routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTSlaveObjectHostAdd, strDeviceAddress);
716                                 routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveObjectHostAdd, strDeviceAddress);
717                         } else if(strProtocol.equals(STR_TCPGW_PROTOCOL)) {
718                                 // This is a TCP protocol that connects, e.g. a phone to our runtime system
719                                 // that provides a gateway access (accessed through destination port number)
720                                 commHan.addDevicePort(iDestDeviceDriverPort);
721                                 routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTSlaveObjectHostAdd, strDeviceAddress, STR_TCP_PROTOCOL, iDestDeviceDriverPort);
722                                 routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveObjectHostAdd, strDeviceAddress, STR_TCP_PROTOCOL, iDestDeviceDriverPort);
723                                 routerConfig.configureRouterHTTPPolicies(STR_ROUTER_ADD, strIoTSlaveObjectHostAdd, strDeviceAddress);
724                                 routerConfig.configureHostHTTPPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveObjectHostAdd, strDeviceAddress);
725                         } else {
726                                 // Other port numbers...
727                                 commHan.addDevicePort(iDestDeviceDriverPort);
728                                 routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTSlaveObjectHostAdd, strDeviceAddress, strProtocol, 
729                                         commHan.getComPort(strDeviceAddressKey), iDestDeviceDriverPort);
730                                 routerConfig.configureHostMainPolicies(strIoTSlaveObjectHostAdd, strIoTSlaveObjectHostAdd, strDeviceAddress, strProtocol, 
731                                         commHan.getComPort(strDeviceAddressKey), iDestDeviceDriverPort);
732                         }
733                 }
734         }
735
736         /**
737          * A private method to set router policies for IoTAddress objects
738          *
739          * @params  strFieldIdentifier        String field name + object ID
740          * @params  Map.Entry<String,Object>  Entry of map IoTSet instrumentation
741          * @params  strHostAddress            String host address
742          * @return  void
743          */
744         private void setRouterPolicyIoTSetAddress(String strFieldIdentifier, Map.Entry<String,Object> map, 
745                 String strHostAddress) {
746
747                 // Get information from the set
748                 SetInstrumenter setInstrumenter = (SetInstrumenter) map.getValue();
749                 int iRows = setInstrumenter.numberOfRows();
750                 RuntimeOutput.print("IoTMaster: Number of rows for IoTAddress: " + iRows, BOOL_VERBOSE);
751                 // Transfer the address
752                 for(int iRow=0; iRow<iRows; iRow++) {
753                         arrFieldValues = setInstrumenter.fieldValues(iRow);
754                         objAddInitHand.addField(strFieldIdentifier, arrFieldValues);    // Save this for object instantiation
755                         // Get device address
756                         String strAddress = (String) arrFieldValues[0];
757                         // Setting up router policies for HTTP/HTTPs
758                         routerConfig.configureRouterHTTPPolicies(STR_ROUTER_ADD, strHostAddress, strAddress);
759                         routerConfig.configureHostHTTPPolicies(strHostAddress, strHostAddress, strAddress);
760                 }
761         }
762
763         /**
764          * A private method to instrument an object's IoTSet and IoTRelation field to up policies
765          * <p>
766          * Mostly the IoTSet fields would contain IoTDeviceAddress objects
767          *
768          * @params  strFieldObjectID  String field object ID
769          * @return  void
770          */
771         private void instrumentObjectIoTSet(String strFieldObjectID) throws IOException {
772
773                 // If this is a new object ... then create one
774                 // Instrument the class source code and look for IoTSet for device addresses
775                 // e.g. @config private IoTSet<IoTDeviceAddress> lb_addresses;
776                 HashMap<String,Object> hmObjectFieldObjects = null;
777                 if(STR_LANGUAGE.equals(STR_JAVA)) {
778                         String strObjectClassNamePath = STR_IOT_CODE_PATH + strObjClassName + "/" + strObjClassName + STR_CLS_FILE_EXT;
779                         FileInputStream fis = new FileInputStream(strObjectClassNamePath);
780                         ClassReader cr = new ClassReader(fis);
781                         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
782                         // We need Object ID to instrument IoTDeviceAddress
783                         ClassRuntimeInstrumenterMaster crim = new ClassRuntimeInstrumenterMaster(cw, strFieldObjectID, BOOL_VERBOSE);
784                         cr.accept(crim, 0);
785                         fis.close();
786                         mapClassNameToCrim.put(strObjClassName + strFieldObjectID, crim);
787                         hmObjectFieldObjects = crim.getFieldObjects();
788                 } else {        // For C++
789                         String strObjectClassNamePath = STR_IOT_CODE_PATH + strObjClassName + "/" + strObjClassName + STR_CFG_FILE_EXT;
790                         CRuntimeInstrumenterMaster crim = new CRuntimeInstrumenterMaster(strObjectClassNamePath, strFieldObjectID, BOOL_VERBOSE);
791                         mapClassNameToCrim.put(strObjClassName + strFieldObjectID, crim);
792                         hmObjectFieldObjects = crim.getFieldObjects();
793                 }
794                 // Get the object and the class names
795                 // Build objects for IoTSet and IoTRelation fields in the device object classes
796                 RuntimeOutput.print("IoTMaster: Going to instrument for " + strObjClassName + " with objectID " + 
797                         strFieldObjectID, BOOL_VERBOSE);
798                 for(Map.Entry<String,Object> map : hmObjectFieldObjects.entrySet()) {
799                         RuntimeOutput.print("IoTMaster: Object name: " + map.getValue().getClass().getName(), BOOL_VERBOSE);
800                         // Iterate over HashMap and choose between processing
801                         String strFieldName = map.getKey();
802                         String strClassName = map.getValue().getClass().getName();
803                         String strFieldIdentifier = strFieldName + strFieldObjectID;
804                         if(strClassName.equals(STR_SET_INSTRUMENTER_CLS)) {
805                                 SetInstrumenter setInstrumenter = (SetInstrumenter) map.getValue();
806                                 if(setInstrumenter.getObjTableName().equals(STR_IOT_DEV_ADD_CLS)) { 
807                                 // Instrument the normal IoTDeviceAddress
808                                         setRouterPolicyIoTSetDevice(strFieldIdentifier, map, strIoTSlaveObjectHostAdd);
809                                 } else if(setInstrumenter.getObjTableName().equals(STR_IOT_ADD_CLS)) { 
810                                 // Instrument the IoTAddress
811                                         setRouterPolicyIoTSetAddress(strFieldIdentifier, map, strIoTSlaveObjectHostAdd);
812                                 } else if(setInstrumenter.getObjTableName().equals(STR_IOT_ZB_ADD_CLS)) { 
813                                 // Instrument the IoTZigbeeAddress - special feature for Zigbee device support
814                                         RuntimeOutput.print("IoTMaster: IoTZigbeeAddress found! No router policy is set here..", 
815                                                 BOOL_VERBOSE);
816                                 } else {
817                                         String strErrMsg = "IoTMaster: Device driver object" +
818                                                                                 " can only have IoTSet<IoTAddress>, IoTSet<IoTDeviceAddress>," +
819                                                                                 " or IoTSet<IoTZigbeeAddress>!";
820                                         throw new Error(strErrMsg);
821                                 }
822                         } else {
823                                 String strErrMsg = "IoTMaster: Device driver object can only have IoTSet for addresses!";
824                                 throw new Error(strErrMsg);
825                         }
826                 }
827         }
828
829
830         /**
831          * A private method to send files to a Java slave driver
832          *
833          * @params  serverSocket                                ServerSocket
834          * @params  _inStream                                   InputStream
835          * @params  _outStream                                  OutputStream
836          * @params  strObjName                                  String
837          * @params  strObjClassName                             String
838          * @params  strObjClassInterfaceName    String
839          * @params  strObjStubClsIntfaceName    String
840          * @params  strIoTSlaveObjectHostAdd    String
841          * @params  strFieldObjectID                    String
842          * @params  arrFieldValues                              Object[]
843          * @params  arrFieldClasses                             Class[]
844          * @return  void
845          */
846         private void sendFileToJavaSlaveDriver(ServerSocket serverSocket, InputStream _inStream, OutputStream _outStream,
847                 String strObjName, String strObjClassName, String strObjClassInterfaceName, String strObjStubClsIntfaceName,
848                 String strIoTSlaveObjectHostAdd, String strFieldObjectID, Object[] arrFieldValues, Class[] arrFieldClasses) 
849                         throws IOException, ClassNotFoundException {
850
851                 ObjectInputStream inStream = (ObjectInputStream) _inStream;
852                 ObjectOutputStream outStream = (ObjectOutputStream) _outStream;
853                 // Create message to transfer file first
854                 String sFileName = strObjClassName + STR_JAR_FILE_EXT;
855                 String sPath = STR_IOT_CODE_PATH + strObjClassName + "/" + sFileName;
856                 File file = new File(sPath);
857                 commMasterToSlave(new MessageSendFile(IoTCommCode.TRANSFER_FILE, sFileName, file.length()),
858                         "Sending file!", inStream, outStream);
859                 // Send file - JAR file for object creation
860                 sendFile(serverSocket.accept(), sPath, file.length());
861                 Message msgReply = (Message) inStream.readObject();
862                 RuntimeOutput.print("IoTMaster: Reply message: " + msgReply.getMessage(), BOOL_VERBOSE);
863                 // Pack object information to create object on a IoTSlave
864                 Message msgObjIoTSlave = new MessageCreateObject(IoTCommCode.CREATE_OBJECT, strIoTSlaveObjectHostAdd,
865                         strObjClassName, strObjName, strObjClassInterfaceName, strObjStubClsIntfaceName, commHan.getRMIRegPort(strObjName), 
866                         commHan.getRMIStubPort(strObjName), arrFieldValues, arrFieldClasses);
867                 // Send message
868                 commMasterToSlave(msgObjIoTSlave, "Sending object information", inStream, outStream);
869         }
870
871
872         /**
873          * A private method to send files to a Java slave driver
874          *
875          * @return  void
876          */
877         private void sendFileToCppSlaveDriver(String strObjClassName, String strIoTSlaveObjectHostAdd) 
878                         throws IOException, ClassNotFoundException {
879
880                 // Create message to transfer file first
881                 String sFileName = strObjClassName + STR_ZIP_FILE_EXT;
882                 String sFile = STR_IOT_CODE_PATH + strObjClassName + "/" + sFileName;
883                 String strCmdSend = STR_SCP + " " + sFile + " " + STR_USERNAME + strIoTSlaveObjectHostAdd + ":" + STR_SLAVE_DIR;
884                 runCommand(strCmdSend);
885                 RuntimeOutput.print("IoTMaster: Executing: " + strCmdSend, BOOL_VERBOSE);
886                 // Unzip file
887                 String strCmdUnzip = STR_SSH + " " + STR_USERNAME + strIoTSlaveObjectHostAdd + " cd " +
888                                         STR_SLAVE_DIR + " sudo unzip " + sFileName + ";";
889                 runCommand(strCmdUnzip);
890                 RuntimeOutput.print("IoTMaster: Executing: " + strCmdUnzip, BOOL_VERBOSE);
891
892         }
893
894
895         /**
896          * Construct command line for Java IoTSlave
897          *
898          * @return       String
899          */
900         private String getCmdJavaDriverIoTSlave(String strIoTMasterHostAdd, String strIoTSlaveObjectHostAdd, String strObjName) {
901
902                 return STR_SSH + " " + STR_USERNAME + strIoTSlaveObjectHostAdd + " cd " + STR_RUNTIME_DIR + " sudo java " +
903                         STR_CLS_PATH + " " + STR_RMI_PATH + " " + STR_RMI_HOSTNAME +
904                         strIoTSlaveObjectHostAdd + " " + STR_IOT_SLAVE_CLS + " " + strIoTMasterHostAdd + " " +
905                         commHan.getComPort(strObjName) + " " + commHan.getRMIRegPort(strObjName) + " " +
906                         commHan.getRMIStubPort(strObjName) + " >& " + STR_LOG_FILE_PATH + strObjName + ".log &";
907         }
908
909
910         /**
911          * Construct command line for C++ IoTSlave
912          *
913          * @return       String
914          */
915         private String getCmdCppDriverIoTSlave(String strIoTMasterHostAdd, String strIoTSlaveObjectHostAdd, String strObjName) {
916
917                 return STR_SSH + " " + STR_USERNAME + strIoTSlaveObjectHostAdd + " cd " +
918                                         STR_SLAVE_DIR + " sudo " + STR_IOTSLAVE_CPP + " " + strIoTMasterHostAdd + " " +
919                                         commHan.getComPort(strObjName) + " " + strObjName;
920         }
921
922
923         /**
924          * A private method to create an object on a specific machine
925          *
926          * @params  strObjName                                  String object name
927          * @params  strObjClassName                     String object class name
928          * @params  strObjClassInterfaceName    String object class interface name
929          * @params  strIoTSlaveObjectHostAdd    String IoTSlave host address
930          * @params  strFieldObjectID                    String field object ID
931          * @params  arrFieldValues                              Array of field values
932          * @params  arrFieldClasses                             Array of field classes
933          * @return  void
934          */
935         private void createObject(String strObjName, String strObjClassName, String strObjClassInterfaceName, String strObjStubClsIntfaceName,
936                 String strIoTSlaveObjectHostAdd, String strFieldObjectID, Object[] arrFieldValues, Class[] arrFieldClasses) 
937                 throws IOException, FileNotFoundException, ClassNotFoundException, InterruptedException {
938
939                 // PROFILING
940                 long start = 0;
941                 long result = 0;
942
943                 // PROFILING
944                 start = System.currentTimeMillis();
945
946                 // Construct ssh command line
947                 // e.g. ssh rtrimana@dw-2.eecs.uci.edu cd <path>;
948                 //      java -cp $CLASSPATH:./*.jar
949                 //           -Djava.rmi.server.codebase=file:./*.jar
950                 //           iotruntime.IoTSlave dw-1.eecs.uci.edu 46151 23829 42874 &
951                 // The In-Port for IoTMaster is the Out-Port for IoTSlave and vice versa
952                 String strSSHCommand = null;
953                 if(STR_LANGUAGE.equals(STR_JAVA))
954                         strSSHCommand = getCmdJavaDriverIoTSlave(strIoTMasterHostAdd, strIoTSlaveObjectHostAdd, strObjName);
955                 else
956                         strSSHCommand = getCmdCppDriverIoTSlave(strIoTMasterHostAdd, strIoTSlaveObjectHostAdd, strObjName);
957
958                 RuntimeOutput.print(strSSHCommand, BOOL_VERBOSE);
959                 // Start a new thread to start a new JVM
960                 createThread(strSSHCommand);
961                 ServerSocket serverSocket = new ServerSocket(commHan.getComPort(strObjName));
962                 Socket socket = serverSocket.accept();
963                 //InputStream inStream = new ObjectInputStream(socket.getInputStream());
964                 //OutputStream outStream = new ObjectOutputStream(socket.getOutputStream());
965                 InputStream inStream = null;
966                 OutputStream outStream = null;
967                 if(STR_LANGUAGE.equals(STR_JAVA)) {
968                         inStream = new ObjectInputStream(socket.getInputStream());
969                         outStream = new ObjectOutputStream(socket.getOutputStream());
970                 } else {        // At this point the language is certainly C++, otherwise would've complained above
971                         inStream = new BufferedInputStream(socket.getInputStream());
972                         outStream = new BufferedOutputStream(socket.getOutputStream());
973                         recvAck(inStream);
974                 }
975
976                 // PROFILING
977                 result = System.currentTimeMillis()-start;
978                 System.out.println("\n\n ==> Time needed to start JVM for " + strObjName + ": " + result + "\n\n");
979
980                 // PROFILING
981                 start = System.currentTimeMillis();
982
983                 if(STR_LANGUAGE.equals(STR_JAVA)) {
984                         sendFileToJavaSlaveDriver(serverSocket, inStream, outStream, strObjName, 
985                                 strObjClassName, strObjClassInterfaceName, strObjStubClsIntfaceName,
986                                 strIoTSlaveObjectHostAdd, strFieldObjectID, arrFieldValues, arrFieldClasses);
987                 } else {
988                         sendFileToCppSlaveDriver(strObjClassName, strIoTSlaveObjectHostAdd);
989                         createObjectCpp(strObjName, strObjClassName, strObjClassInterfaceName, strIoTSlaveObjectHostAdd,
990                         commHan.getRMIRegPort(strObjName), commHan.getRMIStubPort(strObjName), arrFieldValues, arrFieldClasses,
991                         outStream, inStream);
992                 }
993
994                 // PROFILING
995                 result = System.currentTimeMillis()-start;
996                 System.out.println("\n\n ==> Time needed to send JAR file for " + strObjName + ": " + result + "\n\n");
997
998                 // PROFILING
999                 start = System.currentTimeMillis();
1000
1001                 // Instrument the class source code and look for IoTSet for device addresses
1002                 // e.g. @config private IoTSet<IoTDeviceAddress> lb_addresses;
1003                 RuntimeOutput.print("IoTMaster: Instantiating for " + strObjClassName + " with objectID " + strFieldObjectID, BOOL_VERBOSE);
1004                 // Get the object and the class names
1005                 // Build objects for IoTSet and IoTRelation fields in the device object classes
1006                 Object crimObj = mapClassNameToCrim.get(strObjClassName + strFieldObjectID);
1007                 HashMap<String,Object> hmObjectFieldObjects = null;
1008                 if (crimObj instanceof ClassRuntimeInstrumenterMaster) {
1009                         ClassRuntimeInstrumenterMaster crim = (ClassRuntimeInstrumenterMaster) crimObj;
1010                         hmObjectFieldObjects = crim.getFieldObjects();
1011                 } else if (crimObj instanceof CRuntimeInstrumenterMaster) {
1012                         CRuntimeInstrumenterMaster crim = (CRuntimeInstrumenterMaster) crimObj;
1013                         hmObjectFieldObjects = crim.getFieldObjects();
1014                 }
1015                 for(Map.Entry<String,Object> map : hmObjectFieldObjects.entrySet()) {
1016                         RuntimeOutput.print("IoTMaster: Object name: " + map.getValue().getClass().getName(), BOOL_VERBOSE);
1017                         // Iterate over HashMap and choose between processing
1018                         String strFieldName = map.getKey();
1019                         String strClassName = map.getValue().getClass().getName();
1020                         String strFieldIdentifier = strFieldName + strFieldObjectID;
1021                         if(strClassName.equals(STR_SET_INSTRUMENTER_CLS)) {
1022                                 SetInstrumenter setInstrumenter = (SetInstrumenter) map.getValue();
1023                                 if(setInstrumenter.getObjTableName().equals(STR_IOT_DEV_ADD_CLS)) { 
1024                                 // Instrument the normal IoTDeviceAddress
1025                                         synchronized(this) {
1026                                                 instrumentIoTSetDevice(strFieldIdentifier, strObjName, strFieldName, strIoTSlaveObjectHostAdd, inStream, outStream);
1027                                         }
1028                                 } else if(setInstrumenter.getObjTableName().equals(STR_IOT_ZB_ADD_CLS)) { 
1029                                 // Instrument the IoTZigbeeAddress - special feature for Zigbee device support
1030                                         synchronized(this) {
1031                                                 instrumentIoTSetZBDevice(map, strObjName, strFieldName, strIoTSlaveObjectHostAdd, inStream, outStream);
1032                                         }
1033                                 } else if(setInstrumenter.getObjTableName().equals(STR_IOT_ADD_CLS)) { 
1034                                 // Instrument the IoTAddress
1035                                         synchronized(this) {
1036                                                 instrumentIoTSetAddress(strFieldIdentifier, strFieldName, inStream, outStream);
1037                                         }
1038                                 } else {
1039                                         String strErrMsg = "IoTMaster: Device driver object can only have IoTSet<IoTAddress>, IoTSet<IoTDeviceAddress>," +
1040                                                                                 " or IoTSet<IoTZigbeeAddress>!";
1041                                         throw new Error(strErrMsg);
1042                                 }
1043                         } else {
1044                                 String strErrMsg = "IoTMaster: Device driver object can only have IoTSet for addresses!";
1045                                 throw new Error(strErrMsg);
1046                         }
1047                 }
1048                 // End the session
1049                 // TODO: Change this later
1050
1051                 if(STR_LANGUAGE.equals(STR_JAVA)) {
1052                         ObjectOutputStream oStream = (ObjectOutputStream) outStream;
1053                         oStream.writeObject(new MessageSimple(IoTCommCode.END_SESSION));
1054                 } else  // C++ side for now will be running continuously because it's an infinite loop (not in a separate thread)
1055                         //endSessionCpp(outStream);
1056                         ;
1057
1058                 // PROFILING
1059                 result = System.currentTimeMillis()-start;
1060                 System.out.println("\n\n ==> Time needed to create object " + strObjName + " and instrument IoTDeviceAddress: " + result + "\n\n");
1061
1062                 // Closing streams
1063                 outStream.close();
1064                 inStream.close();
1065                 socket.close();
1066                 serverSocket.close();
1067         }
1068
1069
1070         /**
1071          * A private method to create controller objects
1072          *
1073          * @return  void
1074          */
1075         private void createControllerObjects() throws InterruptedException {
1076
1077                 // Create a list of threads
1078                 List<Thread> threads = new ArrayList<Thread>();
1079                 // Get the list of active controller objects and loop it
1080                 List<String> listActiveControllerObject = commHan.getActiveControllerObjectList();
1081                 for(String strObjName : listActiveControllerObject) {
1082
1083                         ObjectCreationInfo objCrtInfo = commHan.getObjectCreationInfo(strObjName);
1084                         Thread objectThread = new Thread(new Runnable() {
1085                                 public void run() {
1086                                         synchronized(this) {
1087                                                 try {
1088                                                         createObject(strObjName, objCrtInfo.getObjectClassName(), objCrtInfo.getObjectClassInterfaceName(),
1089                                                                 objCrtInfo.getObjectStubClassInterfaceName(), objCrtInfo.getIoTSlaveObjectHostAdd(), 
1090                                                                 commHan.getFieldObjectID(strObjName), commHan.getArrayFieldValues(strObjName), 
1091                                                                 commHan.getArrayFieldClasses(strObjName));
1092                                                 } catch (IOException                    | 
1093                                                                  ClassNotFoundException |
1094                                                                  InterruptedException ex) {
1095                                                         ex.printStackTrace();
1096                                                 }
1097                                         }
1098                                 }
1099                         });
1100                         threads.add(objectThread);
1101                         objectThread.start();
1102                 }
1103                 // Join all threads
1104                 for (Thread thread : threads) {
1105                         try {
1106                                 thread.join();
1107                         } catch (InterruptedException ex) {
1108                                 ex.printStackTrace();
1109                         }
1110                 }
1111         }       
1112
1113
1114         /**
1115          * A private method to instrument IoTSet
1116          *
1117          * @params  Map.Entry<String,Object>  Entry of map IoTSet instrumentation
1118          * @params  strFieldName              String field name
1119          * @return  void
1120          */
1121         private void instrumentIoTSet(Map.Entry<String,Object> map, String strFieldName) 
1122                 throws IOException, ClassNotFoundException, InterruptedException {
1123                                 
1124                 // Get information from the set
1125                 SetInstrumenter setInstrumenter = (SetInstrumenter) map.getValue();
1126                 objInitHand.addField(strFieldName, IoTCommCode.CREATE_NEW_IOTSET);
1127
1128                 int iRows = setInstrumenter.numberOfRows();
1129                 for(int iRow=0; iRow<iRows; iRow++) {
1130                         // Get field classes and values
1131                         arrFieldClasses = setInstrumenter.fieldClasses(iRow);
1132                         arrFieldValues = setInstrumenter.fieldValues(iRow);
1133                         // Get object ID and class name
1134                         String strObjID = setInstrumenter.fieldObjectID(iRow);
1135                         strObjClassName = setInstrumenter.fieldEntryType(strObjID);
1136                         // Call the method to create an object
1137                         instrumentObject(strObjID);
1138                         int iNumOfPorts = Integer.parseInt(STR_NUM_CALLBACK_PORTS);
1139                         objInitHand.addObjectIntoField(strFieldName, strIoTSlaveObjectHostAdd, strObjName,
1140                                 strObjClassName, strObjClassInterfaceName, strObjStubClsIntfaceName, commHan.getRMIRegPort(strObjName), 
1141                                 commHan.getRMIStubPort(strObjName), commHan.getCallbackPorts(strObjName, iNumOfPorts));
1142                 }
1143         }
1144
1145
1146         /**
1147          * A private method to instrument IoTRelation
1148          *
1149          * @params  Map.Entry<String,Object>  Entry of map IoTRelation instrumentation
1150          * @params  strFieldName              String field name
1151          * @return  void
1152          */
1153         private void instrumentIoTRelation(Map.Entry<String,Object> map, String strFieldName) 
1154                 throws IOException, ClassNotFoundException, InterruptedException {
1155
1156                         // Get information from the set
1157                 RelationInstrumenter relationInstrumenter = (RelationInstrumenter) map.getValue();
1158                 int iRows = relationInstrumenter.numberOfRows();
1159                 objInitHand.addField(strFieldName, IoTCommCode.CREATE_NEW_IOTRELATION);
1160
1161                 for(int iRow=0; iRow<iRows; iRow++) {
1162                         // Operate on the first set first
1163                         arrFieldClasses = relationInstrumenter.firstFieldClasses(iRow);
1164                         arrFieldValues = relationInstrumenter.firstFieldValues(iRow);
1165                         String strObjID = relationInstrumenter.firstFieldObjectID(iRow);
1166                         strObjClassName = relationInstrumenter.firstEntryFieldType(strObjID);
1167                         // Call the method to create an object
1168                         instrumentObject(strObjID);
1169                         // Get the first object controller host address
1170                         String strFirstIoTSlaveObjectHostAdd = strIoTSlaveObjectHostAdd;
1171                         int iNumOfPorts = Integer.parseInt(STR_NUM_CALLBACK_PORTS);
1172                         objInitHand.addObjectIntoField(strFieldName, strIoTSlaveObjectHostAdd, strObjName,
1173                                 strObjClassName, strObjClassInterfaceName, strObjStubClsIntfaceName, 
1174                                 commHan.getRMIRegPort(strObjName), commHan.getRMIStubPort(strObjName), 
1175                                 commHan.getCallbackPorts(strObjName, iNumOfPorts));
1176                         // Operate on the second set
1177                         arrFieldClasses = relationInstrumenter.secondFieldClasses(iRow);
1178                         arrFieldValues = relationInstrumenter.secondFieldValues(iRow);
1179                         strObjID = relationInstrumenter.secondFieldObjectID(iRow);
1180                         strObjClassName = relationInstrumenter.secondEntryFieldType(strObjID);
1181                         // Call the method to create an object
1182                         instrumentObject(strObjID);
1183                         // Get the second object controller host address
1184                         String strSecondIoTSlaveObjectHostAdd = strIoTSlaveObjectHostAdd;
1185                         objInitHand.addSecondObjectIntoField(strFieldName, strIoTSlaveObjectHostAdd, strObjName,
1186                                 strObjClassName, strObjClassInterfaceName, strObjStubClsIntfaceName, 
1187                                 commHan.getRMIRegPort(strObjName), commHan.getRMIStubPort(strObjName),
1188                                 commHan.getCallbackPorts(strObjName, iNumOfPorts));
1189                         // ROUTING POLICY: first and second controller objects in IoTRelation
1190                         routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strFirstIoTSlaveObjectHostAdd,
1191                                 strSecondIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
1192                         // ROUTING POLICY: Send the same routing policy to both the hosts
1193                         routerConfig.configureHostMainPolicies(strFirstIoTSlaveObjectHostAdd, strFirstIoTSlaveObjectHostAdd,
1194                                 strSecondIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
1195                         routerConfig.configureHostMainPolicies(strSecondIoTSlaveObjectHostAdd, strFirstIoTSlaveObjectHostAdd,
1196                                 strSecondIoTSlaveObjectHostAdd, STR_TCP_PROTOCOL);
1197                 }
1198         }
1199
1200         /**
1201          * A method to reinitialize IoTSet and IoTRelation in the code based on ObjectInitHandler information
1202          *
1203          * @params  inStream                  ObjectInputStream communication
1204          * @params  outStream                 ObjectOutputStream communication
1205          * @return      void
1206          */
1207         private void initializeSetsAndRelationsJava(InputStream inStream, OutputStream outStream)  
1208                 throws IOException, ClassNotFoundException {
1209                 // Get list of fields
1210                 List<String> strFields = objInitHand.getListOfFields();
1211                 // Iterate on HostAddress
1212                 for(String str : strFields) {
1213                         IoTCommCode iotcommMsg = objInitHand.getFieldMessage(str);
1214                         if (iotcommMsg == IoTCommCode.CREATE_NEW_IOTSET) {
1215                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO CREATE IOTSET
1216                                 Message msgCrtIoTSet = new MessageCreateSetRelation(IoTCommCode.CREATE_NEW_IOTSET, str);
1217                                 commMasterToSlave(msgCrtIoTSet, "Create new IoTSet!", inStream, outStream);
1218                                 List<ObjectInitInfo> listObject = objInitHand.getListObjectInitInfo(str);
1219                                 for (ObjectInitInfo objInitInfo : listObject) {
1220                                         // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTSET
1221                                         commMasterToSlave(new MessageGetObject(IoTCommCode.GET_IOTSET_OBJECT, objInitInfo.getIoTSlaveObjectHostAdd(),
1222                                                 objInitInfo.getObjectName(), objInitInfo.getObjectClassName(), objInitInfo.getObjectClassInterfaceName(), 
1223                                                 objInitInfo.getObjectStubClassInterfaceName(), objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(),
1224                                                 objInitInfo.getRMICallbackPorts()), "Get IoTSet object!", inStream, outStream);
1225
1226                                 }
1227                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO REINITIALIZE IOTSET FIELD
1228                                 commMasterToSlave(new MessageSimple(IoTCommCode.REINITIALIZE_IOTSET_FIELD),
1229                                         "Renitialize IoTSet field!", inStream, outStream);
1230                         } else if (iotcommMsg == IoTCommCode.CREATE_NEW_IOTRELATION) {
1231                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO CREATE IOTRELATION
1232                                 Message msgCrtIoTRel = new MessageCreateSetRelation(IoTCommCode.CREATE_NEW_IOTRELATION, str);
1233                                 commMasterToSlave(msgCrtIoTRel, "Create new IoTRelation!", inStream, outStream);
1234                                 List<ObjectInitInfo> listObject = objInitHand.getListObjectInitInfo(str);
1235                                 List<ObjectInitInfo> listSecondObject = objInitHand.getSecondObjectInitInfo(str);
1236                                 Iterator it = listSecondObject.iterator();
1237                                 for (ObjectInitInfo objInitInfo : listObject) {
1238                                         // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTRELATION (FIRST OBJECT)
1239                                         commMasterToSlave(new MessageGetObject(IoTCommCode.GET_IOTRELATION_FIRST_OBJECT, 
1240                                                 objInitInfo.getIoTSlaveObjectHostAdd(), objInitInfo.getObjectName(), objInitInfo.getObjectClassName(),
1241                                                 objInitInfo.getObjectClassInterfaceName(), objInitInfo.getObjectStubClassInterfaceName(),
1242                                                 objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(), objInitInfo.getRMICallbackPorts()), 
1243                                                 "Get IoTRelation first object!", inStream, outStream);
1244                                         ObjectInitInfo objSecObj = (ObjectInitInfo) it.next();
1245                                         // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTRELATION (SECOND OBJECT)
1246                                         commMasterToSlave(new MessageGetObject(IoTCommCode.GET_IOTRELATION_SECOND_OBJECT,
1247                                                 objSecObj.getIoTSlaveObjectHostAdd(), objSecObj.getObjectName(), objSecObj.getObjectClassName(),
1248                                                 objSecObj.getObjectClassInterfaceName(), objSecObj.getObjectStubClassInterfaceName(),
1249                                                 objSecObj.getRMIRegistryPort(), objSecObj.getRMIStubPort(), objSecObj.getRMICallbackPorts()), 
1250                                                 "Get IoTRelation second object!", inStream, outStream);
1251                                 }
1252                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO REINITIALIZE IOTRELATION FIELD
1253                                 commMasterToSlave(new MessageSimple(IoTCommCode.REINITIALIZE_IOTRELATION_FIELD),
1254                                         "Renitialize IoTRelation field!", inStream, outStream);
1255                         }
1256                 }
1257         }
1258
1259         /**
1260          * A method to reinitialize IoTSet and IoTRelation in the code based on ObjectInitHandler information
1261          *
1262          * @params  inStream                  ObjectInputStream communication
1263          * @params  outStream                 ObjectOutputStream communication
1264          * @return      void
1265          */
1266         private void initializeSetsAndRelationsCpp(InputStream inStream, OutputStream outStream)  
1267                 throws IOException, ClassNotFoundException {
1268                 // Get list of fields
1269                 List<String> strFields = objInitHand.getListOfFields();
1270                 // Iterate on HostAddress
1271                 for(String str : strFields) {
1272                         IoTCommCode iotcommMsg = objInitHand.getFieldMessage(str);
1273                         if (iotcommMsg == IoTCommCode.CREATE_NEW_IOTSET) {
1274                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO CREATE IOTSET
1275                                 createNewIoTSetCpp(str, outStream, inStream);
1276                                 List<ObjectInitInfo> listObject = objInitHand.getListObjectInitInfo(str);
1277                                 for (ObjectInitInfo objInitInfo : listObject) {
1278                                         // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTSET
1279                                         getIoTSetRelationObjectCpp(objInitInfo.getIoTSlaveObjectHostAdd(), objInitInfo.getObjectName(), 
1280                                                 objInitInfo.getObjectClassName(), objInitInfo.getObjectClassInterfaceName(), 
1281                                                 objInitInfo.getObjectStubClassInterfaceName(), objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(),
1282                                                 objInitInfo.getRMICallbackPorts(), outStream, inStream);
1283                                 }
1284                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO REINITIALIZE IOTSET FIELD
1285                                 reinitializeIoTSetFieldCpp(outStream, inStream);
1286                         } else if (iotcommMsg == IoTCommCode.CREATE_NEW_IOTRELATION) {
1287                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO CREATE IOTRELATION
1288                                 // TODO: createNewIoTRelation needs to be created here!
1289                                 createNewIoTRelationCpp(str, outStream, inStream);
1290                                 List<ObjectInitInfo> listObject = objInitHand.getListObjectInitInfo(str);
1291                                 List<ObjectInitInfo> listSecondObject = objInitHand.getSecondObjectInitInfo(str);
1292                                 Iterator it = listSecondObject.iterator();
1293                                 for (ObjectInitInfo objInitInfo : listObject) {
1294                                         // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTRELATION (FIRST OBJECT)
1295                                         getIoTSetRelationObjectCpp(objInitInfo.getIoTSlaveObjectHostAdd(), objInitInfo.getObjectName(), 
1296                                                 objInitInfo.getObjectClassName(), objInitInfo.getObjectClassInterfaceName(), 
1297                                                 objInitInfo.getObjectStubClassInterfaceName(), objInitInfo.getRMIRegistryPort(), objInitInfo.getRMIStubPort(),
1298                                                 objInitInfo.getRMICallbackPorts(), outStream, inStream);
1299                                         ObjectInitInfo objSecObj = (ObjectInitInfo) it.next();
1300                                         // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO FILL IN IOTRELATION (SECOND OBJECT)
1301                                         getIoTSetRelationObjectCpp(objSecObj.getIoTSlaveObjectHostAdd(), objSecObj.getObjectName(), 
1302                                                 objSecObj.getObjectClassName(), objSecObj.getObjectClassInterfaceName(), 
1303                                                 objSecObj.getObjectStubClassInterfaceName(), objSecObj.getRMIRegistryPort(), objSecObj.getRMIStubPort(),
1304                                                 objSecObj.getRMICallbackPorts(), outStream, inStream);
1305                                 }
1306                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO REINITIALIZE IOTRELATION FIELD
1307                                 reinitializeIoTRelationFieldCpp(outStream, inStream);
1308                         }
1309                 }
1310         }
1311
1312         /**
1313          * A method to set router basic policies at once
1314          *
1315          * @param       strRouter String router name
1316          * @return      void
1317          */
1318         private void setRouterBasicPolicies(String strRouter) {
1319
1320                 String strMonitorHost = routerConfig.getIPFromMACAddress(STR_MONITORING_HOST);
1321                 routerConfig.configureRouterICMPPolicies(strRouter, strMonitorHost);
1322                 routerConfig.configureRouterDHCPPolicies(strRouter);
1323                 routerConfig.configureRouterDNSPolicies(strRouter);
1324                 routerConfig.configureRouterSSHPolicies(strRouter, strMonitorHost);
1325                 routerConfig.configureRejectPolicies(strRouter);
1326         }
1327
1328         /**
1329          * A method to set host basic policies at once
1330          *
1331          * @param       strHost String host name
1332          * @return      void
1333          */
1334         private void setHostBasicPolicies(String strHost) {
1335
1336                 String strMonitorHost = routerConfig.getIPFromMACAddress(STR_MONITORING_HOST);
1337                 routerConfig.configureHostDHCPPolicies(strHost);
1338                 routerConfig.configureHostDNSPolicies(strHost);
1339                 if (strHost.equals(strMonitorHost)) {
1340                 // Check if this is the monitoring host
1341                         routerConfig.configureHostICMPPolicies(strHost);
1342                         routerConfig.configureHostSSHPolicies(strHost);
1343                 } else {
1344                         routerConfig.configureHostICMPPolicies(strHost, strMonitorHost);
1345                         routerConfig.configureHostSSHPolicies(strHost, strMonitorHost);
1346                 }
1347                 // Apply SQL allowance policies to master host
1348                 if (strHost.equals(strIoTMasterHostAdd)) {
1349                         routerConfig.configureHostSQLPolicies(strHost);
1350                 }
1351                 routerConfig.configureRejectPolicies(strHost);
1352         }
1353
1354         /**
1355          * A method to create a thread for policy deployment
1356          *
1357          * @param  strRouterAddress             String router address to configure
1358          * @param  setHostAddresses             Set of strings for host addresses to configure
1359          * @return                              void
1360          */
1361         private void createPolicyThreads(String strRouterAddress, Set<String> setHostAddresses) throws IOException {
1362
1363                 // Create a list of threads
1364                 List<Thread> threads = new ArrayList<Thread>();
1365                 // Start threads for hosts
1366                 for(String strAddress : setHostAddresses) {
1367                         Thread policyThread = new Thread(new Runnable() {
1368                                 public void run() {
1369                                         synchronized(this) {
1370                                                 routerConfig.sendHostPolicies(strAddress);
1371                                         }
1372                                 }
1373                         });
1374                         threads.add(policyThread);
1375                         policyThread.start();
1376                         RuntimeOutput.print("Deploying policies for: " + strAddress, BOOL_VERBOSE);
1377                 }
1378                 // A thread for router
1379                 Thread policyThread = new Thread(new Runnable() {
1380                         public void run() {
1381                                 synchronized(this) {
1382                                         routerConfig.sendRouterPolicies(strRouterAddress);
1383                                 }
1384                         }
1385                 });
1386                 threads.add(policyThread);
1387                 policyThread.start();
1388                 RuntimeOutput.print("Deploying policies on router: " + strRouterAddress, BOOL_VERBOSE);         
1389                 // Join all threads
1390                 for (Thread thread : threads) {
1391                         try {
1392                                 thread.join();
1393                         } catch (InterruptedException ex) {
1394                                 ex.printStackTrace();
1395                         }
1396                 }
1397         }
1398
1399
1400         /**
1401          * A method to send files to Java IoTSlave
1402          *
1403          * @params  strObjControllerName      String
1404          * @params  serverSocket              ServerSocket
1405          * @params  inStream                  ObjectInputStream communication
1406          * @params  outStream                 ObjectOutputStream communication
1407          * @return       void
1408          */     
1409         private void sendFileToJavaSlave(String strObjControllerName, ServerSocket serverSocket, 
1410                         InputStream _inStream, OutputStream _outStream) throws IOException, ClassNotFoundException {
1411
1412                 ObjectInputStream inStream = (ObjectInputStream) _inStream;
1413                 ObjectOutputStream outStream = (ObjectOutputStream) _outStream;
1414                 // Send .jar file
1415                 String strControllerJarName = strObjControllerName + STR_JAR_FILE_EXT;
1416                 String strControllerJarNamePath = STR_CONT_PATH + strObjControllerName + "/" +
1417                         strControllerJarName;
1418                 File file = new File(strControllerJarNamePath);
1419                 commMasterToSlave(new MessageSendFile(IoTCommCode.TRANSFER_FILE, strControllerJarName, file.length()),
1420                         "Sending file!", inStream, outStream);
1421                 // Send file - Class file for object creation
1422                 sendFile(serverSocket.accept(), strControllerJarNamePath, file.length());
1423                 Message msgReply = (Message) inStream.readObject();
1424                 RuntimeOutput.print("IoTMaster: Reply message: " + msgReply.getMessage(), BOOL_VERBOSE);
1425                 // Send .zip file if additional zip file is specified
1426                 String strObjCfgFile = strObjControllerName + STR_CFG_FILE_EXT;
1427                 String strObjCfgFilePath = STR_CONT_PATH + strObjControllerName + "/" + strObjCfgFile;
1428                 String strAdditionalFile = parseConfigFile(strObjCfgFilePath, STR_FILE_TRF_CFG);
1429                 if (strAdditionalFile.equals(STR_YES)) {
1430                         String strControllerCmpName = strObjControllerName + STR_ZIP_FILE_EXT;
1431                         String strControllerCmpNamePath = STR_CONT_PATH + strObjControllerName + "/" +
1432                                 strControllerCmpName;
1433                         file = new File(strControllerCmpNamePath);
1434                         commMasterToSlave(new MessageSendFile(IoTCommCode.TRANSFER_FILE, strControllerCmpName, file.length()),
1435                                 "Sending file!", inStream, outStream);
1436                         // Send file - Class file for object creation
1437                         sendFile(serverSocket.accept(), strControllerCmpNamePath, file.length());
1438                         msgReply = (Message) inStream.readObject();
1439                         RuntimeOutput.print("IoTMaster: Reply message: " + msgReply.getMessage(), BOOL_VERBOSE);
1440                 }
1441         }
1442
1443
1444         /**
1445          * A method to send files to C++ IoTSlave
1446          *
1447          * @return       void
1448          * TODO: Need to look into this (as of now, file transferred retains the "data" format, 
1449          * hence it is unreadable from outside world
1450          */
1451         private void sendFileToCppSlave(String sFilePath, String sFileName, Socket fileSocket, 
1452                         InputStream inStream, OutputStream outStream) throws IOException {
1453
1454                 sendCommCode(IoTCommCode.TRANSFER_FILE, outStream, inStream);
1455                 // Send file name
1456                 sendString(sFileName, outStream); recvAck(inStream);
1457                 File file = new File(sFilePath + sFileName);
1458                 int iFileLen = toIntExact(file.length());
1459                 RuntimeOutput.print("IoTSlave: Sending file " + sFileName + " with length " + iFileLen + " bytes...", BOOL_VERBOSE);
1460                 // Send file length
1461                 sendInteger(iFileLen, outStream); recvAck(inStream);
1462                 RuntimeOutput.print("IoTSlave: Sent file size!", BOOL_VERBOSE);
1463                 byte[] bytFile = new byte[iFileLen];
1464                 InputStream inFileStream = new FileInputStream(file);
1465                 RuntimeOutput.print("IoTSlave: Opened file!", BOOL_VERBOSE);
1466
1467                 OutputStream outFileStream = fileSocket.getOutputStream();
1468                 RuntimeOutput.print("IoTSlave: Got output stream!", BOOL_VERBOSE);
1469                 int iCount;
1470                 while ((iCount = inFileStream.read(bytFile)) > 0) {
1471                         outFileStream.write(bytFile, 0, iCount);
1472                 }
1473                 RuntimeOutput.print("IoTSlave: File sent!", BOOL_VERBOSE);
1474                 recvAck(inStream);
1475         }
1476
1477
1478         /**
1479          * A method to send files to C++ IoTSlave (now master using Process() to start 
1480          * file transfer using scp)
1481          *
1482          * @return       void
1483          */
1484         private void sendFileToCppSlave(String sFilePath, String sFileName) throws IOException {
1485
1486                 // Construct shell command to transfer file     
1487                 String sFile = sFilePath + sFileName;
1488                 String strCmdSend = STR_SCP + " " + sFile + " " + STR_USERNAME + strIoTSlaveControllerHostAdd + ":" + STR_SLAVE_DIR;
1489                 runCommand(strCmdSend);
1490                 RuntimeOutput.print("IoTMaster: Executing: " + strCmdSend, BOOL_VERBOSE);
1491                 // Unzip file
1492                 String strCmdUnzip = STR_SSH + " " + STR_USERNAME + strIoTSlaveControllerHostAdd + " cd " +
1493                                         STR_SLAVE_DIR + " sudo unzip " + sFileName + ";";
1494                 runCommand(strCmdUnzip);
1495                 RuntimeOutput.print("IoTMaster: Executing: " + strCmdUnzip, BOOL_VERBOSE);
1496         }
1497
1498
1499         /**
1500          * runCommand() method runs shell command
1501          *
1502          * @param   strCommand  String that contains command line
1503          * @return  void
1504          */
1505         private void runCommand(String strCommand) {
1506
1507                 try {
1508                         Runtime runtime = Runtime.getRuntime();
1509                         Process process = runtime.exec(strCommand);
1510                         process.waitFor();
1511                 } catch (IOException ex) {
1512                         System.out.println("RouterConfig: IOException: " + ex.getMessage());
1513                         ex.printStackTrace();
1514                 } catch (InterruptedException ex) {
1515                         System.out.println("RouterConfig: InterruptException: " + ex.getMessage());
1516                         ex.printStackTrace();
1517                 }
1518         }
1519
1520
1521         /**
1522          * Construct command line for Java IoTSlave
1523          *
1524          * @return       String
1525          */
1526         private String getCmdJavaIoTSlave(String strObjControllerName) {
1527
1528                 return STR_SSH + " " + STR_USERNAME + strIoTSlaveControllerHostAdd + " cd " +
1529                                         STR_RUNTIME_DIR + " sudo java " + STR_JVM_INIT_HEAP_SIZE + " " + 
1530                                         STR_JVM_MAX_HEAP_SIZE + " " + STR_CLS_PATH + " " +
1531                                         STR_RMI_PATH + " " + STR_IOT_SLAVE_CLS + " " + strIoTMasterHostAdd + " " +
1532                                         commHan.getComPort(strObjControllerName) + " " +
1533                                         commHan.getRMIRegPort(strObjControllerName) + " " +
1534                                         commHan.getRMIStubPort(strObjControllerName) + " >& " +
1535                                         STR_LOG_FILE_PATH + strObjControllerName + ".log &";
1536         }
1537
1538
1539         /**
1540          * Construct command line for C++ IoTSlave
1541          *
1542          * @return       String
1543          */
1544         private String getCmdCppIoTSlave(String strObjControllerName) {
1545
1546                 return STR_SSH + " " + STR_USERNAME + strIoTSlaveControllerHostAdd + " cd " +
1547                                         STR_SLAVE_DIR + " sudo " + STR_IOTSLAVE_CPP + " " + strIoTMasterHostAdd + " " +
1548                                         commHan.getComPort(strObjControllerName) + " " + strObjControllerName;
1549         }
1550
1551
1552         /**
1553          * sendInteger() sends an integer in bytes
1554          */
1555         public void sendInteger(int intSend, OutputStream outStream) throws IOException {
1556
1557                 BufferedOutputStream output = (BufferedOutputStream) outStream;
1558                 // Transform integer into bytes
1559                 ByteBuffer bb = ByteBuffer.allocate(INT_SIZE);
1560                 bb.putInt(intSend);
1561                 // Send the byte array
1562                 output.write(bb.array(), 0, INT_SIZE);
1563                 output.flush();
1564         }
1565
1566
1567         /**
1568          * recvInteger() receives integer in bytes
1569          */
1570         public int recvInteger(InputStream inStream) throws IOException {
1571
1572                 BufferedInputStream input = (BufferedInputStream) inStream;
1573                 // Wait until input is available
1574                 while(input.available() == 0);
1575                 // Read integer - 4 bytes
1576                 byte[] recvInt = new byte[INT_SIZE];
1577                 input.read(recvInt, 0, INT_SIZE);
1578                 int retVal = ByteBuffer.wrap(recvInt).getInt();
1579
1580                 return retVal;
1581         }
1582
1583
1584         /**
1585          * recvString() receives String in bytes
1586          */
1587         public String recvString(InputStream inStream) throws IOException {
1588
1589                 BufferedInputStream input = (BufferedInputStream) inStream;
1590                 int strLen = recvInteger(inStream);
1591                 // Wait until input is available
1592                 while(input.available() == 0);
1593                 // Read String per strLen
1594                 byte[] recvStr = new byte[strLen];
1595                 input.read(recvStr, 0, strLen);
1596                 String retVal = new String(recvStr);
1597
1598                 return retVal;
1599         }
1600
1601
1602         /**
1603          * sendString() sends a String in bytes
1604          */
1605         public void sendString(String strSend, OutputStream outStream) throws IOException {
1606
1607                 BufferedOutputStream output = (BufferedOutputStream) outStream;
1608                 // Transform String into bytes
1609                 byte[] strSendBytes = strSend.getBytes();
1610                 int strLen = strSend.length();
1611                 // Send the string length first
1612                 sendInteger(strLen, outStream);
1613                 // Send the byte array
1614                 output.write(strSendBytes, 0, strLen);
1615                 output.flush();
1616         }
1617
1618
1619         /**
1620          * Convert integer to enum
1621          */
1622         public IoTCommCode getCode(int intCode) throws IOException {
1623
1624                 IoTCommCode[] commCode = IoTCommCode.values();
1625                 IoTCommCode retCode = commCode[intCode];
1626                 return retCode;
1627
1628         }
1629
1630
1631         /**
1632          * Receive ACK
1633          */
1634         public boolean recvAck(InputStream inStream) throws IOException {
1635
1636                 int intAck = recvInteger(inStream);
1637                 IoTCommCode codeAck = getCode(intAck);
1638                 if (codeAck == IoTCommCode.ACKNOWLEDGED)
1639                         return true;
1640                 return false;
1641
1642         }
1643
1644
1645         /**
1646          * Send END
1647          */
1648         public void sendEndTransfer(OutputStream outStream) throws IOException {
1649
1650                 int endCode = IoTCommCode.END_TRANSFER.ordinal();
1651                 sendInteger(endCode, outStream);
1652         }
1653
1654
1655         /**
1656          * Send communication code to C++
1657          */
1658         public void sendCommCode(IoTCommCode inpCommCode, OutputStream outStream, InputStream inStream) throws IOException {
1659
1660
1661                 IoTCommCode commCode = inpCommCode;
1662                 int intCode = commCode.ordinal();
1663                 // TODO: delete this later
1664                 System.out.println("DEBUG: Sending " + commCode + " with ordinal: " + intCode);
1665                 sendInteger(intCode, outStream); recvAck(inStream);
1666         }
1667
1668
1669         /**
1670          * Create a main controller object for C++
1671          */
1672         public void createMainObjectCpp(String strObjControllerName, OutputStream outStream, InputStream inStream) throws IOException {
1673
1674                 sendCommCode(IoTCommCode.CREATE_MAIN_OBJECT, outStream, inStream);
1675                 String strMainObjName = strObjControllerName;
1676                 sendString(strMainObjName, outStream); recvAck(inStream);
1677                 RuntimeOutput.print("IoTSlave: Create a main object: " + strMainObjName, BOOL_VERBOSE);
1678         }
1679
1680
1681         /**
1682          * A helper function that converts Class into String
1683          *
1684          * @param  strDataType  String MySQL data type
1685          * @return              Class
1686          */
1687         public String getClassConverted(Class<?> cls) {
1688
1689                 if (cls == String.class) {
1690                         return "string";
1691                 } else if (cls == int.class) {
1692                         return "int";
1693                 } else {
1694                         return null;
1695                 }
1696         }
1697
1698
1699         /**
1700          * A helper function that converts Object into String for transfer to C++ slave
1701          *
1702          * @param  obj           Object to be converted
1703          * @param  strClassType  String Java Class type
1704          * @return               Object
1705          */
1706         public String getObjectConverted(Object obj) {
1707
1708                 if (obj instanceof String) {
1709                         return (String) obj;
1710                 } else if (obj instanceof Integer) {
1711                         return Integer.toString((Integer) obj);
1712                 } else {
1713                         return null;
1714                 }
1715         }
1716
1717
1718         /**
1719          * Create a driver object for C++
1720          */
1721         public void createObjectCpp(String strObjName, String strObjClassName, String strObjClassInterfaceName, String strIoTSlaveObjectHostAdd, 
1722                 Integer iRMIRegistryPort, Integer iRMIStubPort, Object[] arrFieldValues, Class[] arrFieldClasses, 
1723                 OutputStream outStream, InputStream inStream) throws IOException {
1724
1725                 sendCommCode(IoTCommCode.CREATE_OBJECT, outStream, inStream);
1726                 RuntimeOutput.print("IoTSlave: Send request to create a driver object... ", BOOL_VERBOSE);
1727                 RuntimeOutput.print("IoTSlave: Driver object name: " + strObjName, BOOL_VERBOSE);
1728                 sendString(strObjName, outStream); recvAck(inStream);
1729                 RuntimeOutput.print("IoTSlave: Driver object class name: " + strObjClassName, BOOL_VERBOSE);
1730                 sendString(strObjClassName, outStream); recvAck(inStream);
1731                 RuntimeOutput.print("IoTSlave: Driver object interface name: " + strObjClassInterfaceName, BOOL_VERBOSE);
1732                 sendString(strObjStubClsIntfaceName, outStream); recvAck(inStream);
1733                 RuntimeOutput.print("IoTSlave: Driver object skeleton class name: " + strObjClassInterfaceName + STR_SKEL_CLASS_SUFFIX, BOOL_VERBOSE);
1734                 sendString(strObjClassInterfaceName + STR_SKEL_CLASS_SUFFIX, outStream); recvAck(inStream);
1735                 RuntimeOutput.print("IoTSlave: Driver object registry port: " + iRMIRegistryPort, BOOL_VERBOSE);
1736                 sendInteger(iRMIRegistryPort, outStream); recvAck(inStream);
1737                 RuntimeOutput.print("IoTSlave: Driver object stub port: " + iRMIStubPort, BOOL_VERBOSE);
1738                 sendInteger(iRMIStubPort, outStream); recvAck(inStream);
1739                 int numOfArgs = arrFieldValues.length;
1740                 RuntimeOutput.print("IoTSlave: Send constructor arguments! Number of arguments: " + numOfArgs, BOOL_VERBOSE);
1741                 sendInteger(numOfArgs, outStream); recvAck(inStream);
1742                 for(Object obj : arrFieldValues) {
1743                         String str = getObjectConverted(obj);
1744                         sendString(str, outStream); recvAck(inStream);
1745                 }
1746                 RuntimeOutput.print("IoTSlave: Send constructor argument classes!", BOOL_VERBOSE);
1747                 for(Class cls : arrFieldClasses) {
1748                         String str = getClassConverted(cls);
1749                         sendString(str, outStream); recvAck(inStream);
1750                 }
1751         }
1752
1753
1754         /**
1755          * Create new IoTSet for C++
1756          */
1757         public void createNewIoTSetCpp(String strObjFieldName, OutputStream outStream, InputStream inStream) throws IOException {
1758
1759                 sendCommCode(IoTCommCode.CREATE_NEW_IOTSET, outStream, inStream);
1760                 RuntimeOutput.print("IoTSlave: Creating new IoTSet...", BOOL_VERBOSE);
1761                 RuntimeOutput.print("IoTSlave: Send object field name: " + strObjFieldName, BOOL_VERBOSE);
1762                 sendString(strObjFieldName, outStream); recvAck(inStream);
1763         }
1764
1765
1766         /**
1767          * Create new IoTRelation for C++
1768          */
1769         public void createNewIoTRelationCpp(String strObjFieldName, OutputStream outStream, InputStream inStream) throws IOException {
1770
1771                 sendCommCode(IoTCommCode.CREATE_NEW_IOTRELATION, outStream, inStream);
1772                 RuntimeOutput.print("IoTSlave: Creating new IoTRelation...", BOOL_VERBOSE);
1773                 RuntimeOutput.print("IoTSlave: Send object field name: " + strObjFieldName, BOOL_VERBOSE);
1774                 sendString(strObjFieldName, outStream); recvAck(inStream);
1775         }
1776
1777
1778         /**
1779          * Get a IoTDeviceAddress object for C++
1780          */
1781         public void getDeviceIoTSetObjectCpp(OutputStream outStream, InputStream inStream,
1782                         String strDeviceAddress, int iSourcePort, int iDestPort, boolean bSourceWildCard, boolean bDestWildCard) throws IOException {
1783
1784                 sendCommCode(IoTCommCode.GET_DEVICE_IOTSET_OBJECT, outStream, inStream);
1785                 RuntimeOutput.print("IoTSlave: Getting IoTDeviceAddress...", BOOL_VERBOSE);
1786                 sendString(strDeviceAddress, outStream); recvAck(inStream);
1787                 sendInteger(iSourcePort, outStream); recvAck(inStream);
1788                 sendInteger(iDestPort, outStream); recvAck(inStream);
1789                 int iSourceWildCard = (bSourceWildCard ? 1 : 0);
1790                 sendInteger(iSourceWildCard, outStream); recvAck(inStream);
1791                 int iDestWildCard = (bDestWildCard ? 1 : 0);
1792                 sendInteger(iDestWildCard, outStream); recvAck(inStream);
1793                 RuntimeOutput.print("IoTSlave: Send device address: " + strDeviceAddress, BOOL_VERBOSE);
1794         }
1795
1796
1797         /**
1798          * Get a IoTSet content object for C++
1799          */
1800         public void getIoTSetRelationObjectCpp(String strIoTSlaveHostAddress, String strObjectName, String strObjectClassName, 
1801                         String strObjectClassInterfaceName, String strObjectStubClassInterfaceName, int iRMIRegistryPort, int iRMIStubPort, 
1802                         Integer[] iCallbackPorts, OutputStream outStream, InputStream inStream) throws IOException {
1803
1804                 sendCommCode(IoTCommCode.GET_IOTSET_OBJECT, outStream, inStream);
1805                 RuntimeOutput.print("IoTSlave: Getting IoTSet object content...", BOOL_VERBOSE);
1806                 // Send info
1807                 RuntimeOutput.print("IoTSlave: Send host address: " + strIoTSlaveHostAddress, BOOL_VERBOSE);
1808                 sendString(strIoTSlaveHostAddress, outStream); recvAck(inStream);
1809                 RuntimeOutput.print("IoTSlave: Driver object name: " + strObjectName, BOOL_VERBOSE);
1810                 sendString(strObjectName, outStream); recvAck(inStream);
1811                 RuntimeOutput.print("IoTSlave: Driver object class name: " + strObjectClassName, BOOL_VERBOSE);
1812                 sendString(strObjectClassName, outStream); recvAck(inStream);
1813                 RuntimeOutput.print("IoTSlave: Driver object interface name: " + strObjectClassInterfaceName, BOOL_VERBOSE);
1814                 sendString(strObjectClassInterfaceName, outStream); recvAck(inStream);
1815                 RuntimeOutput.print("IoTSlave: Driver object stub class name: " + strObjectStubClassInterfaceName + STR_STUB_CLASS_SUFFIX, BOOL_VERBOSE);
1816                 sendString(strObjectStubClassInterfaceName + STR_STUB_CLASS_SUFFIX, outStream); recvAck(inStream);
1817                 RuntimeOutput.print("IoTSlave: Driver object registry port: " + iRMIRegistryPort, BOOL_VERBOSE);
1818                 sendInteger(iRMIRegistryPort, outStream); recvAck(inStream);
1819                 RuntimeOutput.print("IoTSlave: Driver object stub port: " + iRMIStubPort, BOOL_VERBOSE);
1820                 sendInteger(iRMIStubPort, outStream); recvAck(inStream);
1821                 sendInteger(iCallbackPorts.length, outStream); recvAck(inStream);
1822                 for(Integer i : iCallbackPorts) {
1823                         sendInteger(i, outStream); recvAck(inStream);
1824                 }
1825         }
1826
1827
1828         /**
1829          * Reinitialize IoTRelation field for C++
1830          */
1831         private void reinitializeIoTRelationFieldCpp(OutputStream outStream, InputStream inStream) throws IOException {
1832
1833                 RuntimeOutput.print("IoTSlave: About to Reinitialize IoTRelation field!", BOOL_VERBOSE);
1834                 sendCommCode(IoTCommCode.REINITIALIZE_IOTRELATION_FIELD, outStream, inStream);
1835                 RuntimeOutput.print("IoTSlave: Reinitialize IoTRelation field!", BOOL_VERBOSE);
1836         }
1837
1838
1839         /**
1840          * Reinitialize IoTSet field for C++
1841          */
1842         private void reinitializeIoTSetFieldCpp(OutputStream outStream, InputStream inStream) throws IOException {
1843
1844                 RuntimeOutput.print("IoTSlave: About to Reinitialize IoTSet field!", BOOL_VERBOSE);
1845                 sendCommCode(IoTCommCode.REINITIALIZE_IOTSET_FIELD, outStream, inStream);
1846                 RuntimeOutput.print("IoTSlave: Reinitialize IoTSet field!", BOOL_VERBOSE);
1847         }
1848
1849
1850         /**
1851          * Invoke init() for C++
1852          */
1853         private void invokeInitMethodCpp(OutputStream outStream, InputStream inStream) throws IOException {
1854
1855                 sendCommCode(IoTCommCode.INVOKE_INIT_METHOD, outStream, inStream);
1856                 RuntimeOutput.print("IoTSlave: Invoke init method!", BOOL_VERBOSE);
1857         }
1858
1859
1860         /**
1861          * End session for C++
1862          */
1863         public void endSessionCpp(OutputStream outStream) throws IOException {
1864
1865                 // Send message to end session
1866                 IoTCommCode endSessionCode = IoTCommCode.END_SESSION;
1867                 int intCode = endSessionCode.ordinal();
1868                 sendInteger(intCode, outStream);
1869                 //RuntimeOutput.print("IoTSlave: Send request to create a main object: " + strObjName, BOOL_VERBOSE);
1870                 RuntimeOutput.print("IoTSlave: Send request to end session!", BOOL_VERBOSE);
1871         }
1872
1873
1874         /**
1875          * A method to assign objects to multiple JVMs, including
1876          * the controller/device object that uses other objects
1877          * in IoTSet and IoTRelation
1878          *
1879          * @return       void
1880          */
1881         private void createObjects() {
1882
1883                 // PROFILING
1884                 long start = 0;
1885                 long result = 0;
1886
1887                 try {
1888                         // Extract hostname for this IoTMaster from MySQL DB
1889                         strIoTMasterHostAdd = routerConfig.getIPFromMACAddress(STR_MASTER_MAC_ADD);
1890                         // Loop as we can still find controller/device classes
1891                         for(int i=0; i<strObjectNames.length; i++) {
1892                                 // PROFILING
1893                                 start = System.currentTimeMillis();
1894
1895                                 // Assign a new list of PrintWriter objects
1896                                 routerConfig.renewPrintWriter();
1897                                 // Get controller names one by one
1898                                 String strObjControllerName = strObjectNames[i];
1899                                 // Use LoadBalancer to assign a host address
1900                                 //strIoTSlaveControllerHostAdd = lbIoT.selectHost();
1901                                 strIoTSlaveControllerHostAdd = routerConfig.getIPFromMACAddress(lbIoT.selectHost());
1902                                 if (strIoTSlaveControllerHostAdd == null)
1903                                         throw new Error("IoTMaster: Could not translate MAC to IP address! Please check the router's /tmp/dhcp.leases!");
1904                                 // == START INITIALIZING CONTROLLER/DEVICE IOTSLAVE ==
1905                                 // Add port connection and get port numbers
1906                                 // Naming for objects ProximitySensor becomes ProximitySensor0, ProximitySensor1, etc.
1907                                 commHan.addPortConnection(strIoTSlaveControllerHostAdd, strObjControllerName);
1908                                 // ROUTING POLICY: IoTMaster and main controller object
1909                                 routerConfig.configureRouterMainPolicies(STR_ROUTER_ADD, strIoTMasterHostAdd,
1910                                         strIoTSlaveControllerHostAdd, STR_TCP_PROTOCOL, commHan.getComPort(strObjControllerName));
1911                                 // ROUTING POLICY: Send the same routing policy to both the hosts
1912                                 routerConfig.configureHostMainPolicies(strIoTMasterHostAdd, strIoTMasterHostAdd,
1913                                         strIoTSlaveControllerHostAdd, STR_TCP_PROTOCOL, commHan.getComPort(strObjControllerName));
1914                                 routerConfig.configureHostMainPolicies(strIoTSlaveControllerHostAdd, strIoTMasterHostAdd,
1915                                         strIoTSlaveControllerHostAdd, STR_TCP_PROTOCOL, commHan.getComPort(strObjControllerName));
1916
1917                                 // Construct ssh command line and create a controller thread for e.g. AcmeProximity
1918                                 String strSSHCommand = null;
1919                                 if(STR_LANGUAGE.equals(STR_JAVA))
1920                                         strSSHCommand = getCmdJavaIoTSlave(strObjControllerName);
1921                                 else if(STR_LANGUAGE.equals(STR_CPP))
1922                                         strSSHCommand = getCmdCppIoTSlave(strObjControllerName);
1923                                 else
1924                                         throw new Error("IoTMaster: Language specification not recognized: " + STR_LANGUAGE);
1925                                 RuntimeOutput.print(strSSHCommand, BOOL_VERBOSE);
1926                                 createThread(strSSHCommand);
1927                                 // Wait for connection
1928                                 // Create a new socket for communication
1929                                 ServerSocket serverSocket = new ServerSocket(commHan.getComPort(strObjControllerName));
1930                                 Socket socket = serverSocket.accept();
1931                                 InputStream inStream = null;
1932                                 OutputStream outStream = null;
1933                                 if(STR_LANGUAGE.equals(STR_JAVA)) {
1934                                         inStream = new ObjectInputStream(socket.getInputStream());
1935                                         outStream = new ObjectOutputStream(socket.getOutputStream());
1936                                 } else {        // At this point the language is certainly C++, otherwise would've complained above
1937                                         inStream = new BufferedInputStream(socket.getInputStream());
1938                                         outStream = new BufferedOutputStream(socket.getOutputStream());
1939                                         recvAck(inStream);
1940                                 }
1941                                 RuntimeOutput.print("IoTMaster: Communication established!", BOOL_VERBOSE);
1942
1943                                 // PROFILING
1944                                 result = System.currentTimeMillis()-start;
1945                                 System.out.println("\n\n ==> From start until after SSH for main controller: " + result);
1946                                 // PROFILING
1947                                 start = System.currentTimeMillis();
1948
1949                                 // Send files for every controller class
1950                                 // e.g. AcmeProximity.jar and AcmeProximity.zip
1951                                 String strControllerClassName = strObjControllerName + STR_CLS_FILE_EXT;
1952                                 String strControllerClassNamePath = STR_CONT_PATH + strObjControllerName + "/" +
1953                                         strControllerClassName;
1954
1955                                 if(STR_LANGUAGE.equals(STR_JAVA)) {
1956                                         sendFileToJavaSlave(strObjControllerName, serverSocket, inStream, outStream);
1957                                         // Create main controller/device object
1958                                         commMasterToSlave(new MessageCreateMainObject(IoTCommCode.CREATE_MAIN_OBJECT, strObjControllerName),
1959                                                 "Create main object!", inStream, outStream);
1960                                 } else {
1961                                         String strControllerZipFile = strObjControllerName + STR_ZIP_FILE_EXT;
1962                                         String strControllerFilePath = STR_CONT_PATH + strObjControllerName + "/";
1963                                         sendFileToCppSlave(strControllerFilePath, strControllerZipFile);
1964                                         createMainObjectCpp(strObjControllerName, outStream, inStream);
1965                                 }
1966
1967                                 // PROFILING
1968                                 result = System.currentTimeMillis()-start;
1969                                 System.out.println("\n\n ==> From IoTSlave start until main controller object is created: " + result);
1970                                 System.out.println(" ==> Including file transfer times!\n\n");
1971                                 // PROFILING
1972                                 start = System.currentTimeMillis();
1973
1974                                 // == END INITIALIZING CONTROLLER/DEVICE IOTSLAVE ==
1975                                 // Instrumenting one file
1976                                 RuntimeOutput.print("IoTMaster: Opening class file: " + strControllerClassName, BOOL_VERBOSE);
1977                                 RuntimeOutput.print("IoTMaster: Class file path: " + strControllerClassNamePath, BOOL_VERBOSE);
1978                                 HashMap<String,Object> hmControllerFieldObjects = null;
1979                                 if(STR_LANGUAGE.equals(STR_JAVA)) {
1980                                         FileInputStream fis = new FileInputStream(strControllerClassNamePath);
1981                                         ClassReader cr = new ClassReader(fis);
1982                                         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
1983                                         ClassRuntimeInstrumenterMaster crim = new ClassRuntimeInstrumenterMaster(cw, null, BOOL_VERBOSE);
1984                                         cr.accept(crim, 0);
1985                                         fis.close();
1986                                         hmControllerFieldObjects = crim.getFieldObjects();
1987                                 } else {
1988                                         String strControllerConfigFile = STR_CONT_PATH + strObjControllerName + "/" + strObjControllerName + STR_CFG_FILE_EXT;
1989                                         CRuntimeInstrumenterMaster crim = new CRuntimeInstrumenterMaster(strControllerConfigFile, null, BOOL_VERBOSE);
1990                                         hmControllerFieldObjects = crim.getFieldObjects();
1991                                 }
1992                                 // Get the object and the class names
1993                                 // Build objects for IoTSet and IoTRelation fields in the controller/device classes
1994                                 //HashMap<String,Object> hmControllerFieldObjects = crim.getFieldObjects();
1995                                 for(Map.Entry<String,Object> map : hmControllerFieldObjects.entrySet()) {
1996                                         RuntimeOutput.print("IoTMaster: Object name: " + map.getValue().getClass().getName(), BOOL_VERBOSE);
1997                                         // Iterate over HashMap and choose between processing
1998                                         // SetInstrumenter vs. RelationInstrumenter
1999                                         String strFieldName = map.getKey();
2000                                         String strClassName = map.getValue().getClass().getName();
2001                                         if(strClassName.equals(STR_SET_INSTRUMENTER_CLS)) {
2002                                                 SetInstrumenter setInstrumenter = (SetInstrumenter) map.getValue();
2003                                                 if(setInstrumenter.getObjTableName().equals(STR_IOT_DEV_ADD_CLS)) { 
2004                                                         String strErrMsg = "IoTMaster: Controller object" +
2005                                                                 " cannot have IoTSet<IoTDeviceAddress>!";
2006                                                         throw new Error(strErrMsg);
2007                                                 } else if(setInstrumenter.getObjTableName().equals(STR_IOT_ZB_ADD_CLS)) { 
2008                                                         String strErrMsg = "IoTMaster: Controller object" +
2009                                                                 " cannot have IoTSet<ZigbeeAddress>!";
2010                                                         throw new Error(strErrMsg);
2011                                                 } else if(setInstrumenter.getObjTableName().equals(STR_IOT_ADD_CLS)) { 
2012                                                 // Instrument the IoTAddress
2013                                                         setRouterPolicyIoTSetAddress(strFieldName, map, strIoTSlaveControllerHostAdd);
2014                                                         instrumentIoTSetAddress(strFieldName, strFieldName, inStream, outStream);
2015                                                 } else {
2016                                                 // Any other cases
2017                                                         instrumentIoTSet(map, strFieldName);
2018                                                 }
2019                                         } else if (strClassName.equals(STR_REL_INSTRUMENTER_CLS)) {
2020                                                 instrumentIoTRelation(map, strFieldName);
2021                                         }
2022                                 }
2023                                 // PROFILING
2024                                 result = System.currentTimeMillis()-start;
2025                                 System.out.println("\n\n ==> Time needed to instrument device driver objects: " + result + "\n\n");
2026                                 System.out.println(" ==> #Objects: " + commHan.getActiveControllerObjectList().size() + "\n\n");
2027
2028                                 // PROFILING
2029                                 start = System.currentTimeMillis();
2030
2031                                 // ROUTING POLICY: Deploy basic policies if this is the last controller
2032                                 if (i == strObjectNames.length-1) {
2033                                         // ROUTING POLICY: implement basic policies to reject all other irrelevant traffics
2034                                         for(String s: commHan.getHosts()) {
2035                                                 setHostBasicPolicies(s);
2036                                         }
2037                                         // We retain all the basic policies for router, 
2038                                         // but we delete the initial allowance policies for internal all TCP and UDP communications
2039                                         setRouterBasicPolicies(STR_ROUTER_ADD);
2040                                 }
2041                                 // Close access to policy files and deploy policies
2042                                 routerConfig.close();
2043                                 // Deploy the policy
2044                                 HashSet<String> setAddresses = new HashSet<String>(commHan.getHosts());
2045                                 setAddresses.add(strIoTMasterHostAdd);
2046                                 createPolicyThreads(STR_ROUTER_ADD, setAddresses);
2047
2048                                 // PROFILING
2049                                 result = System.currentTimeMillis()-start;
2050                                 System.out.println("\n\n ==> Time needed to send policy files and deploy them : " + result + "\n\n");
2051
2052                                 // PROFILING
2053                                 start = System.currentTimeMillis();
2054
2055                                 // Separating object creations and Set/Relation initializations
2056                                 createControllerObjects();
2057
2058                                 // PROFILING
2059                                 result = System.currentTimeMillis()-start;
2060                                 System.out.println("\n\n ==> Time needed to instantiate objects: " + result + "\n\n");
2061                                 // PROFILING
2062                                 start = System.currentTimeMillis();
2063
2064                                 // Sets and relations initializations
2065                                 if(STR_LANGUAGE.equals(STR_JAVA))
2066                                         initializeSetsAndRelationsJava(inStream, outStream);
2067                                 else
2068                                         initializeSetsAndRelationsCpp(inStream, outStream);;
2069
2070                                 // PROFILING
2071                                 result = System.currentTimeMillis()-start;
2072                                 System.out.println("\n\n ==> Time needed to initialize sets and relations: " + result + "\n\n");
2073
2074                                 if(STR_LANGUAGE.equals(STR_JAVA))
2075                                         // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO EXECUTE INIT METHOD
2076                                         commMasterToSlave(new MessageSimple(IoTCommCode.INVOKE_INIT_METHOD), "Invoke init() method!", inStream, outStream);
2077                                 else
2078                                         invokeInitMethodCpp(outStream, inStream);
2079                                 // == COMMUNICATION WITH IOTSLAVE CONTROLLER TO END PROCESS
2080                                 if(STR_LANGUAGE.equals(STR_JAVA)) {
2081                                         ObjectOutputStream oStream = (ObjectOutputStream) outStream;
2082                                         oStream.writeObject(new MessageSimple(IoTCommCode.END_SESSION));
2083                                 } else  // C++ side will wait until the program finishes, it's not generating a separate thread for now
2084                                         //endSessionCpp(outStream);
2085                                 outStream.close();
2086                                 inStream.close();
2087                                 socket.close();
2088                                 serverSocket.close();
2089                                 commHan.printLists();
2090                                 lbIoT.printHostInfo();
2091                         }
2092
2093                 } catch (IOException          |
2094                                  InterruptedException |
2095                                  ClassNotFoundException ex) {
2096                         System.out.println("IoTMaster: Exception: "
2097                                 + ex.getMessage());
2098                         ex.printStackTrace();
2099                 }
2100         }
2101
2102         public static void main(String args[]) {
2103
2104                 // Detect the available controller/device classes
2105                 // Input args[] should be used to list the controllers/devices
2106                 // e.g. java IoTMaster AcmeProximity AcmeThermostat AcmeVentController
2107                 IoTMaster iotMaster = new IoTMaster(args);
2108                 // Read config file
2109                 iotMaster.parseIoTMasterConfigFile();
2110                 // Initialize CommunicationHandler, LoadBalancer, and RouterConfig
2111                 iotMaster.initLiveDataStructure();
2112                 // Create objects
2113                 iotMaster.createObjects();
2114         }
2115 }