Cleaning up code for runtime, installer, RMI, compiler for the Java side
[iot2.git] / iotjava / iotrmi / Java / IoTRMICommServer.java
index bf8d3b59cad2a37e70af8b6e49b0f8afadf8d59e..189a43f603c6e62d3cc0fce4a62d0d0549b37e59 100644 (file)
@@ -23,7 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
  * @version     1.0
  * @since       2017-01-27
  */
-public class IoTRMICommServer extends IoTRMIComm {
+public final class IoTRMICommServer extends IoTRMIComm {
 
        /**
         * Class Properties
@@ -66,7 +66,7 @@ public class IoTRMICommServer extends IoTRMIComm {
                                        didServerRecvConnect.set(true);
                                } catch (Exception ex) {
                                        ex.printStackTrace();
-                                       throw new Error("IoTRMIComm: Error receiving return value bytes on client!");
+                                       throw new Error("IoTRMICommServer: Error starting receiver server!");
                                }
                        }
                };
@@ -86,7 +86,7 @@ public class IoTRMICommServer extends IoTRMIComm {
                                        didServerSendConnect.set(true);
                                } catch (Exception ex) {
                                        ex.printStackTrace();
-                                       throw new Error("IoTRMIComm: Error receiving return value bytes on client!");
+                                       throw new Error("IoTRMICommServer: Error starting sender server!");
                                }
                        }
                };
@@ -106,22 +106,18 @@ public class IoTRMICommServer extends IoTRMIComm {
                                        try {
                                                packetBytes = rmiServerRecv.receiveBytes(packetBytes);
                                                if (packetBytes != null) {
-                                                       System.out.println("Packet received: " + Arrays.toString(packetBytes));
                                                        int packetType = IoTRMIComm.getPacketType(packetBytes);
                                                        if (packetType == IoTRMIUtil.METHOD_TYPE) {
-                                                               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));
                                                                returnQueue.offer(packetBytes);
                                                        } else
-                                                               throw new Error("IoTRMIComm: Packet type is unknown: " + packetType);
-                                               } //else
-                                               //      Thread.sleep(100);
+                                                               throw new Error("IoTRMICommServer: Packet type is unknown: " + packetType);
+                                               }
                                                packetBytes = null;
                                        } catch (Exception ex) {
                                                ex.printStackTrace();
-                                               throw new Error("IoTRMIComm: Error receiving return value bytes on client!");
+                                               throw new Error("IoTRMICommServer: Error receiving return value bytes on server!");
                                        }
                                }
                        }
@@ -136,18 +132,25 @@ public class IoTRMICommServer extends IoTRMIComm {
        public synchronized void sendReturnObj(Object retObj, byte[] methodBytes) {
 
                // Send back return value
-               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 = 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 {
                        rmiServerSend.sendBytes(retAllBytes);
                } catch (IOException ex) {
@@ -174,6 +177,7 @@ public class IoTRMICommServer extends IoTRMIComm {
                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);
                try {
                        rmiServerSend.sendBytes(retAllBytes);
                } catch (IOException ex) {