|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.navercorp.pinpoint.collector.cluster; |
| 17 | + |
| 18 | +import com.google.protobuf.GeneratedMessageV3; |
| 19 | +import com.navercorp.pinpoint.io.ResponseMessage; |
| 20 | +import com.navercorp.pinpoint.realtime.collector.service.AgentConnection; |
| 21 | +import com.navercorp.pinpoint.rpc.stream.ClientStreamChannel; |
| 22 | +import com.navercorp.pinpoint.rpc.stream.ClientStreamChannelEventHandler; |
| 23 | +import com.navercorp.pinpoint.rpc.stream.StreamException; |
| 24 | +import com.navercorp.pinpoint.thrift.sender.message.CommandGrpcToThriftMessageConverter; |
| 25 | +import org.apache.thrift.TBase; |
| 26 | + |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.concurrent.CompletableFuture; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author youngjin.kim2 |
| 32 | + */ |
| 33 | +public class AgentConnectionImpl implements AgentConnection { |
| 34 | + |
| 35 | + private final ClusterPoint<?> clusterPoint; |
| 36 | + private final CommandGrpcToThriftMessageConverter messageConverter; |
| 37 | + |
| 38 | + public AgentConnectionImpl(ClusterPoint<?> clusterPoint, CommandGrpcToThriftMessageConverter messageConverter) { |
| 39 | + this.clusterPoint = Objects.requireNonNull(clusterPoint, "clusterPoint"); |
| 40 | + this.messageConverter = Objects.requireNonNull(messageConverter, "messageConverter"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public ClientStreamChannel requestStream(ClientStreamChannelEventHandler handler, GeneratedMessageV3 command) { |
| 45 | + TBase<?, ?> tCommand = this.messageConverter.toMessage(command); |
| 46 | + if (!this.clusterPoint.isSupportCommand(tCommand)) { |
| 47 | + throw new RuntimeException("Unsupported command: " + command); |
| 48 | + } |
| 49 | + if (clusterPoint instanceof GrpcAgentConnection) { |
| 50 | + return openStream(handler, tCommand); |
| 51 | + } |
| 52 | + throw new RuntimeException("Invalid clusterPoint: " + clusterPoint); |
| 53 | + } |
| 54 | + |
| 55 | + private ClientStreamChannel openStream(ClientStreamChannelEventHandler handler, TBase<?, ?> tCommand) { |
| 56 | + try { |
| 57 | + return ((GrpcAgentConnection) clusterPoint).openStream(tCommand, handler); |
| 58 | + } catch (StreamException e) { |
| 59 | + throw new RuntimeException("Failed to openStream " + tCommand); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public CompletableFuture<ResponseMessage> request(GeneratedMessageV3 command) { |
| 65 | + TBase<?, ?> tCommand = this.messageConverter.toMessage(command); |
| 66 | + if (!this.clusterPoint.isSupportCommand(tCommand)) { |
| 67 | + throw new RuntimeException("Unsupported command: " + command); |
| 68 | + } |
| 69 | + if (clusterPoint instanceof GrpcAgentConnection) { |
| 70 | + return ((GrpcAgentConnection) this.clusterPoint).request(tCommand); |
| 71 | + } |
| 72 | + throw new RuntimeException("Invalid clusterPoint: " + clusterPoint); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments