mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
add remote session dependency
This commit is contained in:
parent
808b0d2e5d
commit
3bad1f5747
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
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.RemoteSessionInvalidException;
|
||||||
import org.wso2.carbon.device.mgt.extensions.remote.session.exception.RemoteSessionManagementException;
|
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
|
* {@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 static final Log log = LogFactory.getLog(RemoteSession.class);
|
||||||
private String tenantDomain, operationId, deviceType, deviceId;
|
private String tenantDomain, operationId, deviceType, deviceId;
|
||||||
@ -44,23 +41,21 @@ public abstract class RemoteSession {
|
|||||||
private Session mySession;
|
private Session mySession;
|
||||||
private final Object writeLockObject = new Object();
|
private final Object writeLockObject = new Object();
|
||||||
|
|
||||||
protected RemoteSession(Session session, String tenantDomain, String deviceType, String deviceId, String
|
protected RemoteSession(Session session, String tenantDomain, String deviceType, String deviceId) {
|
||||||
operationId) {
|
|
||||||
this.mySession = session;
|
this.mySession = session;
|
||||||
this.deviceType = deviceType;
|
this.deviceType = deviceType;
|
||||||
this.deviceId = deviceId;
|
this.deviceId = deviceId;
|
||||||
this.tenantDomain = tenantDomain;
|
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) {
|
if (message != null) {
|
||||||
boolean isMessageCountExceed = false;
|
boolean isMessageCountExceed = false;
|
||||||
if (mySession != null && mySession.isOpen()) {
|
if (mySession != null && mySession.isOpen()) {
|
||||||
synchronized (writeLockObject) {
|
synchronized (writeLockObject) {
|
||||||
try {
|
try {
|
||||||
isMessageCountExceed = applyThrottlingPolicy();
|
isMessageCountExceed = applyRateLimit();
|
||||||
if (!isMessageCountExceed) {
|
if (!isMessageCountExceed) {
|
||||||
if (message instanceof String) {
|
if (message instanceof String) {
|
||||||
mySession.getBasicRemote().sendText(message.toString());
|
mySession.getBasicRemote().sendText(message.toString());
|
||||||
@ -68,6 +63,8 @@ public abstract class RemoteSession {
|
|||||||
mySession.getBasicRemote().sendBinary(ByteBuffer.wrap((byte[]) message));
|
mySession.getBasicRemote().sendBinary(ByteBuffer.wrap((byte[]) message));
|
||||||
}
|
}
|
||||||
this.lastMessageTimeStamp = System.currentTimeMillis();
|
this.lastMessageTimeStamp = System.currentTimeMillis();
|
||||||
|
} else {
|
||||||
|
log.warn("Message count per second is exceeded for device id :" + deviceId);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warn("Send data to session failed due to ", 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
|
throw new RemoteSessionInvalidException("Peer Session already closed ", new CloseReason
|
||||||
(CloseReason.CloseCodes.CANNOT_ACCEPT, "Peer Session already closed "));
|
(CloseReason.CloseCodes.CANNOT_ACCEPT, "Peer Session already closed "));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMessageCountExceed) {
|
|
||||||
JSONObject response = new JSONObject();
|
|
||||||
response.put("code", RemoteSessionConstants.THROTTLE_OUT);
|
|
||||||
sendMessageToPeer(message.toString());
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw new RemoteSessionManagementException("Message is empty");
|
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);
|
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() {
|
public Session getMySession() {
|
||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
7
pom.xml
7
pom.xml
@ -460,6 +460,11 @@
|
|||||||
<version>${carbon.devicemgt.plugins.version}</version>
|
<version>${carbon.devicemgt.plugins.version}</version>
|
||||||
<type>war</type>
|
<type>war</type>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.extensions.remote.session</artifactId>
|
||||||
|
<version>${carbon.devicemgt.plugins.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--Arduino DeviceType Impl, API and Agent-->
|
<!--Arduino DeviceType Impl, API and Agent-->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -1141,7 +1146,7 @@
|
|||||||
<javax.ws.rs.version>1.1.1</javax.ws.rs.version>
|
<javax.ws.rs.version>1.1.1</javax.ws.rs.version>
|
||||||
|
|
||||||
<!-- Carbon Device Management -->
|
<!-- Carbon Device Management -->
|
||||||
<carbon.devicemgt.version>3.0.75</carbon.devicemgt.version>
|
<carbon.devicemgt.version>3.0.93-SNAPSHOT</carbon.devicemgt.version>
|
||||||
<carbon.devicemgt.version.range>[3.0.0, 4.0.0)</carbon.devicemgt.version.range>
|
<carbon.devicemgt.version.range>[3.0.0, 4.0.0)</carbon.devicemgt.version.range>
|
||||||
|
|
||||||
<!-- Carbon App Management -->
|
<!-- Carbon App Management -->
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user