1 /*
2  * Copyright 2019 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 #ifndef ANDROID_AUTOMOTIVE_COMPUTEPIPE_ROUTER_PIPE_CONTEXT
17 #define ANDROID_AUTOMOTIVE_COMPUTEPIPE_ROUTER_PIPE_CONTEXT
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "ClientHandle.h"
24 #include "PipeHandle.h"
25 
26 namespace android {
27 namespace automotive {
28 namespace computepipe {
29 namespace router {
30 
31 /**
32  * This is the context of a registered pipe.
33  * It tracks assignments to clients and availability.
34  * It also owns the handle to the runner interface.
35  * This is utilized by the registry to track every registered pipe
36  */
37 template <typename T>
38 class PipeContext {
39   public:
40     // Check if associated runner is alive
isAlive()41     bool isAlive() const {
42         return mPipeHandle->isAlive();
43     }
44     // Retrieve the graph name
getGraphName()45     std::string getGraphName() const {
46         return mGraphName;
47     }
48     /**
49      * Check if its available for clients
50      *
51      * If no client is assigned mClientHandle is null.
52      * If a client is assigned use it to determine if its still alive,
53      * If its not then the runner is available
54      */
isAvailable()55     bool isAvailable() {
56         if (!mClientHandle) {
57             return true;
58         }
59         if (!mClientHandle->isAlive()) {
60             mClientHandle = nullptr;
61             return true;
62         }
63         return false;
64     }
65     // Mark availability. True if available
setClient(std::unique_ptr<ClientHandle> clientHandle)66     void setClient(std::unique_ptr<ClientHandle> clientHandle) {
67         mClientHandle.reset(clientHandle.release());
68     }
69     // Set the name of the graph
setGraphName(std::string name)70     void setGraphName(std::string name) {
71         mGraphName = name;
72     }
73     // Duplicate the pipehandle for retrieval by clients.
dupPipeHandle()74     std::unique_ptr<PipeHandle<T>> dupPipeHandle() {
75         return std::unique_ptr<PipeHandle<T>>(mPipeHandle->clone());
76     }
77     // Setup pipecontext
PipeContext(std::unique_ptr<PipeHandle<T>> h,std::string name)78     PipeContext(std::unique_ptr<PipeHandle<T>> h, std::string name)
79         : mGraphName(name), mPipeHandle(std::move(h)) {
80     }
~PipeContext()81     ~PipeContext() {
82         if (mPipeHandle) {
83             mPipeHandle = nullptr;
84         }
85         if (mClientHandle) {
86             mClientHandle = nullptr;
87         }
88     }
89 
90   private:
91     std::string mGraphName;
92     std::unique_ptr<PipeHandle<T>> mPipeHandle;
93     std::unique_ptr<ClientHandle> mClientHandle;
94     bool hasClient;
95 };
96 
97 }  // namespace router
98 }  // namespace computepipe
99 }  // namespace automotive
100 }  // namespace android
101 #endif
102