• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.datasources.transport.grpc;
17 
18 import io.grpc.ManagedChannel;
19 import io.grpc.ManagedChannelBuilder;
20 import io.grpc.StatusRuntimeException;
21 import ohos.devtools.datasources.transport.grpc.service.IProfilerServiceGrpc;
22 import ohos.devtools.datasources.transport.grpc.service.ProfilerServiceTypes;
23 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
24 import ohos.devtools.views.common.LayoutConstants;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 
28 import java.util.Iterator;
29 import java.util.concurrent.TimeUnit;
30 
31 /**
32  * Provide device-side grpc interface encapsulation
33  *
34  * @since 2021/5/19 16:39
35  */
36 public class ProfilerClient {
37     private static final Logger LOGGER = LogManager.getLogger(ProfilerClient.class);
38 
39     /**
40      * IProfilerServiceStub
41      */
42     private IProfilerServiceGrpc.IProfilerServiceStub profilerServiceStub;
43 
44     private ManagedChannel channel;
45 
46     private String host;
47 
48     private int port;
49 
50     private IProfilerServiceGrpc.IProfilerServiceBlockingStub profilerBlockInClient;
51 
52     /**
53      * ProfilerClient
54      *
55      * @param host localhost
56      * @param port port number
57      */
ProfilerClient(String host, int port)58     public ProfilerClient(String host, int port) {
59         this(host, port, null);
60     }
61 
62     /**
63      * ProfilerClient
64      *
65      * @param host localhost
66      * @param port port number
67      * @param channel channel
68      */
ProfilerClient(String host, int port, ManagedChannel channel)69     public ProfilerClient(String host, int port, ManagedChannel channel) {
70         if (ProfilerLogManager.isInfoEnabled()) {
71             LOGGER.info("ProfilerClient");
72         }
73         this.host = host;
74         this.port = port;
75         if (channel == null || channel.isShutdown() || channel.isTerminated()) {
76             this.channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
77         } else {
78             this.channel = channel;
79         }
80         profilerBlockInClient = IProfilerServiceGrpc.newBlockingStub(this.channel);
81         profilerServiceStub = IProfilerServiceGrpc.newStub(this.channel);
82     }
83 
84     /**
85      * get profiler client
86      *
87      * @return IProfilerServiceGrpc.IProfilerServiceBlockingStub
88      */
getProfilerClient()89     public IProfilerServiceGrpc.IProfilerServiceBlockingStub getProfilerClient() {
90         if (ProfilerLogManager.isInfoEnabled()) {
91             LOGGER.info("getProfilerClient");
92         }
93         return profilerBlockInClient;
94     }
95 
96     /**
97      * 获取支持的插件列表
98      *
99      * @param getCapabilitiesRequest getCapabilitiesRequest
100      * @return ProfilerServiceTypes.GetCapabilitiesResponse
101      * @throws StatusRuntimeException StatusRuntimeException
102      */
getCapabilities( ProfilerServiceTypes.GetCapabilitiesRequest getCapabilitiesRequest)103     public ProfilerServiceTypes.GetCapabilitiesResponse getCapabilities(
104         ProfilerServiceTypes.GetCapabilitiesRequest getCapabilitiesRequest) throws StatusRuntimeException {
105         if (ProfilerLogManager.isInfoEnabled()) {
106             LOGGER.info("getProfilerClient");
107         }
108         return profilerBlockInClient.withDeadlineAfter(LayoutConstants.FIVE, TimeUnit.SECONDS)
109             .getCapabilities(getCapabilitiesRequest);
110     }
111 
112     /**
113      * createSession
114      *
115      * @param createSessionRequest createSessionRequest
116      * @return ProfilerServiceTypes.CreateSessionResponse
117      * @throws StatusRuntimeException GrpcException
118      */
createSession( ProfilerServiceTypes.CreateSessionRequest createSessionRequest)119     public ProfilerServiceTypes.CreateSessionResponse createSession(
120         ProfilerServiceTypes.CreateSessionRequest createSessionRequest) throws StatusRuntimeException {
121         if (ProfilerLogManager.isInfoEnabled()) {
122             LOGGER.info("createSession");
123         }
124         return profilerBlockInClient.withDeadlineAfter(LayoutConstants.FIVE, TimeUnit.SECONDS)
125             .createSession(createSessionRequest);
126     }
127 
128     /**
129      * startSession
130      *
131      * @param startSessionRequest startSessionRequest
132      * @return ProfilerServiceTypes.StartSessionResponse
133      * @throws StatusRuntimeException StatusRuntimeException
134      */
startSession( ProfilerServiceTypes.StartSessionRequest startSessionRequest)135     public ProfilerServiceTypes.StartSessionResponse startSession(
136         ProfilerServiceTypes.StartSessionRequest startSessionRequest) throws StatusRuntimeException {
137         if (ProfilerLogManager.isInfoEnabled()) {
138             LOGGER.info("startSession");
139         }
140         return profilerBlockInClient.withDeadlineAfter(LayoutConstants.THREE, TimeUnit.SECONDS)
141             .startSession(startSessionRequest);
142     }
143 
144     /**
145      * 抓取数据
146      *
147      * @param fetchDataRequest fetchDataRequest
148      * @return Iterator<ProfilerServiceTypes.FetchDataResponse>
149      * @throws StatusRuntimeException StatusRuntimeException
150      */
fetchData( ProfilerServiceTypes.FetchDataRequest fetchDataRequest)151     public Iterator<ProfilerServiceTypes.FetchDataResponse> fetchData(
152         ProfilerServiceTypes.FetchDataRequest fetchDataRequest) throws StatusRuntimeException {
153         if (ProfilerLogManager.isInfoEnabled()) {
154             LOGGER.info("fetchData");
155         }
156         return profilerBlockInClient.withMaxInboundMessageSize(Integer.MAX_VALUE)
157             .withMaxOutboundMessageSize(Integer.MAX_VALUE).fetchData(fetchDataRequest);
158     }
159 
160     /**
161      * stop Session
162      *
163      * @param stopSessionRequest stopSessionRequest
164      * @return ProfilerServiceTypes.StopSessionResponse
165      * @throws StatusRuntimeException StatusRuntimeException
166      */
stopSession( ProfilerServiceTypes.StopSessionRequest stopSessionRequest)167     public ProfilerServiceTypes.StopSessionResponse stopSession(
168         ProfilerServiceTypes.StopSessionRequest stopSessionRequest) throws StatusRuntimeException {
169         if (ProfilerLogManager.isInfoEnabled()) {
170             LOGGER.info("stopSession");
171         }
172         return profilerBlockInClient.withDeadlineAfter(3, TimeUnit.SECONDS).stopSession(stopSessionRequest);
173     }
174 
175     /**
176      * destroy Session
177      *
178      * @param destroyRequest destroyRequest
179      * @return ProfilerServiceTypes.DestroySessionResponse
180      * @throws StatusRuntimeException StatusRuntimeException
181      */
destroySession( ProfilerServiceTypes.DestroySessionRequest destroyRequest)182     public ProfilerServiceTypes.DestroySessionResponse destroySession(
183         ProfilerServiceTypes.DestroySessionRequest destroyRequest) throws StatusRuntimeException {
184         if (ProfilerLogManager.isInfoEnabled()) {
185             LOGGER.info("destroySession");
186         }
187         return profilerBlockInClient.withDeadlineAfter(1, TimeUnit.SECONDS).destroySession(destroyRequest);
188     }
189 
190     /**
191      * keepSession
192      *
193      * @param keepSessionRequest keepSessionRequest
194      * @return ProfilerServiceTypes.KeepSessionResponse
195      * @throws StatusRuntimeException StatusRuntimeException
196      */
keepSession( ProfilerServiceTypes.KeepSessionRequest keepSessionRequest)197     public ProfilerServiceTypes.KeepSessionResponse keepSession(
198         ProfilerServiceTypes.KeepSessionRequest keepSessionRequest) throws StatusRuntimeException {
199         if (ProfilerLogManager.isInfoEnabled()) {
200             LOGGER.info("keepSession");
201         }
202         return profilerBlockInClient.withDeadlineAfter(3, TimeUnit.SECONDS).keepSession(keepSessionRequest);
203     }
204 
205     /**
206      * Closing method
207      */
shutdown()208     public void shutdown() {
209         try {
210             if (ProfilerLogManager.isInfoEnabled()) {
211                 LOGGER.info("shutdown");
212             }
213             channel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
214         } catch (InterruptedException exception) {
215             if (ProfilerLogManager.isErrorEnabled()) {
216                 LOGGER.error(exception.getMessage());
217             }
218         }
219     }
220 
221     /**
222      * get ManagedChannel
223      *
224      * @return ManagedChannel
225      */
getChannel()226     public ManagedChannel getChannel() {
227         return channel;
228     }
229 
230     /**
231      * getProfilerServiceStub
232      *
233      * @return IProfilerServiceGrpc.IProfilerServiceStub
234      */
getProfilerServiceStub()235     public IProfilerServiceGrpc.IProfilerServiceStub getProfilerServiceStub() {
236         return profilerServiceStub;
237     }
238 }
239