observe state of full presence table 59/27059/1
authorRap <peter.rafaj@kistler.com>
Fri, 27 Apr 2018 12:06:33 +0000 (14:06 +0200)
committerPeter Rafaj <peter.rafaj@kistler.com>
Tue, 25 Sep 2018 11:23:12 +0000 (13:23 +0200)
Change-Id: I17386998c025115f015a1b92e4e27f19bdf65f8c
Signed-off-by: Peter Rafaj <peter.rafaj@kistler.com>
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/RDManager.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/presence/PresenceManager.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/presence/device/DevicePresenceResource.java

index ab74400..4209878 100644 (file)
@@ -95,8 +95,13 @@ public class DevicePresence extends Resource {
                                     + (mRequest.getUriQuery() != null
                                             ? (";" + mRequest.getUriQuery())
                                             : "");
-                            mRequest = MessageBuilder.modifyRequest(mRequest,
-                                    null, uriQuery, null, null);
+                            if(mRequest.getUriQueryMap() != null && mRequest.getUriQueryMap().containsKey(Constants.DEVICE_ID)){
+                                mRequest = MessageBuilder.modifyRequest(mRequest,
+                                        null, uriQuery, null, null);
+                            }else {
+                                mRequest = MessageBuilder.modifyRequest(mRequest,
+                                        null, null, null, null);
+                            }
                         }
                     }
 
index 5ac2143..1d8b0be 100644 (file)
@@ -44,9 +44,16 @@ public class RDManager {
 
     private ArrayList<HashMap<String, Object>> mResourcePresence = new ArrayList<>();
 
+    private static final RDManager INSTANCE = new RDManager();
+
+    public static RDManager getInstance(){
+        return INSTANCE;
+    }
+
+
     /**
      * API for handling resource-publish process
-     * 
+     *
      * @param requestPayload
      *            request payload
      * @return response payload
index 0fa0c57..9e4cae9 100644 (file)
 package org.iotivity.cloud.rdserver.resources.presence;
 
 import java.io.ByteArrayOutputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 
+import org.bson.Document;
+import org.iotivity.cloud.rdserver.resources.directory.RDManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.iotivity.cloud.base.device.Device;
@@ -65,7 +64,7 @@ public class PresenceManager {
         public IRequest mRequest;
     }
 
-    private static class PresenceInfo {
+    private class PresenceInfo {
 
         PresenceInfo() {
             mSubscriber = new HashMap<>();
@@ -74,18 +73,117 @@ public class PresenceManager {
         }
 
         // di , token, Subscriber list
-        private HashMap<String, HashMap<String, PresenceSubscriber>> mSubscriber;
+        protected HashMap<String, HashMap<String, PresenceSubscriber>> mSubscriber;
         // token, di list
-        private HashMap<String, List<String>>                        mSubscribedDevices;
-        private HashMap<String, Long>                                mSequenceNumber;
+        protected HashMap<String, List<String>>                        mSubscribedDevices;
+        protected HashMap<String, Long>                                mSequenceNumber;
+
+
+        public void subscribe(Device srcDevice, IRequest request,
+                              List<String> deviceIdList){
+            for (String deviceId : deviceIdList) {
+                HashMap<String, PresenceSubscriber> subscribers = mSubscriber
+                        .get(deviceId);
+
+                if (subscribers == null) {
+                    subscribers = new HashMap<>();
+                    mSubscriber.put(deviceId, subscribers);
+                }
+
+                subscribers.put(request.getRequestId(),
+                        new PresenceSubscriber(srcDevice, request));
+            }
+
+            mSubscribedDevices.put(request.getRequestId(),
+                    deviceIdList);
+            mSequenceNumber.put(request.getRequestId(), (long) 1);
+        }
+
+        public void unSubscribe(IRequest request, List<String> deviceIdList) {
+
+            for (String deviceId : deviceIdList) {
+                HashMap<String, PresenceSubscriber> subscribers = mSubscriber
+                        .get(deviceId);
+
+                if (subscribers == null) {
+                    continue;
+                }
+
+                subscribers.remove(request.getRequestId());
+            }
+        }
+
+        public void notify(String deviceId) {
+
+            HashMap<String, PresenceSubscriber> tokenNSubscribers = mSubscriber
+                    .get(deviceId);
+
+            if (tokenNSubscribers != null) {
+                byte[] payload = makeResponsePayload(Arrays.asList(deviceId));
+
+                for (PresenceSubscriber subscriber : tokenNSubscribers.values()) {
+
+                    subscriber.mSubscriber.sendResponse(
+                            MessageBuilder.createResponse(subscriber.mRequest,
+                                    ResponseStatus.CONTENT,
+                                    ContentFormat.APPLICATION_CBOR, payload));
+                }
+            }
+        }
+
+    }
+
+    private class CompletePresenceInfo extends PresenceInfo {
+
+        // di , token, Subscriber list
+        private Map<String, PresenceSubscriber> mAllSubscribers;
+
+        public CompletePresenceInfo() {
+            super();
+            this.mAllSubscribers = new HashMap<>();
+        }
+
+        @Override
+        public void subscribe(Device srcDevice, IRequest request, List<String> deviceIdList) {
+            if(deviceIdList != null && deviceIdList.size() > 0){
+                super.subscribe(srcDevice,request,deviceIdList);
+            }else {
+                mAllSubscribers.put(request.getRequestId(),
+                        new PresenceSubscriber(srcDevice, request));
+                mSequenceNumber.put(request.getRequestId(), (long) 1);
+            }
+        }
+
+        @Override
+        public void unSubscribe(IRequest request, List<String> deviceIdList) {
+            if (deviceIdList != null && deviceIdList.size() > 0) {
+                super.unSubscribe(request, deviceIdList);
+            } else {
+                mAllSubscribers.remove(request.getRequestId());
+            }
+        }
+
+        @Override
+        public void notify(String deviceId) {
+
+            byte[] payload = makeResponsePayload(Arrays.asList(deviceId));
+            for (PresenceSubscriber subscriber : this.mAllSubscribers.values()) {
+                subscriber.mSubscriber.sendResponse(
+                        MessageBuilder.createResponse(subscriber.mRequest,
+                                ResponseStatus.CONTENT,
+                                ContentFormat.APPLICATION_CBOR, payload));
+            }
+        }
     }
 
     private PresenceInfo mDevicePresence   = null;
+    private PresenceInfo mAllDevicePresence = null;
     private PresenceInfo mResourcePresence = null;
 
     private PresenceManager() {
         mDevicePresence = new PresenceInfo();
         mResourcePresence = new PresenceInfo();
+        mAllDevicePresence = new CompletePresenceInfo();
     }
 
     /**
@@ -112,24 +210,8 @@ public class PresenceManager {
     public void subscribePresence(Device srcDevice, IRequest request,
             List<String> deviceIdList, String presenceType) {
 
-        PresenceInfo presenceInfo = getPresenceInfo(presenceType);
-
-        for (String deviceId : deviceIdList) {
-            HashMap<String, PresenceSubscriber> subscribers = presenceInfo.mSubscriber
-                    .get(deviceId);
-
-            if (subscribers == null) {
-                subscribers = new HashMap<>();
-                presenceInfo.mSubscriber.put(deviceId, subscribers);
-            }
-
-            subscribers.put(request.getRequestId(),
-                    new PresenceSubscriber(srcDevice, request));
-        }
-
-        presenceInfo.mSubscribedDevices.put(request.getRequestId(),
-                deviceIdList);
-        presenceInfo.mSequenceNumber.put(request.getRequestId(), (long) 1);
+        PresenceInfo presenceInfo = getPresenceInfo(presenceType,deviceIdList);
+        presenceInfo.subscribe(srcDevice,request,deviceIdList);
     }
 
     /**
@@ -145,18 +227,8 @@ public class PresenceManager {
     public void unsubscribePresence(IRequest request, List<String> deviceIdList,
             String presenceType) {
 
-        PresenceInfo presenceInfo = getPresenceInfo(presenceType);
-
-        for (String deviceId : deviceIdList) {
-            HashMap<String, PresenceSubscriber> subscribers = presenceInfo.mSubscriber
-                    .get(deviceId);
-
-            if (subscribers == null) {
-                continue;
-            }
-
-            subscribers.remove(request.getRequestId());
-        }
+        PresenceInfo presenceInfo = getPresenceInfo(presenceType,deviceIdList);
+        presenceInfo.unSubscribe(request,deviceIdList);
     }
 
     /**
@@ -166,21 +238,8 @@ public class PresenceManager {
      *            device id
      */
     public void notifyToObservers(String deviceId) {
-
-        HashMap<String, PresenceSubscriber> tokenNSubscribers = mDevicePresence.mSubscriber
-                .get(deviceId);
-
-        if (tokenNSubscribers != null) {
-            byte[] payload = makeResponsePayload(Arrays.asList(deviceId));
-
-            for (PresenceSubscriber subscriber : tokenNSubscribers.values()) {
-
-                subscriber.mSubscriber.sendResponse(
-                        MessageBuilder.createResponse(subscriber.mRequest,
-                                ResponseStatus.CONTENT,
-                                ContentFormat.APPLICATION_CBOR, payload));
-            }
-        }
+        this.mDevicePresence.notify(deviceId);
+        this.mAllDevicePresence.notify(deviceId);
     }
 
     /**
@@ -235,12 +294,16 @@ public class PresenceManager {
         return state;
     }
 
-    private PresenceInfo getPresenceInfo(String presenceType) {
+    private PresenceInfo getPresenceInfo(String presenceType,final List<String> deviceIds) {
 
         PresenceInfo presenceInfo = null;
         switch (presenceType) {
             case Constants.DEVICE_PRESENCE:
-                presenceInfo = mDevicePresence;
+                if(deviceIds == null || deviceIds.size() == 0){
+                    presenceInfo = mAllDevicePresence;
+                }else {
+                    presenceInfo = mDevicePresence;
+                }
                 break;
             case Constants.RESOURCE_PRESENCE:
                 presenceInfo = mResourcePresence;
index fe4556f..ff8312b 100644 (file)
@@ -21,9 +21,7 @@
  */
 package org.iotivity.cloud.rdserver.resources.presence.device;
 
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 
 import org.iotivity.cloud.base.device.Device;
 import org.iotivity.cloud.base.exception.ServerException;
@@ -80,9 +78,11 @@ public class DevicePresenceResource extends Resource {
             throws ServerException {
         HashMap<String, List<String>> queryMap = request.getUriQueryMap();
 
-        checkQueryException(Arrays.asList(Constants.DEVICE_ID), queryMap);
-
-        List<String> deviceList = queryMap.get(Constants.DEVICE_ID);
+//        checkQueryException(Arrays.asList(Constants.DEVICE_ID), queryMap);
+        List<String> deviceList = new LinkedList<>();
+        if(queryMap != null){
+            deviceList.addAll(queryMap.getOrDefault(Constants.DEVICE_ID,new ArrayList<>()));
+        }
 
         switch (request.getObserve()) {
             case SUBSCRIBE: