7 changed files with 347 additions and 44 deletions
@ -0,0 +1,14 @@ |
|||||
|
package org.thingsboard.server.service.edge; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.server.dao.edge.EdgeService; |
||||
|
|
||||
|
@Component |
||||
|
@Data |
||||
|
public class EdgeContextComponent { |
||||
|
|
||||
|
@Autowired |
||||
|
private EdgeService edgeService; |
||||
|
} |
||||
@ -0,0 +1,130 @@ |
|||||
|
package org.thingsboard.server.service.edge.rpc; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
|
import io.grpc.stub.StreamObserver; |
||||
|
import lombok.Data; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.thingsboard.server.common.data.edge.Edge; |
||||
|
import org.thingsboard.server.common.data.id.EdgeId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.gen.edge.ConnectRequestMsg; |
||||
|
import org.thingsboard.server.gen.edge.ConnectResponseCode; |
||||
|
import org.thingsboard.server.gen.edge.ConnectResponseMsg; |
||||
|
import org.thingsboard.server.gen.edge.EdgeConfigurationProto; |
||||
|
import org.thingsboard.server.gen.edge.RequestMsg; |
||||
|
import org.thingsboard.server.gen.edge.RequestMsgType; |
||||
|
import org.thingsboard.server.gen.edge.ResponseMsg; |
||||
|
import org.thingsboard.server.gen.edge.UplinkMsg; |
||||
|
import org.thingsboard.server.gen.edge.UplinkResponseMsg; |
||||
|
import org.thingsboard.server.service.edge.EdgeContextComponent; |
||||
|
|
||||
|
import java.util.Optional; |
||||
|
import java.util.UUID; |
||||
|
import java.util.function.BiConsumer; |
||||
|
import java.util.function.Consumer; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Data |
||||
|
public final class EdgeGrpcSession implements Cloneable { |
||||
|
|
||||
|
private final UUID sessionId; |
||||
|
private final BiConsumer<EdgeId, EdgeGrpcSession> sessionOpenListener; |
||||
|
private final Consumer<EdgeId> sessionCloseListener; |
||||
|
|
||||
|
private EdgeContextComponent ctx; |
||||
|
private Edge edge; |
||||
|
private StreamObserver<RequestMsg> inputStream; |
||||
|
private StreamObserver<ResponseMsg> outputStream; |
||||
|
private boolean connected; |
||||
|
|
||||
|
EdgeGrpcSession(EdgeContextComponent ctx, StreamObserver<ResponseMsg> outputStream |
||||
|
, BiConsumer<EdgeId, EdgeGrpcSession> sessionOpenListener |
||||
|
, Consumer<EdgeId> sessionCloseListener) { |
||||
|
this.sessionId = UUID.randomUUID(); |
||||
|
this.ctx = ctx; |
||||
|
this.outputStream = outputStream; |
||||
|
this.sessionOpenListener = sessionOpenListener; |
||||
|
this.sessionCloseListener = sessionCloseListener; |
||||
|
initInputStream(); |
||||
|
} |
||||
|
|
||||
|
private void initInputStream() { |
||||
|
this.inputStream = new StreamObserver<RequestMsg>() { |
||||
|
@Override |
||||
|
public void onNext(RequestMsg requestMsg) { |
||||
|
if (!connected && requestMsg.getMsgType().equals(RequestMsgType.CONNECT_RPC_MESSAGE)) { |
||||
|
ConnectResponseMsg responseMsg = processConnect(requestMsg.getConnectRequestMsg()); |
||||
|
outputStream.onNext(ResponseMsg.newBuilder() |
||||
|
.setConnectResponseMsg(responseMsg) |
||||
|
.build()); |
||||
|
if (ConnectResponseCode.ACCEPTED != responseMsg.getResponseCode()) { |
||||
|
outputStream.onError(new RuntimeException(responseMsg.getErrorMsg())); |
||||
|
} |
||||
|
} |
||||
|
if (connected) { |
||||
|
if (requestMsg.getMsgType().equals(RequestMsgType.UPLINK_RPC_MESSAGE) && requestMsg.hasUplinkMsg()) { |
||||
|
outputStream.onNext(ResponseMsg.newBuilder() |
||||
|
.setUplinkResponseMsg(processUplinkMsg(requestMsg.getUplinkMsg())) |
||||
|
.build()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void onError(Throwable t) { |
||||
|
log.error("Failed to deliver message from client!", t); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void onCompleted() { |
||||
|
sessionCloseListener.accept(edge.getId()); |
||||
|
outputStream.onCompleted(); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
private UplinkResponseMsg processUplinkMsg(UplinkMsg uplinkMsg) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private ConnectResponseMsg processConnect(ConnectRequestMsg request) { |
||||
|
Optional<Edge> optional = ctx.getEdgeService().findEdgeByRoutingKey(TenantId.SYS_TENANT_ID, request.getEdgeRoutingKey()); |
||||
|
if (optional.isPresent()) { |
||||
|
edge = optional.get(); |
||||
|
try { |
||||
|
if (edge.getSecret().equals(request.getEdgeSecret())) { |
||||
|
connected = true; |
||||
|
sessionOpenListener.accept(edge.getId(), this); |
||||
|
return ConnectResponseMsg.newBuilder() |
||||
|
.setResponseCode(ConnectResponseCode.ACCEPTED) |
||||
|
.setErrorMsg("") |
||||
|
.setConfiguration(constructEdgeConfigProto(edge)).build(); |
||||
|
} |
||||
|
return ConnectResponseMsg.newBuilder() |
||||
|
.setResponseCode(ConnectResponseCode.BAD_CREDENTIALS) |
||||
|
.setErrorMsg("Failed to validate the edge!") |
||||
|
.setConfiguration(EdgeConfigurationProto.getDefaultInstance()).build(); |
||||
|
} catch (Exception e) { |
||||
|
log.error("[{}] Failed to process edge connection!", request.getEdgeRoutingKey(), e); |
||||
|
return ConnectResponseMsg.newBuilder() |
||||
|
.setResponseCode(ConnectResponseCode.SERVER_UNAVAILABLE) |
||||
|
.setErrorMsg("Failed to process edge connection!") |
||||
|
.setConfiguration(EdgeConfigurationProto.getDefaultInstance()).build(); |
||||
|
} |
||||
|
} |
||||
|
return ConnectResponseMsg.newBuilder() |
||||
|
.setResponseCode(ConnectResponseCode.BAD_CREDENTIALS) |
||||
|
.setErrorMsg("Failed to find the edge! Routing key: " + request.getEdgeRoutingKey()) |
||||
|
.setConfiguration(EdgeConfigurationProto.getDefaultInstance()).build(); |
||||
|
} |
||||
|
|
||||
|
private EdgeConfigurationProto constructEdgeConfigProto(Edge edge) throws JsonProcessingException { |
||||
|
return EdgeConfigurationProto.newBuilder() |
||||
|
.setTenantIdMSB(edge.getTenantId().getId().getMostSignificantBits()) |
||||
|
.setTenantIdLSB(edge.getTenantId().getId().getLeastSignificantBits()) |
||||
|
.setName(edge.getName()) |
||||
|
.setRoutingKey(edge.getRoutingKey()) |
||||
|
.setType(edge.getType().toString()) |
||||
|
.build(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2019 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed 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.thingsboard.edge.exception; |
||||
|
|
||||
|
public class EdgeConnectionException extends RuntimeException { |
||||
|
|
||||
|
private static final long serialVersionUID = -4372754681230555723L; |
||||
|
|
||||
|
public EdgeConnectionException(String message) { |
||||
|
super(message); |
||||
|
} |
||||
|
|
||||
|
public EdgeConnectionException(String message, Throwable cause) { |
||||
|
super(message, cause); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue