blob: 87598830b6024c7266bdf8ef2aaa2af323365df9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package com.android.tv.mdnsoffloadmanager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@WorkerThread
public class InterfaceOffloadManager {
private static final String TAG = InterfaceOffloadManager.class.getSimpleName();
private final String mNetworkInterface;
private final OffloadIntentStore mOffloadIntentStore;
private final OffloadWriter mOffloadWriter;
private final Set<Integer> mCurrentOffloadKeys = new HashSet<>();
private final Set<String> mCurrentPassthroughQNames = new HashSet<>();
private boolean mIsNetworkAvailable = false;
InterfaceOffloadManager(
@NonNull String networkInterface,
@NonNull OffloadIntentStore offloadIntentStore,
@NonNull OffloadWriter offloadWriter) {
mNetworkInterface = networkInterface;
mOffloadIntentStore = offloadIntentStore;
mOffloadWriter = offloadWriter;
}
void onVendorServiceConnected() {
refreshProtocolResponses();
refreshPassthroughList();
}
void onAppIdAllowlistUpdated() {
refreshProtocolResponses();
refreshPassthroughList();
}
void onNetworkAvailable() {
String msg = "Network interface {" + mNetworkInterface + "} is connected." +
" Offloading all stored data.";
Log.d(TAG, msg);
mIsNetworkAvailable = true;
refreshProtocolResponses();
refreshPassthroughList();
}
void onNetworkLost() {
String msg = "Network interface {" + mNetworkInterface + "} was disconnected."
+ " Clearing all associated data.";
Log.d(TAG, msg);
mIsNetworkAvailable = false;
clearProtocolResponses();
clearPassthroughList();
}
void onVendorServiceDisconnected() {
mCurrentOffloadKeys.clear();
mCurrentPassthroughQNames.clear();
}
void refreshProtocolResponses() {
if (!mIsNetworkAvailable) {
return;
}
applyOffloadIntents(mOffloadIntentStore.getOffloadIntentsForInterface(mNetworkInterface));
}
void refreshPassthroughList() {
if (!mIsNetworkAvailable) {
return;
}
applyPassthroughIntents(
mOffloadIntentStore.getPassthroughIntentsForInterface(mNetworkInterface));
}
private void clearProtocolResponses() {
applyOffloadIntents(Collections.emptySet());
}
private void clearPassthroughList() {
applyPassthroughIntents(Collections.emptyList());
}
private void applyOffloadIntents(Collection<OffloadIntentStore.OffloadIntent> offloadIntents) {
if (!mOffloadWriter.isVendorServiceConnected()) {
Log.e(TAG, "Vendor service disconnected, cannot apply mDNS offload state");
return;
}
Collection<Integer> deleted = mOffloadWriter.deleteOffloadData(mCurrentOffloadKeys);
mCurrentOffloadKeys.removeAll(deleted);
Collection<Integer> offloaded = mOffloadWriter.writeOffloadData(
mNetworkInterface, offloadIntents);
mCurrentOffloadKeys.addAll(offloaded);
}
private void applyPassthroughIntents(
List<OffloadIntentStore.PassthroughIntent> passthroughIntents) {
if (!mOffloadWriter.isVendorServiceConnected()){
Log.e(TAG, "Vendor service disconnected, cannot apply mDNS passthrough state");
return;
}
Collection<String> deleted = mOffloadWriter.deletePassthroughData(
mNetworkInterface, mCurrentPassthroughQNames);
mCurrentPassthroughQNames.removeAll(deleted);
Collection<String> added = mOffloadWriter.writePassthroughData(
mNetworkInterface, passthroughIntents);
mCurrentPassthroughQNames.addAll(added);
}
@WorkerThread
void dump(PrintWriter writer) {
writer.println("InterfaceOffloadManager[%s]:".formatted(mNetworkInterface));
writer.println("mIsNetworkAvailable=%b".formatted(mIsNetworkAvailable));
writer.println("current offload keys:");
mCurrentOffloadKeys.forEach(key -> writer.println("* %d".formatted(key)));
writer.println("current passthrough qnames:");
mCurrentPassthroughQNames.forEach(qname -> writer.println("* %s".formatted(qname)));
writer.println();
}
}
|