Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef NETD_SERVER_EVENT_REPORTER_H |
| 18 | #define NETD_SERVER_EVENT_REPORTER_H |
| 19 | |
| 20 | #include <atomic> |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame^] | 21 | #include <map> |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 22 | #include <mutex> |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 23 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame^] | 24 | #include <android-base/thread_annotations.h> |
| 25 | #include <binder/IServiceManager.h> |
| 26 | #include "android/net/INetd.h" |
| 27 | #include "android/net/INetdUnsolicitedEventListener.h" |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 28 | #include "android/net/metrics/INetdEventListener.h" |
| 29 | |
| 30 | /* |
Bernie Innocenti | cd25764 | 2018-12-20 15:56:40 +0900 | [diff] [blame] | 31 | * This class can be used to get the event listener service. |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 32 | */ |
| 33 | class EventReporter { |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame^] | 34 | public: |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 35 | // Returns the binder reference to the netd events listener service, attempting to fetch it if |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 36 | // we do not have it already. This method is threadsafe. |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 37 | android::sp<android::net::metrics::INetdEventListener> getNetdEventListener(); |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame^] | 38 | using UnsolListenerMap = |
| 39 | std::map<pid_t, const android::sp<android::net::INetdUnsolicitedEventListener>>; |
| 40 | UnsolListenerMap getNetdUnsolicitedEventListenerVec() EXCLUDES(mUnsolicitedMutex); |
| 41 | void registerUnsolEventListener( |
| 42 | pid_t pid, const android::sp<android::net::INetdUnsolicitedEventListener>& listener) |
| 43 | EXCLUDES(mUnsolicitedMutex); |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 44 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame^] | 45 | private: |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 46 | // TODO: consider changing this into an atomic type such as |
| 47 | // std::atomic<android::net::metrics::INetdEventListener> and deleting the mutex. |
| 48 | // |
| 49 | // Alternatively, if this locking causes a performance penalty, have each single-threaded |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 50 | // caller (FwmarkServer) keep their own per-thread copy of NetdEventListener |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 51 | // and remove mNetdEventListener entirely. |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame^] | 52 | std::mutex mEventMutex; |
| 53 | std::mutex mUnsolicitedMutex; |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 54 | android::sp<android::net::metrics::INetdEventListener> mNetdEventListener; |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame^] | 55 | UnsolListenerMap mNetdUnsolicitedEventListenerMap GUARDED_BY(mUnsolicitedMutex); |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | #endif // NETD_SERVER_EVENT_REPORTER_H |