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, Integer>> 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 = handleGetPingConfig(request);
74 response = handlePutPingConfig(srcDevice, request);
78 throw new BadRequestException(
79 request.getMethod() + " request type is not support");
82 srcDevice.sendResponse(response);
85 public void startSessionChecker() {
86 mTimer.schedule(new KeepAliveTask(), 30000, 60000);
89 public void stopSessionChecker() {
94 * API for making interval and first response to Resource
97 * ChannelHandlerContext of request message
99 private IResponse handleGetPingConfig(IRequest request) {
101 HashMap<String, int[]> payloadData = new HashMap<>();
102 payloadData.put("inarray", mIntervals);
104 return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
105 ContentFormat.APPLICATION_CBOR,
106 mCbor.encodingPayloadToCbor(payloadData));
109 private IResponse handlePutPingConfig(Device srcDevice, IRequest request) {
111 HashMap<String, Integer> payloadData = mCbor
112 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
114 if (payloadData.containsKey("in")) {
115 mConnectionPool.put(srcDevice, System.currentTimeMillis()
116 + (payloadData.get("in") * (long) 60000));
119 return MessageBuilder.createResponse(request, ResponseStatus.VALID);
123 * API for managing session
125 private class KeepAliveTask extends TimerTask {
129 Map<Device, Long> map = Collections
130 .synchronizedMap(mConnectionPool);
132 List<Device> deleteList = new ArrayList<>();
135 Long currentTime = System.currentTimeMillis();
136 for (Device device : map.keySet()) {
137 Long lifeTime = (Long) map.get(device);
138 if (lifeTime < currentTime) {
139 deleteList.add(device);
144 for (Device device : deleteList) {
145 mConnectionPool.remove(device);
146 device.getCtx().fireChannelInactive();
147 device.getCtx().close();