1/*
2 * Copyright (C) 2011 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 com.android.server.net;
18
19import android.net.NetworkIdentity;
20
21import java.io.DataInputStream;
22import java.io.DataOutputStream;
23import java.io.IOException;
24import java.net.ProtocolException;
25import java.util.HashSet;
26
27/**
28 * Identity of a {@code iface}, defined by the set of {@link NetworkIdentity}
29 * active on that interface.
30 *
31 * @hide
32 */
33public class NetworkIdentitySet extends HashSet<NetworkIdentity> {
34    private static final int VERSION_INIT = 1;
35    private static final int VERSION_ADD_ROAMING = 2;
36
37    public NetworkIdentitySet() {
38    }
39
40    public NetworkIdentitySet(DataInputStream in) throws IOException {
41        final int version = in.readInt();
42        switch (version) {
43            case VERSION_INIT: {
44                final int size = in.readInt();
45                for (int i = 0; i < size; i++) {
46                    final int ignoredVersion = in.readInt();
47                    final int type = in.readInt();
48                    final int subType = in.readInt();
49                    final String subscriberId = readOptionalString(in);
50                    add(new NetworkIdentity(type, subType, subscriberId, false));
51                }
52                break;
53            }
54            case VERSION_ADD_ROAMING: {
55                final int size = in.readInt();
56                for (int i = 0; i < size; i++) {
57                    final int type = in.readInt();
58                    final int subType = in.readInt();
59                    final String subscriberId = readOptionalString(in);
60                    final boolean roaming = in.readBoolean();
61                    add(new NetworkIdentity(type, subType, subscriberId, roaming));
62                }
63                break;
64            }
65            default: {
66                throw new ProtocolException("unexpected version: " + version);
67            }
68        }
69    }
70
71    public void writeToStream(DataOutputStream out) throws IOException {
72        out.writeInt(VERSION_ADD_ROAMING);
73        out.writeInt(size());
74        for (NetworkIdentity ident : this) {
75            out.writeInt(ident.getType());
76            out.writeInt(ident.getSubType());
77            writeOptionalString(out, ident.getSubscriberId());
78            out.writeBoolean(ident.getRoaming());
79        }
80    }
81
82    private static void writeOptionalString(DataOutputStream out, String value) throws IOException {
83        if (value != null) {
84            out.writeByte(1);
85            out.writeUTF(value);
86        } else {
87            out.writeByte(0);
88        }
89    }
90
91    private static String readOptionalString(DataInputStream in) throws IOException {
92        if (in.readByte() != 0) {
93            return in.readUTF();
94        } else {
95            return null;
96        }
97    }
98}
99