1 /*
2  * Copyright (C) 2021 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 
17 package com.android.car.telemetry.databroker;
18 
19 import android.os.PersistableBundle;
20 import android.os.SystemClock;
21 
22 import com.android.car.telemetry.TelemetryProto;
23 
24 import java.util.Objects;
25 
26 /**
27  * Subscriber class that receives published data and schedules tasks for execution.
28  * All methods of this class must be accessed on telemetry thread.
29  */
30 public class DataSubscriber {
31 
32     private final DataBroker mDataBroker;
33     private final TelemetryProto.MetricsConfig mMetricsConfig;
34     private final TelemetryProto.Subscriber mSubscriber;
35 
DataSubscriber( DataBroker dataBroker, TelemetryProto.MetricsConfig metricsConfig, TelemetryProto.Subscriber subscriber)36     public DataSubscriber(
37             DataBroker dataBroker,
38             TelemetryProto.MetricsConfig metricsConfig,
39             TelemetryProto.Subscriber subscriber) {
40         mDataBroker = dataBroker;
41         mMetricsConfig = metricsConfig;
42         mSubscriber = subscriber;
43     }
44 
45     /** Returns the handler function name for this subscriber. */
getHandlerName()46     public String getHandlerName() {
47         return mSubscriber.getHandler();
48     }
49 
50     /**
51      * Returns the publisher param {@link TelemetryProto.Publisher} that
52      * contains the data source and the config.
53      */
getPublisherParam()54     public TelemetryProto.Publisher getPublisherParam() {
55         return mSubscriber.getPublisher();
56     }
57 
58     /**
59      * Creates a {@link ScriptExecutionTask} and pushes it to the priority queue where the task
60      * will be pending execution. Flag isLargeData indicates whether data is large.
61      */
push(PersistableBundle data, boolean isLargeData)62     public void push(PersistableBundle data, boolean isLargeData) {
63         ScriptExecutionTask task = new ScriptExecutionTask(
64                 this, data, SystemClock.elapsedRealtime(), isLargeData);
65         mDataBroker.addTaskToQueue(task);
66     }
67 
68     /**
69      * Creates a {@link ScriptExecutionTask} and pushes it to the priority queue where the task
70      * will be pending execution. Defaults isLargeData flag to false.
71      */
push(PersistableBundle data)72     public void push(PersistableBundle data) {
73         push(data, false);
74     }
75 
76     /** Returns the {@link TelemetryProto.MetricsConfig}. */
getMetricsConfig()77     public TelemetryProto.MetricsConfig getMetricsConfig() {
78         return mMetricsConfig;
79     }
80 
81     /** Returns the {@link TelemetryProto.Subscriber}. */
getSubscriber()82     public TelemetryProto.Subscriber getSubscriber() {
83         return mSubscriber;
84     }
85 
86     /** Returns the priority of subscriber. */
getPriority()87     public int getPriority() {
88         return mSubscriber.getPriority();
89     }
90 
91     @Override
equals(Object o)92     public boolean equals(Object o) {
93         if (!(o instanceof DataSubscriber)) {
94             return false;
95         }
96         DataSubscriber other = (DataSubscriber) o;
97         return mMetricsConfig.getName().equals(other.getMetricsConfig().getName())
98                 && mMetricsConfig.getVersion() == other.getMetricsConfig().getVersion()
99                 && mSubscriber.getHandler().equals(other.getSubscriber().getHandler());
100     }
101 
102     @Override
hashCode()103     public int hashCode() {
104         return Objects.hash(mMetricsConfig.getName(), mMetricsConfig.getVersion(),
105                 mSubscriber.getHandler());
106     }
107 }
108