summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/tv/tunerresourcemanager/CasResource.java
blob: 4a81c95f0b8fa272ca9b9d6dcdc30db1cccaec54 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.tv.tunerresourcemanager;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * A Cas resource object used by the Tuner Resource Manager to record the cas
 * information.
 *
 * @hide
 */
public class CasResource {

    private final int mSystemId;

    private int mMaxSessionNum;

    private int mAvailableSessionNum;

    /**
     * The owner clients' ids when part of the Cas is occupied.
     */
    private Map<Integer, Integer> mOwnerClientIdsToSessionNum = new HashMap<>();

    CasResource(Builder builder) {
        this.mSystemId = builder.mSystemId;
        this.mMaxSessionNum = builder.mMaxSessionNum;
        this.mAvailableSessionNum = builder.mMaxSessionNum;
    }

    public int getSystemId() {
        return mSystemId;
    }

    public int getMaxSessionNum() {
        return mMaxSessionNum;
    }

    public int getUsedSessionNum() {
        return (mMaxSessionNum - mAvailableSessionNum);
    }

    public boolean isFullyUsed() {
        return mAvailableSessionNum == 0;
    }

    /**
     * Update max session number.
     *
     * @param maxSessionNum the new max session num.
     */
    public void updateMaxSessionNum(int maxSessionNum) {
        mAvailableSessionNum = Math.max(
                0, mAvailableSessionNum + (maxSessionNum - mMaxSessionNum));
        mMaxSessionNum = maxSessionNum;
    }

    /**
     * Set an owner for the cas
     *
     * @param ownerId the client id of the owner.
     */
    public void setOwner(int ownerId) {
        int sessionNum = mOwnerClientIdsToSessionNum.get(ownerId) == null
                ? 1 : (mOwnerClientIdsToSessionNum.get(ownerId) + 1);
        mOwnerClientIdsToSessionNum.put(ownerId, sessionNum);
        mAvailableSessionNum--;
    }

    /**
     * Remove an owner of the Cas.
     *
     * @param ownerId the removing client id of the owner.
     */
    public void removeOwner(int ownerId) {
        mAvailableSessionNum += mOwnerClientIdsToSessionNum.get(ownerId);
        mOwnerClientIdsToSessionNum.remove(ownerId);
    }

    public Set<Integer> getOwnerClientIds() {
        return mOwnerClientIdsToSessionNum.keySet();
    }

    @Override
    public String toString() {
        return "CasResource[systemId=" + this.mSystemId
                + ", isFullyUsed=" + (this.mAvailableSessionNum == 0)
                + ", maxSessionNum=" + this.mMaxSessionNum
                + ", ownerClients=" + ownersMapToString() + "]";
    }

    /**
     * Builder class for {@link CasResource}.
     */
    public static class Builder {

        private int mSystemId;
        protected int mMaxSessionNum;

        Builder(int systemId) {
            this.mSystemId = systemId;
        }

        /**
         * Builder for {@link CasResource}.
         *
         * @param maxSessionNum the max session num the current Cas has.
         */
        public Builder maxSessionNum(int maxSessionNum) {
            this.mMaxSessionNum = maxSessionNum;
            return this;
        }

        /**
         * Build a {@link CasResource}.
         *
         * @return {@link CasResource}.
         */
        public CasResource build() {
            CasResource cas = new CasResource(this);
            return cas;
        }
    }

    protected String ownersMapToString() {
        StringBuilder string = new StringBuilder("{");
        for (int clienId : mOwnerClientIdsToSessionNum.keySet()) {
            string.append(" clientId=")
                  .append(clienId)
                  .append(", owns session num=")
                  .append(mOwnerClientIdsToSessionNum.get(clienId))
                  .append(",");
        }
        return string.append("}").toString();
    }
}