DnsEvent.java revision 95cb226c1a4ff47531da65ef2617ade0dea5c9b8
1/*
2 * Copyright (C) 2016 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
17package android.net.metrics;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * A DNS event recorded by NetdEventListenerService.
24 * {@hide}
25 */
26final public class DnsEvent implements Parcelable {
27    public final int netId;
28
29    // The event type is currently only 1 or 2, so we store it as a byte.
30    public final byte[] eventTypes;
31    // Current getaddrinfo codes go from 1 to EAI_MAX = 15. gethostbyname returns errno, but there
32    // are fewer than 255 errno values. So we store the result code in a byte as well.
33    public final byte[] returnCodes;
34    // The latency is an integer because a) short arrays aren't parcelable and b) a short can only
35    // store a maximum latency of 32757 or 65535 ms, which is too short for pathologically slow
36    // queries.
37    public final int[] latenciesMs;
38
39    public DnsEvent(int netId, byte[] eventTypes, byte[] returnCodes, int[] latenciesMs) {
40        this.netId = netId;
41        this.eventTypes = eventTypes;
42        this.returnCodes = returnCodes;
43        this.latenciesMs = latenciesMs;
44    }
45
46    private DnsEvent(Parcel in) {
47        this.netId = in.readInt();
48        this.eventTypes = in.createByteArray();
49        this.returnCodes = in.createByteArray();
50        this.latenciesMs = in.createIntArray();
51    }
52
53    @Override
54    public void writeToParcel(Parcel out, int flags) {
55        out.writeInt(netId);
56        out.writeByteArray(eventTypes);
57        out.writeByteArray(returnCodes);
58        out.writeIntArray(latenciesMs);
59    }
60
61    @Override
62    public int describeContents() {
63        return 0;
64    }
65
66    @Override
67    public String toString() {
68        return String.format("DnsEvent(%d, %d events)", netId, eventTypes.length);
69    }
70
71    public static final Parcelable.Creator<DnsEvent> CREATOR = new Parcelable.Creator<DnsEvent>() {
72        @Override
73        public DnsEvent createFromParcel(Parcel in) {
74            return new DnsEvent(in);
75        }
76
77        @Override
78        public DnsEvent[] newArray(int size) {
79            return new DnsEvent[size];
80        }
81    };
82}
83