From 3bad1f5747fa8d207c325dce828f50fe9d44858d Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Sun, 1 Oct 2017 10:51:28 +0530 Subject: [PATCH] add remote session dependency --- .../remote.session/dto/ClientSession.java | 39 -------------- .../remote.session/dto/DeviceSession.java | 47 ----------------- .../dto/{common => }/RemoteSession.java | 35 ++++++------- .../RemoteSessionInvalidException.java | 52 ------------------- pom.xml | 7 ++- 5 files changed, 23 insertions(+), 157 deletions(-) delete mode 100644 components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/ClientSession.java delete mode 100644 components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/DeviceSession.java rename components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/{common => }/RemoteSession.java (82%) delete mode 100644 components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/exception/RemoteSessionInvalidException.java diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/ClientSession.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/ClientSession.java deleted file mode 100644 index 70253479be..0000000000 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/ClientSession.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.wso2.carbon.device.mgt.extensions.remote.session.dto; - -import org.wso2.carbon.device.mgt.extensions.remote.session.dto.common.RemoteSession; - -import javax.websocket.Session; - -/** - * {@link ClientSession} is the represent of client which will be connecting to the device - */ -public class ClientSession extends RemoteSession { - - - public ClientSession(Session session, String tenantDomain, String deviceType, String deviceId, String operationId) { - super(session, tenantDomain, deviceType, deviceId, operationId); - } - - - @Override - public boolean applyThrottlingPolicy() { - return false; - } -} diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/DeviceSession.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/DeviceSession.java deleted file mode 100644 index 5e88dc0d9f..0000000000 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/DeviceSession.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.wso2.carbon.device.mgt.extensions.remote.session.dto; - -import org.wso2.carbon.device.mgt.extensions.remote.session.dto.common.RemoteSession; -import org.wso2.carbon.device.mgt.extensions.remote.session.internal.RemoteSessionManagementDataHolder; - -import javax.websocket.Session; - -/** - * {@link DeviceSession} is the represent of device which will be connecting based on client request - */ -public class DeviceSession extends RemoteSession { - - - public DeviceSession(Session session, String tenantDomain, String deviceType, String deviceId, String operationId) { - super(session, tenantDomain, deviceType, deviceId, operationId); - } - - - @Override - public boolean applyThrottlingPolicy() { - if (RemoteSessionManagementDataHolder.getInstance().getMessagesPerSession() > 0) { - long minDurationMessagesPerSecond = 1000 / RemoteSessionManagementDataHolder.getInstance() - .getMessagesPerSession(); - if ((System.currentTimeMillis() - getLastMessageTimeStamp()) < minDurationMessagesPerSecond) { - return true; - } - } - return false; - } -} diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/common/RemoteSession.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/RemoteSession.java similarity index 82% rename from components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/common/RemoteSession.java rename to components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/RemoteSession.java index cffea29e09..5402f131f2 100644 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/common/RemoteSession.java +++ b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/dto/RemoteSession.java @@ -19,8 +19,6 @@ package org.wso2.carbon.device.mgt.extensions.remote.session.dto.common; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONObject; -import org.wso2.carbon.device.mgt.extensions.remote.session.constants.RemoteSessionConstants; import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionInvalidException; import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException; @@ -31,10 +29,9 @@ import java.nio.ByteBuffer; /** * {@link RemoteSession} will represent remote websocket session - * This class implements the behaviours of sending message to the session in multithreaded environment. - * + * This class implements the behaviours of sending message to the session in multithreaded environment. */ -public abstract class RemoteSession { +public class RemoteSession { private static final Log log = LogFactory.getLog(RemoteSession.class); private String tenantDomain, operationId, deviceType, deviceId; @@ -44,23 +41,21 @@ public abstract class RemoteSession { private Session mySession; private final Object writeLockObject = new Object(); - protected RemoteSession(Session session, String tenantDomain, String deviceType, String deviceId, String - operationId) { + protected RemoteSession(Session session, String tenantDomain, String deviceType, String deviceId) { this.mySession = session; this.deviceType = deviceType; this.deviceId = deviceId; this.tenantDomain = tenantDomain; - this.operationId = operationId; } - public void sendMessage(Object message) throws RemoteSessionInvalidException, RemoteSessionManagementException { + private void sendMessage(Object message) throws RemoteSessionInvalidException, RemoteSessionManagementException { if (message != null) { boolean isMessageCountExceed = false; if (mySession != null && mySession.isOpen()) { synchronized (writeLockObject) { try { - isMessageCountExceed = applyThrottlingPolicy(); + isMessageCountExceed = applyRateLimit(); if (!isMessageCountExceed) { if (message instanceof String) { mySession.getBasicRemote().sendText(message.toString()); @@ -68,6 +63,8 @@ public abstract class RemoteSession { mySession.getBasicRemote().sendBinary(ByteBuffer.wrap((byte[]) message)); } this.lastMessageTimeStamp = System.currentTimeMillis(); + } else { + log.warn("Message count per second is exceeded for device id :" + deviceId); } } catch (IOException e) { log.warn("Send data to session failed due to ", e); @@ -77,23 +74,25 @@ public abstract class RemoteSession { throw new RemoteSessionInvalidException("Peer Session already closed ", new CloseReason (CloseReason.CloseCodes.CANNOT_ACCEPT, "Peer Session already closed ")); } - - if (isMessageCountExceed) { - JSONObject response = new JSONObject(); - response.put("code", RemoteSessionConstants.THROTTLE_OUT); - sendMessageToPeer(message.toString()); - } } else { throw new RemoteSessionManagementException("Message is empty"); } } - public void sendMessageToPeer(Object message) throws RemoteSessionInvalidException, RemoteSessionManagementException { + public void sendMessageToPeer(Object message) throws RemoteSessionInvalidException, + RemoteSessionManagementException { peerSession.sendMessage(message); } - public abstract boolean applyThrottlingPolicy(); + /** + * Use for limit the messages for given time + * + * @return message rate applied + */ + public boolean applyRateLimit(){ + return false; + } public Session getMySession() { diff --git a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/exception/RemoteSessionInvalidException.java b/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/exception/RemoteSessionInvalidException.java deleted file mode 100644 index 139cc74344..0000000000 --- a/components/extensions/remote-session-extension/org.wso2.carbon.device.mgt.extensions.remote.session/src/main/java/org/wso2/carbon/device/mgt/extensions/remote.session/exception/RemoteSessionInvalidException.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.wso2.carbon.device.mgt.extensions.remote.session.exception; - - -import javax.websocket.CloseReason; - -/** - * This Exception will be thrown, when there any interference with Remote RemoteSession. - */ -public class RemoteSessionInvalidException extends Exception { - - CloseReason closeReason; - - public RemoteSessionInvalidException(String msg, CloseReason closeReason, Exception nestedEx) { - super(msg, nestedEx); - this.closeReason = closeReason; - } - - public RemoteSessionInvalidException(String message, CloseReason closeReason, Throwable cause) { - super(message, cause); - this.closeReason = closeReason; - } - - public RemoteSessionInvalidException(String msg, CloseReason closeReason) { - super(msg); - this.closeReason = closeReason; - } - - public CloseReason getCloseReason() { - return closeReason; - } - - public void setCloseReason(CloseReason closeReason) { - this.closeReason = closeReason; - } -} diff --git a/pom.xml b/pom.xml index eb67c1f3a9..5d5eb34070 100644 --- a/pom.xml +++ b/pom.xml @@ -460,6 +460,11 @@ ${carbon.devicemgt.plugins.version} war + + org.wso2.carbon.devicemgt-plugins + org.wso2.carbon.device.mgt.extensions.remote.session + ${carbon.devicemgt.plugins.version} + @@ -1141,7 +1146,7 @@ 1.1.1 - 3.0.75 + 3.0.93-SNAPSHOT [3.0.0, 4.0.0)