1 # 2 # Copyright (C) 2018 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 17 import multiprocessing 18 19 from host_controller import common 20 21 22 class SharedDict(object): 23 """Class for the state of devices connected to the host. 24 25 shared between the main process and the process pool. 26 27 Attributes: 28 _dict: multiprocessing.SyncManager.dict. A dictionary 29 shared among processes. 30 """ 31 32 def __init__(self): 33 manager = multiprocessing.Manager() 34 self._dict = manager.dict() 35 36 def __getitem__(self, key): 37 """getitem for self._dict. 38 39 Args: 40 key: string, serial number of a device. 41 42 Returns: 43 integer value defined in _DEVICE_STATUS_DICT. 44 """ 45 if key not in self._dict: 46 self._dict[key] = common._DEVICE_STATUS_DICT["unknown"] 47 return self._dict[key] 48 49 def __setitem__(self, key, value): 50 """setitem for self._dict. 51 52 if the value is not a defined one, device status is set to "unknown". 53 54 Args: 55 key: string, serial number of a device. 56 value: integer, status value defined in _DEVICE_STATUS_DICT. 57 """ 58 if value not in range(len(common._DEVICE_STATUS_DICT)): 59 self._dict[key] = common._DEVICE_STATUS_DICT["unknown"] 60 else: 61 self._dict[key] = value