Making classes final to make inheritance impossible
[iot2.git] / iotjava / iotrmi / Java / IoTRMICommClient.java
index 99d0af92095d71b22a2f5b9832ce1a09c18a65e1..8598844153d7b6b0fb394f18b7a39028269ba7a6 100644 (file)
@@ -22,7 +22,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
  * @version     1.0
  * @since       2017-01-27
  */
-public class IoTRMICommClient extends IoTRMIComm {
+public final class IoTRMICommClient extends IoTRMIComm {
 
        /**
         * Class Properties
@@ -45,6 +45,20 @@ public class IoTRMICommClient extends IoTRMIComm {
        }
 
 
+       /**
+        * Constructor (for stub) - only destination port numbers
+        */
+       public IoTRMICommClient(int _portSend, int _portRecv, String _address, int _rev) throws  
+               ClassNotFoundException, InstantiationException, 
+                       IllegalAccessException, IOException {
+
+               super();
+               rmiClientRecv = new IoTSocketClient(_portSend, _address, _rev);
+               rmiClientSend = new IoTSocketClient(_portRecv, _address, _rev);
+               waitForPacketsOnClient();
+       }
+
+
        /**
         * waitForPacketsOnClient() starts a thread that waits for packet bytes on client side
         */
@@ -59,10 +73,10 @@ public class IoTRMICommClient extends IoTRMIComm {
                                                if (packetBytes != null) {
                                                        int packetType = IoTRMIComm.getPacketType(packetBytes);
                                                        if (packetType == IoTRMIUtil.METHOD_TYPE) {
-                                                               System.out.println("Method packet: " + Arrays.toString(packetBytes));
+                                                               //System.out.println("Method packet: " + Arrays.toString(packetBytes));
                                                                methodQueue.offer(packetBytes);
                                                        } else if (packetType == IoTRMIUtil.RET_VAL_TYPE) {
-                                                               System.out.println("Return value packet: " + Arrays.toString(packetBytes));
+                                                               //System.out.println("Return value packet: " + Arrays.toString(packetBytes));
                                                                returnQueue.offer(packetBytes);
                                                        } else
                                                                throw new Error("IoTRMICommClient: Packet type is unknown: " + packetType);
@@ -86,18 +100,27 @@ public class IoTRMICommClient extends IoTRMIComm {
        public synchronized void sendReturnObj(Object retObj, byte[] methodBytes) {
 
                // Send back return value
-               byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
+               //byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
+               byte[] retObjBytes = null;
+               if (retObj != null)     // Handle nullness
+                       retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
                // Send return value together with OBJECT_ID and METHOD_ID for arbitration
                int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
                int headerLen = objMethIdLen + IoTRMIUtil.PACKET_TYPE_LEN;
-               byte[] retAllBytes = new byte[headerLen + retObjBytes.length];
+               //byte[] retAllBytes = new byte[headerLen + retObjBytes.length];
+               byte[] retAllBytes = null;
+               if (retObj == null)     // Handle nullness
+                       retAllBytes = new byte[headerLen];
+               else
+                       retAllBytes = new byte[headerLen + retObjBytes.length];
                // Copy OBJECT_ID and METHOD_ID
                System.arraycopy(methodBytes, 0, retAllBytes, 0, objMethIdLen);
                int packetType = IoTRMIUtil.RET_VAL_TYPE;       // This is a return value
                byte[] packetTypeBytes = IoTRMIUtil.intToByteArray(packetType);
                System.arraycopy(packetTypeBytes, 0, retAllBytes, objMethIdLen, IoTRMIUtil.PACKET_TYPE_LEN);
                // Copy array of bytes (return object)
-               System.arraycopy(retObjBytes, 0, retAllBytes, headerLen, retObjBytes.length);
+               if (retObj != null)
+                       System.arraycopy(retObjBytes, 0, retAllBytes, headerLen, retObjBytes.length);
                try {
                        rmiClientSend.sendBytes(retAllBytes);
                } catch (IOException ex) {