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 
17 #pragma once
18 
19 #include <grpc++/grpc++.h>
20 
21 #include <atomic>
22 #include <chrono>
23 #include <utility>
24 
25 #include "common/blocking_queue.h"
26 #include "facade/common.pb.h"
27 #include "os/log.h"
28 
29 namespace bluetooth {
30 namespace grpc {
31 
32 template <typename T>
33 class GrpcEventQueue {
34  public:
35   /**
36    * Create a GrpcEventQueue that can be used to shuffle event from one thread to another
37    * @param log_name
38    */
GrpcEventQueue(std::string log_name)39   explicit GrpcEventQueue(std::string log_name) : log_name_(std::move(log_name)){};
40 
41   /**
42    * Run the event loop and blocks until client cancels the stream request
43    * Event queue will be cleared before entering the loop. Hence, only events occurred after gRPC request will be
44    * delivered to the user. Hence user is advised to run the loop before generating pending events.
45    *
46    * @param context client context
47    * @param writer output writer
48    * @return gRPC status
49    */
RunLoop(::grpc::ServerContext * context,::grpc::ServerWriter<T> * writer)50   ::grpc::Status RunLoop(::grpc::ServerContext* context, ::grpc::ServerWriter<T>* writer) {
51     using namespace std::chrono_literals;
52     LOG_INFO("%s: Entering Loop", log_name_.c_str());
53     pending_events_.clear();
54     running_ = true;
55     while (!context->IsCancelled()) {
56       // Wait for 500 ms so that cancellation can be caught in amortized 250 ms latency
57       if (pending_events_.wait_to_take(500ms)) {
58         LOG_DEBUG("%s: Got event after queue", log_name_.c_str());
59         writer->Write(pending_events_.take());
60       }
61     }
62     running_ = false;
63     LOG_INFO("%s: Exited Loop", log_name_.c_str());
64     return ::grpc::Status::OK;
65   }
66 
67   /**
68    * Called when there is an incoming event
69    * @param event incoming event
70    */
OnIncomingEvent(T event)71   void OnIncomingEvent(T event) {
72     if (!running_) {
73       LOG_DEBUG("%s: Discarding an event while not running the loop", log_name_.c_str());
74       return;
75     }
76     LOG_DEBUG("%s: Got event before queue", log_name_.c_str());
77     pending_events_.push(std::move(event));
78   }
79 
80  private:
81   std::string log_name_;
82   std::atomic<bool> running_ = false;
83   common::BlockingQueue<T> pending_events_;
84 };
85 
86 }  // namespace grpc
87 }  // namespace bluetooth
88