import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
-import java.util.Iterator;
+import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
*
*/
public class KeepAliveResource extends Resource {
- private int[] mIntervals = null;
- private Timer mTimer = new Timer();
- private Cbor<HashMap<String, Integer>> mCbor = new Cbor<>();
- private HashMap<Device, Long> mConnectionPool = new HashMap<>();
+ private int[] mIntervals = null;
+ private Timer mTimer = new Timer();
+ private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
+ private HashMap<Device, Long> mConnectionPool = new HashMap<>();
public KeepAliveResource(int[] intervals) {
super(Arrays.asList(Constants.PREFIX_OIC, Constants.KEEP_ALIVE_URI));
switch (request.getMethod()) {
case GET:
- response = handleGetPingConfig(request);
- mConnectionPool.put(srcDevice, System.currentTimeMillis()
- + (mIntervals[0] * (long) 60000));
+ response = handlePingConfig(request);
break;
case PUT:
- response = handlePutPingConfig(srcDevice, request);
+ case POST:
+ response = handlePingConfig(srcDevice, request);
break;
default:
srcDevice.sendResponse(response);
}
- public void startSessionChecker() {
- mTimer.schedule(new KeepAliveTask(), 30000, 60000);
+ public void startSessionChecker(int startTime, int intervalTime) {
+ mTimer.schedule(new KeepAliveTask(), startTime, intervalTime);
}
public void stopSessionChecker() {
* @param request
* ChannelHandlerContext of request message
*/
- private IResponse handleGetPingConfig(IRequest request) {
+ private IResponse handlePingConfig(IRequest request) {
HashMap<String, int[]> payloadData = new HashMap<>();
- payloadData.put("inarray", mIntervals);
+ payloadData.put(Constants.REQ_PING_ARRAY, mIntervals);
return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
ContentFormat.APPLICATION_CBOR,
mCbor.encodingPayloadToCbor(payloadData));
}
- private IResponse handlePutPingConfig(Device srcDevice, IRequest request) {
+ private IResponse handlePingConfig(Device srcDevice, IRequest request) {
- HashMap<String, Integer> payloadData = mCbor
+ HashMap<String, Object> payloadData = mCbor
.parsePayloadFromCbor(request.getPayload(), HashMap.class);
- if (payloadData != null) {
- if (payloadData.containsKey("in")) {
- mConnectionPool.put(srcDevice, System.currentTimeMillis()
- + (payloadData.get("in") * (long) 60000));
- }
- }
+
+ checkPayloadException(Constants.REQ_PING, payloadData);
+
+ Long pingTime = Integer.valueOf(
+ payloadData.get(Constants.REQ_PING).toString()) * (long) 60000;
+ Long connectionTime = System.currentTimeMillis() + pingTime;
+ mConnectionPool.put(srcDevice, connectionTime);
+
return MessageBuilder.createResponse(request, ResponseStatus.VALID);
}
public void run() {
Map<Device, Long> map = Collections
.synchronizedMap(mConnectionPool);
- Set<Device> keySet = map.keySet();
- ArrayList<Device> deleteList = new ArrayList<>();
- Iterator<Device> iterator = null;
+
+ List<Device> deleteList = new ArrayList<>();
+
synchronized (map) {
- iterator = keySet.iterator();
Long currentTime = System.currentTimeMillis();
- // check interval
- while (iterator.hasNext()) {
- Device key = iterator.next();
- if (map.containsKey(key)) {
- if (map.get(key) != null) {
- Long lifeTime = (Long) map.get(key);
- if (lifeTime != null) {
- if (lifeTime < currentTime) {
- deleteList.add(key);
- }
- }
- }
+ for (Device device : map.keySet()) {
+ Long lifeTime = (Long) map.get(device);
+ if (lifeTime != null && lifeTime < currentTime) {
+ deleteList.add(device);
}
}
-
}
- iterator = deleteList.iterator();
- // remove session
- while (iterator.hasNext()) {
- Device key = iterator.next();
- mConnectionPool.remove(key);
- key.getCtx().fireChannelInactive();
- key.getCtx().close();
+
+ for (Device device : deleteList) {
+ mConnectionPool.remove(device);
+ device.getCtx().fireChannelInactive();
+ device.getCtx().close();
}
}
}