summaryrefslogtreecommitdiff
path: root/system/gd/grpc/grpc_event_queue.h
blob: dc1c58870800e5fc71e68c9e5c8b7e4a31002cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <grpc++/grpc++.h>

#include <atomic>
#include <chrono>
#include <utility>

#include "blueberry/facade/common.pb.h"
#include "common/blocking_queue.h"
#include "os/log.h"

namespace bluetooth {
namespace grpc {

template <typename T>
class GrpcEventQueue {
 public:
  /**
   * Create a GrpcEventQueue that can be used to shuffle event from one thread to another
   * @param log_name
   */
  explicit GrpcEventQueue(std::string log_name) : log_name_(std::move(log_name)){};

  /**
   * Run the event loop and blocks until client cancels the stream request
   * Event queue will be cleared before entering the loop. Hence, only events occurred after gRPC request will be
   * delivered to the user. Hence user is advised to run the loop before generating pending events.
   *
   * @param context client context
   * @param writer output writer
   * @return gRPC status
   */
  ::grpc::Status RunLoop(::grpc::ServerContext* context, ::grpc::ServerWriter<T>* writer) {
    using namespace std::chrono_literals;
    LOG_INFO("%s: Entering Loop", log_name_.c_str());
    while (!context->IsCancelled()) {
      // Wait for 100 ms so that cancellation can be caught in amortized 50 ms latency
      if (pending_events_.wait_to_take(100ms)) {
        LOG_INFO("%s: Got event from queue", log_name_.c_str());
        writer->Write(pending_events_.take());
      }
    }
    running_ = false;
    LOG_INFO("%s: Exited Loop", log_name_.c_str());
    return ::grpc::Status::OK;
  }

  /**
   * Called when there is an incoming event
   * @param event incoming event
   */
  void OnIncomingEvent(T event) {
    if (!running_) {
      LOG_INFO("%s: Discarding an event while not running the loop", log_name_.c_str());
      return;
    }
    LOG_INFO("%s: Got event, enqueuing", log_name_.c_str());
    pending_events_.push(std::move(event));
  }

 private:
  std::string log_name_;
  std::atomic<bool> running_{true};
  common::BlockingQueue<T> pending_events_;
};

}  // namespace grpc
}  // namespace bluetooth