Working Java v.1.0 for arbitrary calls of callback objects
authorrtrimana <rtrimana@uci.edu>
Thu, 26 Jan 2017 18:58:52 +0000 (10:58 -0800)
committerrtrimana <rtrimana@uci.edu>
Thu, 26 Jan 2017 18:58:52 +0000 (10:58 -0800)
iotjava/iotrmi/Java/IoTRMICall.java
iotjava/iotrmi/Java/IoTRMIObject.java
iotjava/iotrmi/Java/IoTRMIUtil.java
iotjava/iotrmi/Java/IoTSocket.java
iotjava/iotrmi/Java/IoTSocketClient.java
iotjava/iotrmi/Java/basics/CallBack.java
iotjava/iotrmi/Java/basics/TestClass.java
iotjava/iotrmi/Java/basics/TestClassCallbacks_Stub.java
iotjava/iotrmi/Java/basics/TestClass_Skeleton.java

index 1c8722183ec6b6327f3d4e02a61a829acc03cdba..9250bad81d9b63356f8c9e1c9cf4c8648e10760d 100644 (file)
@@ -12,6 +12,9 @@ import java.lang.reflect.Method;
 import java.util.HashSet;
 import java.util.Set;
 
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicBoolean;
+
 
 /** Class IoTRMICall is a class that serves method calls on stub.
  *  <p>
@@ -31,20 +34,214 @@ public class IoTRMICall {
         */
        private IoTRMIUtil rmiUtil;
        private IoTSocketClient rmiClient;
-
-
+       private byte[] retValueBytes;
+       //private AtomicBoolean didGetReturnValue;
+       //private Map<String,byte[]> mapReturnValue;            // Store the return value received in a map
+       private ConcurrentLinkedQueue<byte[]> returnQueue;
+       private Map<String,AtomicBoolean> mapStubId;
+       private AtomicBoolean didGetReturnBytes;
+       private int objectIdCounter = Integer.MAX_VALUE;
+       
        /**
         * Constructors
         */
        public IoTRMICall(int _port, String _address, int _rev) throws IOException {
 
+               //didGetReturnValue = new AtomicBoolean(false);
                rmiUtil = new IoTRMIUtil();
                rmiClient = new IoTSocketClient(_port, _address, _rev);
+               retValueBytes = null;
+               returnQueue = new ConcurrentLinkedQueue<byte[]>();
+               mapStubId = new HashMap<String,AtomicBoolean>();
+               didGetReturnBytes = new AtomicBoolean(false);
+               //mapReturnValue = new HashMap<String,byte[]>();
+               waitForReturnValue();
+               wakeUpThread();
+       }
+       
+       
+       public IoTRMICall(int _localPort, int _port, String _address, int _rev) throws IOException {
+
+               //didGetReturnValue = new AtomicBoolean(false);
+               rmiUtil = new IoTRMIUtil();
+               rmiClient = new IoTSocketClient(_localPort, _port, _address, _rev);
+               retValueBytes = null;
+               returnQueue = new ConcurrentLinkedQueue<byte[]>();
+               mapStubId = new HashMap<String,AtomicBoolean>();
+               didGetReturnBytes = new AtomicBoolean(false);
+               //mapReturnValue = new HashMap<String,byte[]>();
+               waitForReturnValue();
+               wakeUpThread();
        }
 
 
        /**
-        * remoteCall() calls a method remotely by passing in parameters and getting a return Object
+        * waitForReturnValue() starts a thread that waits for return value for a method invocation
+        */
+       public void waitForReturnValue() {
+
+               Thread thread = new Thread() {
+                       public void run() {
+                               byte[] retBytes = null;
+                               while(true) {
+                                       try {
+                                               retBytes = rmiClient.receiveBytes(retBytes);
+                                               if (retBytes != null) {
+                                                       System.out.println("Return value not null: " + Arrays.toString(retBytes));
+                                                       //byte[] keyBytes = getObjectAndMethodIdBytes();
+                                                       //String strKeyBytes = new String(keyBytes);
+                                                       returnQueue.offer(retBytes);
+                                               } else
+                                                       Thread.sleep(100);
+                                               retBytes = null;
+                                       } catch (Exception ex) {
+                                               ex.printStackTrace();
+                                               throw new Error("IoTRMICall: Error receiving return value bytes!");
+                                       }
+                               }
+                       }
+               };
+               thread.start();
+       }
+       
+       
+       /**
+        * wakeUpThread() wakes up the correct thread
+        */
+       public void wakeUpThread() {
+
+               Thread thread = new Thread() {
+                       public void run() {
+                               while(true) {
+                                       // Take the current method from the queue and wake up the correct thread
+                                       retValueBytes = returnQueue.poll();
+                                       if (retValueBytes != null) {    // If there is method bytes
+                                               System.out.println("methodBytes in wake up thread: " + Arrays.toString(retValueBytes));
+                                               int objectId = getObjectId();
+                                               int methodId = getMethodId();
+                                               String strKey = objectId + "-" + methodId;
+                                               AtomicBoolean retRecv = mapStubId.get(strKey);
+                                               System.out.println("boolean status: " + retRecv + " with key: " + strKey);
+                                               didGetReturnBytes.set(false);
+                                               while(!retRecv.compareAndSet(false, true));
+                                               System.out.println("boolean status: " + retRecv + " - map has: " + mapStubId.size());
+                                               while(!didGetReturnBytes.get());        // While skeleton is still processing
+                                       }
+                               }
+                       }
+               };
+               thread.start();
+       }
+       
+
+       /**
+        * registerStub() registers the skeleton to be woken up
+        */
+       public synchronized void registerStub(int objectId, int methodId, AtomicBoolean retValueReceived) {
+
+               String strKey = objectId + "-" + methodId;
+               System.out.println("Key exist? " + mapStubId.containsKey(strKey));
+               mapStubId.put(strKey, retValueReceived);
+               System.out.println("\n\nAdding keyBytes: " + strKey + " now size: " + mapStubId.size() + "\n\n");
+       }
+
+
+       /**
+        * getObjectIdCounter() gets object Id counter
+        */
+       public int getObjectIdCounter() {
+
+               return objectIdCounter;
+       }
+
+
+       /**
+        * setObjectIdCounter() sets object Id counter
+        */
+       public void setObjectIdCounter(int objIdCounter) {
+
+               objectIdCounter = objIdCounter;
+       }
+
+
+       /**
+        * decrementObjectIdCounter() gets object Id counter
+        */
+       public void decrementObjectIdCounter() {
+
+               objectIdCounter--;
+       }
+
+
+
+       /**
+        * setGetReturnBytes() set boolean if there is a new return value already
+        */
+       public synchronized boolean setGetReturnBytes() {
+
+               return didGetReturnBytes.compareAndSet(false, true);
+       }
+
+
+       /**
+        * getObjectAndMethodIdBytes() extracts object Id and method Id from method bytes
+        */
+       public byte[] getObjectAndMethodIdBytes() {
+
+               int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
+               byte[] objectMethodIdBytes = new byte[objMethIdLen];
+               System.arraycopy(retValueBytes, 0, objectMethodIdBytes, 0, objMethIdLen);
+               return objectMethodIdBytes;
+       }
+
+
+       /**
+        * getObjectAndMethodIdBytes() gets object and method Id in bytes
+        */
+       public byte[] getObjectAndMethodIdBytes(int objectId, int methodId) {
+
+               int objMethIdLen = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
+               byte[] objectMethodIdBytes = new byte[objMethIdLen];
+               byte[] objIdBytes = IoTRMIUtil.intToByteArray(objectId);
+               byte[] methIdBytes = IoTRMIUtil.intToByteArray(methodId);
+               System.arraycopy(objIdBytes, 0, objectMethodIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
+               System.arraycopy(methIdBytes, 0, objectMethodIdBytes, IoTRMIUtil.OBJECT_ID_LEN, IoTRMIUtil.METHOD_ID_LEN);
+               return objectMethodIdBytes;
+       }
+
+
+       /**
+        * getObjectId() gets object Id from bytes
+        */
+       public int getObjectId() {
+
+               // Get object Id bytes
+               byte[] objectIdBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN];
+               System.arraycopy(retValueBytes, 0, objectIdBytes, 0, IoTRMIUtil.OBJECT_ID_LEN);
+               // Get object Id
+               int objectId = IoTRMIUtil.byteArrayToInt(objectIdBytes);
+               return objectId;
+       }
+
+
+       /**
+        * getMethodId() gets method Id from bytes
+        */
+       public int getMethodId() {
+
+               // Get method Id bytes
+               byte[] methodIdBytes = new byte[IoTRMIUtil.METHOD_ID_LEN];
+               // Method Id is positioned after object Id in the byte array
+               System.arraycopy(retValueBytes, IoTRMIUtil.OBJECT_ID_LEN, methodIdBytes, 0, IoTRMIUtil.METHOD_ID_LEN);
+               // Get method Id
+               int methodId = IoTRMIUtil.byteArrayToInt(methodIdBytes);
+               // Get method Id
+               return methodId;
+       }
+
+
+       /**
+        * remoteCall() calls a method remotely by passing in parameters and getting a return Object (DEPRECATED)
         */
        public synchronized Object remoteCall(int objectId, int methodId, Class<?> retType, 
                        Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
@@ -73,6 +270,53 @@ public class IoTRMICall {
        }
 
 
+       public synchronized void remoteCall(int objectId, int methodId, Class<?>[] paramCls, Object[] paramObj) {
+
+               // Send method info
+               byte[] methodBytes = methodToBytes(objectId, methodId, paramCls, paramObj);
+               try {
+                       rmiClient.sendBytes(methodBytes);
+               } catch (IOException ex) {
+                       ex.printStackTrace();
+                       throw new Error("IoTRMICall: Error when sending bytes - rmiClient.sendBytes()");
+               }
+       }
+
+
+       /**
+        * getReturnValue() returns return value
+        */
+       public synchronized Object getReturnValue(Class<?> retType, Class<?> retGenTypeVal) {
+
+               // Receive return value and return it to caller
+               // Now just strip off the object ID and method ID
+               int valByteLen = retValueBytes.length - (IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN);
+               byte[] retValBytes = new byte[valByteLen];
+               // Method Id is positioned after object Id in the byte array
+               System.arraycopy(retValueBytes, IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN, retValBytes, 0, valByteLen);
+               Object retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retValBytes);
+               // This means the right object and method have gotten the return value, so we set this back to false
+               return retObj;
+       }
+
+
+       /**
+        * getReturnValue() returns return value
+        */
+       public synchronized Object getReturnValue(Class<?> retType, Class<?> retGenTypeVal, byte[] retValueBytes) {
+
+               // Receive return value and return it to caller
+               // Now just strip off the object ID and method ID
+               int valByteLen = retValueBytes.length - (IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN);
+               byte[] retValBytes = new byte[valByteLen];
+               // Method Id is positioned after object Id in the byte array
+               System.arraycopy(retValueBytes, IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN, retValBytes, 0, valByteLen);
+               Object retObj = IoTRMIUtil.getParamObject(retType, retGenTypeVal, retValBytes);
+               // This means the right object and method have gotten the return value, so we set this back to false
+               return retObj;
+       }
+
+
        /**
         * methodToBytes() returns byte representation of a method
         */
index e88e639499ffbd9c2290d400cc04f74c3360e07e..dfbe9c6b348484a6dcc5bc16430571bf05c60e04 100644 (file)
@@ -10,8 +10,8 @@ import java.util.Map;
 import java.util.Set;
 import java.lang.reflect.*;
 
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 
 /** Class IoTRMIObject is a class that stores info of an object.
@@ -35,7 +35,9 @@ public class IoTRMIObject {
        private IoTRMIUtil rmiUtil;
        private IoTSocketServer rmiServer;
        private byte[] methodBytes;
-       private Lock lock = new ReentrantLock();
+       private ConcurrentLinkedQueue<byte[]> methodQueue;
+       private Map<Integer,AtomicBoolean> mapSkeletonId;
+       private AtomicBoolean didGetMethodBytes;
 
 
        /**
@@ -45,22 +47,94 @@ public class IoTRMIObject {
                ClassNotFoundException, InstantiationException, 
                        IllegalAccessException, IOException {
 
+               didGetMethodBytes = new AtomicBoolean(false);
                rmiUtil = new IoTRMIUtil();
                methodBytes = null;
+               methodQueue = new ConcurrentLinkedQueue<byte[]>();
+               mapSkeletonId = new HashMap<Integer,AtomicBoolean>();
                rmiServer = new IoTSocketServer(_port);
                rmiServer.connect();
+               waitForMethod();
+               wakeUpThread();
        }
 
 
        /**
-        * getMethodBytes() waits for method transmission in bytes
+        * waitForMethod() starts a thread that waits for method bytes
+        */
+       public void waitForMethod() {
+
+               Thread thread = new Thread() {
+                       public void run() {
+                               byte[] methBytes = null;
+                               while(true) {
+                                       try {
+                                               methBytes = rmiServer.receiveBytes(methBytes);
+                                               if (methBytes != null) {
+                                                       System.out.println("Command not null: " + Arrays.toString(methBytes));
+                                                       methodQueue.offer(methBytes);
+                                               } else
+                                                       Thread.sleep(100);
+                                               methBytes = null;
+                                       } catch (Exception ex) {
+                                               ex.printStackTrace();
+                                               throw new Error("IoTRMICall: Error receiving return value bytes!");
+                                       }
+                               }
+                       }
+               };
+               thread.start();
+       }
+
+
+       /**
+        * wakeUpThread() wakes up the correct thread
+        */
+       public void wakeUpThread() {
+
+               Thread thread = new Thread() {
+                       public void run() {
+                               while(true) {
+                                       // Take the current method from the queue and wake up the correct thread
+                                       methodBytes = methodQueue.poll();
+                                       if (methodBytes != null) {      // If there is method bytes
+                                               int currObjId = getObjectId(methodBytes);
+                                               AtomicBoolean methRecv = mapSkeletonId.get(currObjId);
+                                               didGetMethodBytes.set(false);
+                                               while(!methRecv.compareAndSet(false, true));
+                                               while(!didGetMethodBytes.get());        // While skeleton is still processing
+                                       }
+                               }
+                       }
+               };
+               thread.start();
+       }
+
+
+       /**
+        * registerSkeleton() registers the skeleton to be woken up
+        */
+       public synchronized void registerSkeleton(int objectId, AtomicBoolean methodReceived) {
+
+               mapSkeletonId.put(objectId, methodReceived);
+       }
+
+
+       /**
+        * setGetMethodBytes() set didGetMethodBytes to true after getting the bytes
+        */
+       public boolean setGetMethodBytes() {
+
+               return didGetMethodBytes.compareAndSet(false, true);
+       }
+
+
+       /**
+        * getMethodBytes() get the method in bytes
         */
        public byte[] getMethodBytes() throws IOException {
 
-               // Receive method info
-               //System.out.println("Method RMIObj before: " + Arrays.toString(methodBytes));
-               methodBytes = rmiServer.receiveBytes(methodBytes);
-               //System.out.println("Method RMIObj after: " + Arrays.toString(methodBytes));
+               // Just return the methodBytes content
                return methodBytes;
        }
 
@@ -93,16 +167,6 @@ public class IoTRMIObject {
        }
 
 
-       /**
-        * setMethodBytes() sets bytes for method
-        */
-       /*public void setMethodBytes(byte[] _methodBytes) throws IOException {
-
-               // Set method bytes
-               methodBytes = _methodBytes;
-       }*/
-
-
        /**
         * getMethodId() gets method Id from bytes
         */
@@ -181,13 +245,66 @@ public class IoTRMIObject {
 
 
        /**
-        * sendReturnObj() sends back return Object to client
+        * getMethodParams() overloading
+        */
+       public Object[] getMethodParams(Class<?>[] arrCls, Class<?>[] arrGenValCls, byte[] methodBytes) {
+
+               // Byte scanning position
+               int pos = IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN;
+               Object[] paramObj = new Object[arrCls.length];
+               for (int i=0; i < arrCls.length; i++) {
+
+                       String paramType = arrCls[i].getSimpleName();
+                       int paramSize = rmiUtil.getTypeSize(paramType);
+                       // Get the 32-bit field in the byte array to get the actual
+                       //              length (this is a param with indefinite length)
+                       if (paramSize == -1) {
+                               byte[] bytPrmLen = new byte[IoTRMIUtil.PARAM_LEN];
+                               System.arraycopy(methodBytes, pos, bytPrmLen, 0, IoTRMIUtil.PARAM_LEN);
+                               pos = pos + IoTRMIUtil.PARAM_LEN;
+                               paramSize = IoTRMIUtil.byteArrayToInt(bytPrmLen);
+                       }
+                       byte[] paramBytes = new byte[paramSize];
+                       System.arraycopy(methodBytes, pos, paramBytes, 0, paramSize);
+                       pos = pos + paramSize;
+                       paramObj[i] = IoTRMIUtil.getParamObject(arrCls[i], arrGenValCls[i], paramBytes);
+               }
+
+               return paramObj;
+       }
+
+
+       /**
+        * sendReturnObj() overloading
         */
        public void sendReturnObj(Object retObj) throws IOException {
 
+
+               byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
+               // Send return value together with OBJECT_ID and METHOD_ID for arbitration
+               byte[] retAllBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN + retObjBytes.length];
+               // Copy OBJECT_ID and METHOD_ID
+               System.arraycopy(methodBytes, 0, retAllBytes, 0, IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN);
+               // Copy array of bytes (return object)
+               System.arraycopy(retObjBytes, 0, retAllBytes, IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN, retObjBytes.length);
+               rmiServer.sendBytes(retAllBytes);
+       }
+
+
+       /**
+        * sendReturnObj() static version
+        */
+       public void sendReturnObj(Object retObj, byte[] methodBytes) throws IOException {
+
                // Send back return value
                byte[] retObjBytes = IoTRMIUtil.getObjectBytes(retObj);
-               rmiServer.sendBytes(retObjBytes);
+               // Send return value together with OBJECT_ID and METHOD_ID for arbitration
+               byte[] retAllBytes = new byte[IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN + retObjBytes.length];
+               // Copy OBJECT_ID and METHOD_ID
+               System.arraycopy(methodBytes, 0, retAllBytes, 0, IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN);
+               // Copy array of bytes (return object)
+               System.arraycopy(retObjBytes, 0, retAllBytes, IoTRMIUtil.OBJECT_ID_LEN + IoTRMIUtil.METHOD_ID_LEN, retObjBytes.length);
+               rmiServer.sendBytes(retAllBytes);
        }
 
 
index f593a7ad4d277335c0e4c5c4ec093aa200750c4c..c4d383b829ee253cc82fc62d3fa826acbd4baa6c 100644 (file)
@@ -51,6 +51,18 @@ public class IoTRMIUtil {
        public final static int BYT_LEN = 1;
        public final static int BOL_LEN = 1;
 
+       /**
+        * Public static data structure to keep track of multiple skeletons and stubs
+        */
+       //public static IoTRMIObject rmiObj;
+       //public static IoTRMICall rmiCall;
+       //public static int objIdCounter = Integer.MAX_VALUE;
+       public static Integer objIdCounter = new Integer(Integer.MAX_VALUE);
+       public static Map<Integer,Object> mapStub = new HashMap<Integer,Object>();              // Map object to its stub ID
+       public static Map<Object,Object> mapSkel = new HashMap<Object,Object>();                // Map object to its skeleton
+       public static Map<Object,Integer> mapSkelId = new HashMap<Object,Integer>();    // Map object to its skeleton ID
+
+
        /**
         * Constructors
         */
@@ -67,6 +79,20 @@ public class IoTRMIUtil {
                                IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
        }
 
+       
+       /*public static void initRMICall(int port, String address, int rev) throws IOException {
+               rmiCall = new IoTRMICall(port, address, rev);
+       }
+       
+       public static void initRMICall(int localPort, int port, String address, int rev) throws IOException {
+               rmiCall = new IoTRMICall(localPort, port, address, rev);
+       }
+       
+       public static void initRMIObject(int port) throws IOException, ClassNotFoundException, 
+                       InstantiationException, IllegalAccessException {
+               rmiObj = new IoTRMIObject(port);
+       }*/
+       
 
        /**
         * getHashCodeBytes() gets hash value (in bytes) from method name
index 5e1d35e70a999705085a1d69ef198acd0e400547..08e7325af64f2ae818337169cbc3c92edd2d92ca 100644 (file)
@@ -26,6 +26,7 @@ public abstract class IoTSocket {
         * Class Properties
         */
        protected byte data[];
+       protected int localPort;
        protected int port;
        protected Socket sock;
        protected BufferedInputStream input;
@@ -43,6 +44,15 @@ public abstract class IoTSocket {
         */
        protected IoTSocket(int _port) throws IOException
        {
+               localPort = 0;
+               port = _port;
+               data = new byte[BUFFSIZE];
+       }
+       
+        
+       protected IoTSocket(int _localPort, int _port) throws IOException
+       {
+               localPort = _localPort;
                port = _port;
                data = new byte[BUFFSIZE];
        }
@@ -62,7 +72,9 @@ public abstract class IoTSocket {
                // Write the byte array
                output.write(vals, 0, len);
                output.flush();
+               //System.out.println("Sender about to receive ACK!");
                receiveAck();
+               //System.out.println("Sender about to send ACK!\n\n");
                sendAck();
        }
 
@@ -77,7 +89,10 @@ public abstract class IoTSocket {
                int numbytes;
 
                // Wait until input is available
-               while(input.available() == 0);
+//             while(input.available() == 0);
+               if (input.available() == 0)
+                       return null;
+
                // Read the maxlen first - read 4 bytes here
                byte[] lenBytes = new byte[MSG_LEN_SIZE];
                input.read(lenBytes, 0, MSG_LEN_SIZE);
@@ -104,7 +119,9 @@ public abstract class IoTSocket {
                }
                // we now send an acknowledgement to the server to let them
                // know we've got it
+               //System.out.println("Receiver about to send ACK!");
                sendAck();
+               //System.out.println("Receiver about to receive ACK!\n\n");
                receiveAck();
 
                return val;
@@ -114,7 +131,7 @@ public abstract class IoTSocket {
        /**
         * Close socket connection
         */
-       public synchronized void close() throws IOException
+       public void close() throws IOException
        {
                sock.close();
        }
@@ -140,4 +157,13 @@ public abstract class IoTSocket {
                int ack;
                ack = (int) input.read();
        }
+
+
+       /**
+        * Set SO TIMEOUT
+        */
+       public void setSoTimeout(int timeout) throws SocketException {
+
+               sock.setSoTimeout(timeout);
+       }
 }
index 961719cf2ae113a42999d8c364f7e7ea17ad1cf4..4338b0ebc00fc264a95fa4d585fa03fb3921a317 100644 (file)
@@ -24,7 +24,7 @@ public class IoTSocketClient extends IoTSocket {
         * Default constructor
         */
        public IoTSocketClient(int _port, String _address, int rev) throws IOException
-       {
+               {
                super(_port);
                try {
                        sock = new Socket( InetAddress.getByName(_address), port );
@@ -38,4 +38,24 @@ public class IoTSocketClient extends IoTSocket {
                output.write(rev);
                output.flush();
        }
+
+       /**
+        * Additional constructor
+        */
+       public IoTSocketClient(int _localPort, int _port, String _address, int rev) throws IOException
+       {
+               super(_localPort, _port);
+               try {
+                       sock = new Socket( InetAddress.getByName(_address), 
+                                               port, InetAddress.getByName(_address), localPort );
+                       input = new BufferedInputStream(sock.getInputStream(), BUFFSIZE);
+                       output = new BufferedOutputStream(sock.getOutputStream(),BUFFSIZE);
+               }
+               catch ( IOException e ) {
+                       e.printStackTrace();
+               }
+               // now we want to tell the server if we want reversed bytes or not
+               output.write(rev);
+               output.flush();
+       }
 }
index b64b1ae66f222e3012cb2fe188679d9567bbe1b4..1fb79e2fa601e214113d04293e9c77a8ec6744f0 100644 (file)
@@ -30,6 +30,7 @@ public class CallBack implements CallBackInterface {
        
        public void needCallback(TestClassComplete tc) {
 
+               System.out.println("Going to invoke getShort()!");
                System.out.println("Short from TestClass: " + tc.getShort((short)1234));
        }
 }
index 124800490d16454092e4d5e1fa23fd36a72e78b5..48748d0546c7ba5cac88e3832bef5e1c8195c153 100644 (file)
@@ -33,13 +33,29 @@ public class TestClass implements TestClassInterface {
                cblist = new ArrayList<CallBackInterfaceWithCallBack>();
        }
 
+       
+       public int callBack() {
+               
+               int sum = 0;
+               System.out.println("Callback called! cblist: " + cblist.size());
+               for (CallBackInterfaceWithCallBack cb : cblist) {
+                       sum = sum + cb.printInt();
+                       //TestClass tci = new TestClass();
+                       cb.needCallback(this);
+                       //cb.needCallback(this);
+                       //cb.needCallback(tci);
+                       System.out.println("Inside the loop!");
+               }
+               System.out.println("Executed callback of callback! Returning value: " + sum + "\n\n");
+               return sum;
+       }
 
        // Callback
        //public void registerCallback(CallBackInterface _cb) {
        public void registerCallback(CallBackInterfaceWithCallBack _cb) {
 
                cblist.add(_cb);
-               System.out.println("Registering callback object!");
+               System.out.println("Registering callback object! Items: " + cblist.size());
        }
 
 
@@ -73,18 +89,6 @@ public class TestClass implements TestClassInterface {
        }
 
 
-       public int callBack() {
-               
-               int sum = 0;
-               System.out.println("Callback called!");
-               for (CallBackInterfaceWithCallBack cb : cblist) {
-                       sum = sum + cb.printInt();
-                       cb.needCallback(this);
-               }
-               return sum;
-       }
-
-
        // Single variables
        public byte getByte(byte in) {
 
@@ -520,4 +524,10 @@ public class TestClass implements TestClassInterface {
                intA = newA;
                return intA;
        }
+
+       public static void main(String[] args) {
+
+               TestClass tc = new TestClass();
+               System.out.println("Get short: " + tc.getShort((short) 1234));
+       }
 }
index 390ed8fa39f0064df62e97cd17b60b9317e39a57..81b20dd25dab5eedbe22daedb801bedb7b5730c0 100644 (file)
@@ -8,9 +8,10 @@ public class TestClassCallbacks_Stub {
        public static void main(String[] args) throws Exception {
 
                CommunicationHandler comHan = new CommunicationHandler(true);
-               int numOfPorts = 4;
+               int numOfPorts = 2;
                int[] ports = comHan.getCallbackPorts(numOfPorts);
 
+               int localport = 5011;
                int port = 5010;
                //String address = "localhost";
                //String address = "192.168.2.191";     // RPi2
@@ -21,11 +22,20 @@ public class TestClassCallbacks_Stub {
                //String callbackAddress = "192.168.2.191";     // RPi2
                int rev = 0;
 
-               TestClassComplete_Stub tcstub = new TestClassComplete_Stub(port, skeletonAddress, callbackAddress, rev, ports);
+               TestClassComplete_Stub tcstub = new TestClassComplete_Stub(localport, port, skeletonAddress, callbackAddress, rev, ports);
                System.out.println("==== CALLBACKS ====");
                CallBackInterface cbSingle = new CallBack(2354);
                tcstub.registerCallback(cbSingle);
                System.out.println("Registered callback!");
-               System.out.println("Return value from callback: " + tcstub.callBack());
+               //CallBackInterface cbSingle1 = new CallBack(2356);
+               //tcstub.registerCallback(cbSingle1);
+               System.out.println("Registered callback!");
+
+               System.out.println("Return value from callback 1: " + tcstub.callBack() + "\n\n");
+               //System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)4576) + "\n\n");
+               System.out.println("Return value from callback 2: " + tcstub.callBack() + "\n\n");
+               //System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)1233) + "\n\n");
+               //System.out.println("\n\nCalling short one more time value: " + tcstub.getShort((short)1321) + "\n\n");
+               while(true) {}
        }
 }
index 37a4f9954805dba303dbef14cd009a527b00a68b..76e2db7c89bc829925f23cd44356d97ec1cdd7c1 100644 (file)
@@ -8,6 +8,5 @@ public class TestClass_Skeleton {
                String callbackAddress = InetAddress.getLocalHost().getHostAddress();
                TestClass tc = new TestClass(3, 5f, "7911");
                TestClassInterface_Skeleton tcSkel = new TestClassInterface_Skeleton(tc, callbackAddress, port);
-
        }
-}
\ No newline at end of file
+}