1 /*
2  * Copyright (C) 2022 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 #include "BufferPoolClient.h"
19 
20 namespace aidl::android::hardware::media::bufferpool2::implementation {
21 
Observer()22 Observer::Observer() {
23 }
24 
~Observer()25 Observer::~Observer() {
26 }
27 
onMessage(int64_t in_connectionId,int32_t in_msgId)28 ::ndk::ScopedAStatus Observer::onMessage(int64_t in_connectionId, int32_t in_msgId) {
29     std::unique_lock<std::mutex> lock(mLock);
30     auto it = mClients.find(in_connectionId);
31     if (it != mClients.end()) {
32         const std::shared_ptr<BufferPoolClient> client = it->second.lock();
33         if (!client) {
34             mClients.erase(it);
35         } else {
36             lock.unlock();
37             client->receiveInvalidation(in_msgId);
38         }
39     }
40     return ::ndk::ScopedAStatus::ok();
41 }
42 
addClient(ConnectionId connectionId,const std::weak_ptr<BufferPoolClient> & wclient)43 void Observer::addClient(ConnectionId connectionId,
44                          const std::weak_ptr<BufferPoolClient> &wclient) {
45     std::lock_guard<std::mutex> lock(mLock);
46     for (auto it = mClients.begin(); it != mClients.end();) {
47         if (!it->second.lock() || it->first == connectionId) {
48             it = mClients.erase(it);
49         } else {
50             ++it;
51         }
52     }
53     mClients.insert(std::make_pair(connectionId, wclient));
54 
55 }
56 
delClient(ConnectionId connectionId)57 void Observer::delClient(ConnectionId connectionId) {
58     std::lock_guard<std::mutex> lock(mLock);
59     mClients.erase(connectionId);
60 }
61 
62 
63 }  // namespace aidl::android::hardware::media::bufferpool2::implementation
64