blob: e94de3674a2a821fb3d2a0b62d9d40449d71d9c9 [file] [log] [blame]
paulhu72742952022-02-09 21:24:09 +08001/*
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#define LOG_TAG "MDnsEventReporter"
18
19#include "MDnsEventReporter.h"
20
Paul Hudae10af2022-06-01 20:03:35 +080021using android::IInterface;
22using android::sp;
paulhu72742952022-02-09 21:24:09 +080023using android::net::mdns::aidl::IMDnsEventListener;
24
25MDnsEventReporter& MDnsEventReporter::getInstance() {
26 // It should be initialized only once.
27 static MDnsEventReporter instance;
28 return instance;
29}
30
31const MDnsEventReporter::EventListenerSet& MDnsEventReporter::getEventListeners() const {
32 return getEventListenersImpl();
33}
34
Paul Hudae10af2022-06-01 20:03:35 +080035int MDnsEventReporter::addEventListener(const sp<IMDnsEventListener>& listener) {
paulhu72742952022-02-09 21:24:09 +080036 return addEventListenerImpl(listener);
37}
38
Paul Hudae10af2022-06-01 20:03:35 +080039int MDnsEventReporter::removeEventListener(const sp<IMDnsEventListener>& listener) {
paulhu72742952022-02-09 21:24:09 +080040 return removeEventListenerImpl(listener);
41}
42
43const MDnsEventReporter::EventListenerSet& MDnsEventReporter::getEventListenersImpl() const {
44 std::lock_guard lock(mMutex);
45 return mEventListeners;
46}
47
Paul Hudae10af2022-06-01 20:03:35 +080048int MDnsEventReporter::addEventListenerImpl(const sp<IMDnsEventListener>& listener) {
paulhu72742952022-02-09 21:24:09 +080049 if (listener == nullptr) {
50 ALOGE("The event listener should not be null");
51 return -EINVAL;
52 }
53
54 std::lock_guard lock(mMutex);
55
56 for (const auto& it : mEventListeners) {
Paul Hudae10af2022-06-01 20:03:35 +080057 if (IInterface::asBinder(it->getListener()).get() == IInterface::asBinder(listener).get()) {
paulhu72742952022-02-09 21:24:09 +080058 ALOGW("The event listener was already subscribed");
59 return -EEXIST;
60 }
61 }
62
Paul Hudae10af2022-06-01 20:03:35 +080063 auto eventListener = sp<EventListener>::make(this, listener);
64 IInterface::asBinder(listener)->linkToDeath(eventListener);
65 mEventListeners.insert(eventListener);
paulhu72742952022-02-09 21:24:09 +080066 return 0;
67}
68
Paul Hudae10af2022-06-01 20:03:35 +080069int MDnsEventReporter::removeEventListenerImpl(const sp<IMDnsEventListener>& listener) {
paulhu72742952022-02-09 21:24:09 +080070 if (listener == nullptr) {
71 ALOGE("The event listener should not be null");
72 return -EINVAL;
73 }
74
75 std::lock_guard lock(mMutex);
76
Paul Hudae10af2022-06-01 20:03:35 +080077 for (const auto& it : mEventListeners) {
78 const auto& binder = IInterface::asBinder(it->getListener());
79 if (binder == IInterface::asBinder(listener)) {
80 binder->unlinkToDeath(it);
81 mEventListeners.erase(it);
82 return 0;
83 }
84 }
85 ALOGE("The event listener does not exist");
86 return -ENOENT;
87}