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