IOT-3164 29/27129/1
authorRap <peter.rafaj@kistler.com>
Tue, 10 Jul 2018 07:17:21 +0000 (09:17 +0200)
committerPeter Rafaj <peter.rafaj@kistler.com>
Fri, 5 Oct 2018 08:32:33 +0000 (10:32 +0200)
Change-Id: Iaa850c1f8ad7eb824bdd62ed623b0717c797d60f
Signed-off-by: Peter Rafaj <peter.rafaj@kistler.com>
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSession.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/presence/device/DevicePresenceResource.java
cloud/stack/src/main/java/org/iotivity/cloud/base/device/Device.java

index 47995cd..cf88c1f 100644 (file)
@@ -24,6 +24,7 @@ package org.iotivity.cloud.ciserver;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -71,6 +72,8 @@ public class DeviceServerSystem extends ServerSystem {
     private Cbor<HashMap<String, Object>>                 mCbor   = new Cbor<HashMap<String, Object>>();
     private HashMap<ChannelHandlerContext, CoapSignaling> mCsmMap = new HashMap<>();
 
+    public static final String LOGOUT_DEVICE = "LOGOUT_DEVICE";
+
     /**
      *
      * This class provides a set of APIs to manage device pool.
@@ -230,7 +233,10 @@ public class DeviceServerSystem extends ServerSystem {
 
                 switch (urlPath) {
                     case OICConstants.ACCOUNT_SESSION_FULL_URI:
-                        if (response.getStatus() != ResponseStatus.CHANGED) {
+                        final Device device = ctx.channel()
+                                .attr(keyDevice).get();
+                        if(device.existParameter(LOGOUT_DEVICE)){
+                            Log.debug("Device: {} was logout. ", device.getDeviceId());
                             bCloseConnection = true;
                         }
                         break;
@@ -243,9 +249,8 @@ public class DeviceServerSystem extends ServerSystem {
             }
 
             ctx.writeAndFlush(msg);
-
-            if (bCloseConnection == true) {
-                ctx.close();
+            if (bCloseConnection ) {
+                closeTcpConnection(ctx);
             }
         }
 
@@ -287,6 +292,20 @@ public class DeviceServerSystem extends ServerSystem {
             }
         }
 
+        private void closeTcpConnection(final ChannelHandlerContext ctx){
+            CompletableFuture.runAsync(()->{
+                try {
+                    Thread.sleep(500);
+                    final Device device = ctx.channel()
+                            .attr(keyDevice).get();
+                    Log.info("After sign-out, close channel for device: {}",device.getDeviceId());
+                    ctx.close().awaitUninterruptibly();
+                } catch (InterruptedException e) {
+                    Log.error("Unable to sleep: ",e);
+                }
+            });
+        }
+
         /**
          * API for sending state to resource directory
          * 
index f4ad6f7..2d80e8b 100644 (file)
@@ -33,6 +33,7 @@ import org.iotivity.cloud.base.protocols.MessageBuilder;
 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
 import org.iotivity.cloud.base.resource.Resource;
 import org.iotivity.cloud.ciserver.Constants;
+import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.util.Cbor;
 
 /**
@@ -67,6 +68,7 @@ public class AccountSession extends Resource {
             request = MessageBuilder.modifyRequest(request, null, uriQuery,
                     ContentFormat.APPLICATION_CBOR,
                     mCbor.encodingPayloadToCbor(payloadData));
+            srcDevice.setParameter(DeviceServerSystem.LOGOUT_DEVICE,true);
         }
         if (!ConnectorPool.containConnection("rd")) {
             // connection is required for proper presence state configuration
index ff8312b..301ec2c 100644 (file)
@@ -116,7 +116,6 @@ public class DevicePresenceResource extends Resource {
                 Arrays.asList(Constants.DEVICE_ID, Constants.PRESENCE_STATE),
                 parsedPayload);
 
-        // store db
         PresenceManager.getInstance().updateDevicePresence(parsedPayload);
 
         // notification to observers
index 39774b2..9e22afa 100644 (file)
@@ -25,9 +25,14 @@ import org.iotivity.cloud.base.protocols.IResponse;
 
 import io.netty.channel.ChannelHandlerContext;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 public abstract class Device implements IResponseEventHandler {
     protected ChannelHandlerContext ctx;
 
+    private Map<String,Object> params = new ConcurrentHashMap<>();
+
     public Device(ChannelHandlerContext ctx) {
         this.ctx = ctx;
     }
@@ -54,4 +59,18 @@ public abstract class Device implements IResponseEventHandler {
     public abstract String getUserId();
 
     public abstract String getAccessToken();
+
+
+    public void setParameter(final String key, final Object value){
+        this.params.put(key,value);
+    }
+
+    public <T> T getParameter(final String key){
+        return (T)this.params.get(key);
+    }
+
+    public boolean existParameter(final String key){
+        return this.params.containsKey(key);
+    }
+
 }