• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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.android.tradefed.result.proto;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.invoker.IInvocationContext;
20 import com.android.tradefed.log.LogUtil.CLog;
21 import com.android.tradefed.result.proto.TestRecordProto.TestRecord;
22 import com.android.tradefed.util.StreamUtil;
23 
24 import java.io.IOException;
25 import java.net.Socket;
26 
27 /** An implementation of {@link ProtoResultReporter} */
28 public final class StreamProtoResultReporter extends ProtoResultReporter {
29 
30     public static final String PROTO_REPORT_PORT_OPTION = "proto-report-port";
31 
32     @Option(
33         name = PROTO_REPORT_PORT_OPTION,
34         description = "the port where to connect to send the protos."
35     )
36     private Integer mReportPort = null;
37 
38     private Socket mReportSocket = null;
39 
40     @Override
processStartInvocation( TestRecord invocationStartRecord, IInvocationContext context)41     public void processStartInvocation(
42             TestRecord invocationStartRecord, IInvocationContext context) {
43         writeRecordToSocket(invocationStartRecord);
44     }
45 
46     @Override
processTestModuleStarted(TestRecord moduleStartRecord)47     public void processTestModuleStarted(TestRecord moduleStartRecord) {
48         writeRecordToSocket(moduleStartRecord);
49     }
50 
51     @Override
processTestModuleEnd(TestRecord moduleRecord)52     public void processTestModuleEnd(TestRecord moduleRecord) {
53         writeRecordToSocket(moduleRecord);
54     }
55 
56     @Override
processTestRunStarted(TestRecord runStartedRecord)57     public void processTestRunStarted(TestRecord runStartedRecord) {
58         writeRecordToSocket(runStartedRecord);
59     }
60 
61     @Override
processTestRunEnded(TestRecord runRecord)62     public void processTestRunEnded(TestRecord runRecord) {
63         writeRecordToSocket(runRecord);
64     }
65 
66     @Override
processTestCaseStarted(TestRecord testCaseStartedRecord)67     public void processTestCaseStarted(TestRecord testCaseStartedRecord) {
68         writeRecordToSocket(testCaseStartedRecord);
69     }
70 
71     @Override
processTestCaseEnded(TestRecord testCaseRecord)72     public void processTestCaseEnded(TestRecord testCaseRecord) {
73         writeRecordToSocket(testCaseRecord);
74     }
75 
76     @Override
processFinalProto(TestRecord finalRecord)77     public void processFinalProto(TestRecord finalRecord) {
78         writeRecordToSocket(finalRecord);
79         StreamUtil.close(mReportSocket);
80     }
81 
writeRecordToSocket(TestRecord record)82     private void writeRecordToSocket(TestRecord record) {
83         if (mReportPort == null) {
84             CLog.d("No port set. Skipping the reporter.");
85         }
86         try {
87             if (mReportSocket == null) {
88                 mReportSocket = new Socket("localhost", mReportPort);
89             }
90             record.writeDelimitedTo(mReportSocket.getOutputStream());
91         } catch (IOException e) {
92             CLog.e(e);
93         }
94     }
95 }
96