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.ciserver.resources;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Timer;
31 import java.util.TimerTask;
33 import org.iotivity.cloud.base.device.Device;
34 import org.iotivity.cloud.base.exception.ServerException;
35 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
36 import org.iotivity.cloud.base.protocols.IRequest;
37 import org.iotivity.cloud.base.protocols.IResponse;
38 import org.iotivity.cloud.base.protocols.MessageBuilder;
39 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
40 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
41 import org.iotivity.cloud.base.resource.Resource;
42 import org.iotivity.cloud.ciserver.Constants;
43 import org.iotivity.cloud.util.Cbor;
47 * This class provides a set of APIs to use KeepAlive Resource for ensuring the
51 public class KeepAliveResource extends Resource {
52 private int[] mIntervals = null;
53 private Timer mTimer = new Timer();
54 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
55 private HashMap<Device, Long> mConnectionPool = new HashMap<>();
57 public KeepAliveResource(int[] intervals) {
58 super(Arrays.asList(Constants.PREFIX_OIC, Constants.KEEP_ALIVE_URI));
59 mIntervals = intervals;
63 public void onDefaultRequestReceived(Device srcDevice, IRequest request)
64 throws ServerException {
66 IResponse response = null;
68 switch (request.getMethod()) {
70 response = handlePingConfig(request);
75 response = handlePingConfig(srcDevice, request);
79 throw new BadRequestException(
80 request.getMethod() + " request type is not support");
83 srcDevice.sendResponse(response);
86 public void startSessionChecker(int startTime, int intervalTime) {
87 mTimer.schedule(new KeepAliveTask(), startTime, intervalTime);
90 public void stopSessionChecker() {
95 * API for making interval and first response to Resource
98 * ChannelHandlerContext of request message
100 private IResponse handlePingConfig(IRequest request) {
102 HashMap<String, int[]> payloadData = new HashMap<>();
103 payloadData.put(Constants.REQ_PING_ARRAY, mIntervals);
105 return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
106 ContentFormat.APPLICATION_CBOR,
107 mCbor.encodingPayloadToCbor(payloadData));
110 private IResponse handlePingConfig(Device srcDevice, IRequest request) {
112 HashMap<String, Object> payloadData = mCbor
113 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
115 checkPayloadException(Constants.REQ_PING, payloadData);
117 Long pingTime = Integer.valueOf(
118 payloadData.get(Constants.REQ_PING).toString()) * (long) 60000;
119 Long connectionTime = System.currentTimeMillis() + pingTime;
120 mConnectionPool.put(srcDevice, connectionTime);
122 return MessageBuilder.createResponse(request, ResponseStatus.VALID);
126 * API for managing session
128 private class KeepAliveTask extends TimerTask {
132 Map<Device, Long> map = Collections
133 .synchronizedMap(mConnectionPool);
135 List<Device> deleteList = new ArrayList<>();
138 Long currentTime = System.currentTimeMillis();
139 for (Device device : map.keySet()) {
140 Long lifeTime = (Long) map.get(device);
141 if (lifeTime != null && lifeTime < currentTime) {
142 deleteList.add(device);
147 for (Device device : deleteList) {
148 mConnectionPool.remove(device);
149 device.getCtx().fireChannelInactive();
150 device.getCtx().close();