2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.base;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
29 import org.iotivity.cloud.util.Logger;
31 import io.netty.channel.ChannelHandlerContext;
33 public class SessionManager {
35 HashMap<String, ChannelHandlerContext> sessions = new HashMap<String, ChannelHandlerContext>();
37 public void addSession(String did, ChannelHandlerContext ctx) {
38 synchronized (sessions) {
39 sessions.put(did, ctx);
43 public void removeSession(String did) {
45 synchronized (sessions) {
50 public void removeSessionByChannel(ChannelHandlerContext ctx) {
51 synchronized (sessions) {
52 if (!isThereCtxChannel(ctx)) {
53 Logger.d("Already Session Removed : "
54 + ctx.channel().toString());
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());
70 public ChannelHandlerContext querySession(String did) {
71 ChannelHandlerContext ctx = null;
73 synchronized (sessions) {
74 ctx = sessions.get(did);
80 public boolean isThereCtx(ChannelHandlerContext ctx) {
82 synchronized (sessions) {
83 return sessions.containsValue(ctx);
87 public boolean isThereCtxChannel(ChannelHandlerContext ctx) {
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())) {
102 public List<String> getSessions() {
103 return new ArrayList<String>(sessions.keySet());