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