1 /* 2 * Copyright 2020 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 package com.android.server.tv.tunerresourcemanager; 17 18 import java.util.HashMap; 19 import java.util.Map; 20 import java.util.Set; 21 22 /** 23 * A Cas resource object used by the Tuner Resource Manager to record the cas 24 * information. 25 * 26 * @hide 27 */ 28 public class CasResource { 29 30 private final int mSystemId; 31 32 private int mMaxSessionNum; 33 34 private int mAvailableSessionNum; 35 36 /** 37 * The owner clients' ids when part of the Cas is occupied. 38 */ 39 private Map<Integer, Integer> mOwnerClientIdsToSessionNum = new HashMap<>(); 40 CasResource(Builder builder)41 CasResource(Builder builder) { 42 this.mSystemId = builder.mSystemId; 43 this.mMaxSessionNum = builder.mMaxSessionNum; 44 this.mAvailableSessionNum = builder.mMaxSessionNum; 45 } 46 getSystemId()47 public int getSystemId() { 48 return mSystemId; 49 } 50 getMaxSessionNum()51 public int getMaxSessionNum() { 52 return mMaxSessionNum; 53 } 54 getUsedSessionNum()55 public int getUsedSessionNum() { 56 return (mMaxSessionNum - mAvailableSessionNum); 57 } 58 isFullyUsed()59 public boolean isFullyUsed() { 60 return mAvailableSessionNum == 0; 61 } 62 63 /** 64 * Update max session number. 65 * 66 * @param maxSessionNum the new max session num. 67 */ updateMaxSessionNum(int maxSessionNum)68 public void updateMaxSessionNum(int maxSessionNum) { 69 mAvailableSessionNum = Math.max( 70 0, mAvailableSessionNum + (maxSessionNum - mMaxSessionNum)); 71 mMaxSessionNum = maxSessionNum; 72 } 73 74 /** 75 * Set an owner for the cas 76 * 77 * @param ownerId the client id of the owner. 78 */ setOwner(int ownerId)79 public void setOwner(int ownerId) { 80 int sessionNum = mOwnerClientIdsToSessionNum.get(ownerId) == null 81 ? 1 : (mOwnerClientIdsToSessionNum.get(ownerId) + 1); 82 mOwnerClientIdsToSessionNum.put(ownerId, sessionNum); 83 mAvailableSessionNum--; 84 } 85 86 /** 87 * Remove an owner of the Cas. 88 * 89 * @param ownerId the removing client id of the owner. 90 */ removeOwner(int ownerId)91 public void removeOwner(int ownerId) { 92 mAvailableSessionNum += mOwnerClientIdsToSessionNum.get(ownerId); 93 mOwnerClientIdsToSessionNum.remove(ownerId); 94 } 95 getOwnerClientIds()96 public Set<Integer> getOwnerClientIds() { 97 return mOwnerClientIdsToSessionNum.keySet(); 98 } 99 100 @Override toString()101 public String toString() { 102 return "CasResource[systemId=" + this.mSystemId 103 + ", isFullyUsed=" + (this.mAvailableSessionNum == 0) 104 + ", maxSessionNum=" + this.mMaxSessionNum 105 + ", ownerClients=" + ownersMapToString() + "]"; 106 } 107 108 /** 109 * Builder class for {@link CasResource}. 110 */ 111 public static class Builder { 112 113 private int mSystemId; 114 protected int mMaxSessionNum; 115 Builder(int systemId)116 Builder(int systemId) { 117 this.mSystemId = systemId; 118 } 119 120 /** 121 * Builder for {@link CasResource}. 122 * 123 * @param maxSessionNum the max session num the current Cas has. 124 */ maxSessionNum(int maxSessionNum)125 public Builder maxSessionNum(int maxSessionNum) { 126 this.mMaxSessionNum = maxSessionNum; 127 return this; 128 } 129 130 /** 131 * Build a {@link CasResource}. 132 * 133 * @return {@link CasResource}. 134 */ build()135 public CasResource build() { 136 CasResource cas = new CasResource(this); 137 return cas; 138 } 139 } 140 ownersMapToString()141 protected String ownersMapToString() { 142 StringBuilder string = new StringBuilder("{"); 143 for (int clienId : mOwnerClientIdsToSessionNum.keySet()) { 144 string.append(" clientId=") 145 .append(clienId) 146 .append(", owns session num=") 147 .append(mOwnerClientIdsToSessionNum.get(clienId)) 148 .append(","); 149 } 150 return string.append("}").toString(); 151 } 152 } 153