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 #include "Observer.h"
18 
19 namespace android {
20 namespace hardware {
21 namespace media {
22 namespace bufferpool {
23 namespace V2_0 {
24 namespace implementation {
25 
Observer()26 Observer::Observer() {
27 }
28 
~Observer()29 Observer::~Observer() {
30 }
31 
32 // Methods from ::android::hardware::media::bufferpool::V2_0::IObserver follow.
onMessage(int64_t connectionId,uint32_t msgId)33 Return<void> Observer::onMessage(int64_t connectionId, uint32_t msgId) {
34     std::unique_lock<std::mutex> lock(mLock);
35     auto it = mClients.find(connectionId);
36     if (it != mClients.end()) {
37         const std::shared_ptr<BufferPoolClient> client = it->second.lock();
38         if (!client) {
39             mClients.erase(it);
40         } else {
41             lock.unlock();
42             client->receiveInvalidation(msgId);
43         }
44     }
45     return Void();
46 }
47 
addClient(ConnectionId connectionId,const std::weak_ptr<BufferPoolClient> & wclient)48 void Observer::addClient(ConnectionId connectionId,
49                          const std::weak_ptr<BufferPoolClient> &wclient) {
50     std::lock_guard<std::mutex> lock(mLock);
51     for (auto it = mClients.begin(); it != mClients.end();) {
52         if (!it->second.lock() || it->first == connectionId) {
53             it = mClients.erase(it);
54         } else {
55             ++it;
56         }
57     }
58     mClients.insert(std::make_pair(connectionId, wclient));
59 
60 }
61 
delClient(ConnectionId connectionId)62 void Observer::delClient(ConnectionId connectionId) {
63     std::lock_guard<std::mutex> lock(mLock);
64     mClients.erase(connectionId);
65 }
66 
67 
68 }  // namespace implementation
69 }  // namespace V2_0
70 }  // namespace bufferpool
71 }  // namespace media
72 }  // namespace hardware
73 }  // namespace android
74