[Simulator] Minor UI changes fixing the IOT-1087.
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / SessionManager.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.base;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.iotivity.cloud.util.Logger;
30
31 import io.netty.channel.ChannelHandlerContext;
32
33 public class SessionManager {
34
35     HashMap<String, ChannelHandlerContext> sessions = new HashMap<String, ChannelHandlerContext>();
36
37     public void addSession(String did, ChannelHandlerContext ctx) {
38         synchronized (sessions) {
39             sessions.put(did, ctx);
40         }
41     }
42
43     public void removeSession(String did) {
44
45         synchronized (sessions) {
46             sessions.remove(did);
47         }
48     }
49
50     public void removeSessionByChannel(ChannelHandlerContext ctx) {
51         synchronized (sessions) {
52             if (!isThereCtxChannel(ctx)) {
53                 Logger.d("Already Session Removed : "
54                         + ctx.channel().toString());
55                 return;
56             }
57             Iterator<String> iterator = sessions.keySet().iterator();
58             while (iterator.hasNext()) {
59                 String key = (String) iterator.next();
60                 if (ctx.channel().toString()
61                         .equals(querySession(key).channel().toString())) {
62                     Logger.d("Session Remove : " + ctx.channel().toString());
63                     removeSession(key);
64                     break;
65                 }
66             }
67         }
68     }
69
70     public ChannelHandlerContext querySession(String did) {
71         ChannelHandlerContext ctx = null;
72
73         synchronized (sessions) {
74             ctx = sessions.get(did);
75         }
76
77         return ctx;
78     }
79
80     public boolean isThereCtx(ChannelHandlerContext ctx) {
81
82         synchronized (sessions) {
83             return sessions.containsValue(ctx);
84         }
85     }
86
87     public boolean isThereCtxChannel(ChannelHandlerContext ctx) {
88
89         synchronized (sessions) {
90             Iterator<String> iterator = sessions.keySet().iterator();
91             while (iterator.hasNext()) {
92                 String key = (String) iterator.next();
93                 if (ctx.channel().toString()
94                         .equals(querySession(key).channel().toString())) {
95                     return true;
96                 }
97             }
98             return false;
99         }
100     }
101
102     public List<String> getSessions() {
103         return new ArrayList<String>(sessions.keySet());
104     }
105 }