File: message_pipe.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (89 lines) | stat: -rw-r--r-- 2,475 bytes parent folder | download | duplicates (6)
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
86
87
88
89
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// The testing message pipe is intended to be used for integrating
// sender and receiver sessions (or other consumers of MessagePort).

#ifndef CAST_STREAMING_TESTING_MESSAGE_PIPE_H_
#define CAST_STREAMING_TESTING_MESSAGE_PIPE_H_

#include <string>
#include <utility>
#include <vector>

#include "cast/common/public/message_port.h"
#include "cast/streaming/message_fields.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace openscreen {
namespace cast {

class MessagePipeEnd : public MessagePort {
 public:
  explicit MessagePipeEnd(std::string destination_id)
      : destination_id_(destination_id) {}

  ~MessagePipeEnd() override = default;

  void SetClient(MessagePort::Client* client,
                 std::string client_sender_id) override {
    sender_id_ = std::move(client_sender_id);
    client_ = client;
  }

  void SetOtherEnd(MessagePipeEnd* other_end) {
    ASSERT_FALSE(other_end_);
    other_end_ = other_end;
  }

  void ResetClient() override { client_ = nullptr; }

  void ReceiveMessage(const std::string& namespace_,
                      const std::string& message) {
    ASSERT_NE(client_, nullptr);
    client_->OnMessage(destination_id_, namespace_, message);
  }

  void PostMessage(const std::string& sender_id,
                   const std::string& message_namespace,
                   const std::string& message) override {
    ASSERT_NE(other_end_, nullptr);
    other_end_->ReceiveMessage(message_namespace, message);
  }

 private:
  std::string sender_id_;
  std::string destination_id_;
  MessagePort::Client* client_ = nullptr;
  MessagePipeEnd* other_end_ = nullptr;
};

class MessagePipe {
 public:
  explicit MessagePipe(std::string left_id, std::string right_id)
      : left_id_(std::move(right_id)),
        right_id_(std::move(left_id)),
        left_(left_id_),
        right_(right_id_) {
    left_.SetOtherEnd(&right_);
    right_.SetOtherEnd(&left_);
  }

  // Access the ends of the pipe, which can be used as a standard
  // message port.
  MessagePipeEnd* left() { return &left_; }
  MessagePipeEnd* right() { return &right_; }

 private:
  std::string left_id_;
  std::string right_id_;
  MessagePipeEnd left_;
  MessagePipeEnd right_;
};

}  // namespace cast
}  // namespace openscreen

#endif  // CAST_STREAMING_TESTING_MESSAGE_PIPE_H_