|
| 1 | +// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 2 | + |
| 3 | +#include "moxygen/moqtest/MoQTestClient.h" |
| 4 | +#include "folly/coro/BlockingWait.h" |
| 5 | +#include "folly/init/Init.h" |
| 6 | +#include "folly/io/async/ScopedEventBaseThread.h" |
| 7 | +#include "moxygen/MoQClient.h" |
| 8 | +#include "moxygen/moqtest/Utils.h" |
| 9 | + |
| 10 | +namespace moxygen { |
| 11 | + |
| 12 | +DEFINE_string(url, "http://localhost:9999", "URL to connect to"); |
| 13 | +DEFINE_int32(connect_timeout, 1000, "connect timeout in ms"); |
| 14 | +DEFINE_int32(transaction_timeout, 1000, "transaction timeout in ms"); |
| 15 | +const int kDefaultRequestId = 0; |
| 16 | +const std::string kDefaultTrackName = "test"; |
| 17 | +const GroupOrder kDefaultGroupOrder = GroupOrder(0x0); |
| 18 | +const LocationType kDefaultLocationType = LocationType(1); |
| 19 | +const uint64_t kDefaultEndGroup = 128; |
| 20 | + |
| 21 | +MoQTestClient::MoQTestClient(folly::EventBase* evb, proxygen::URL url) |
| 22 | + : moqClient_(std::make_unique<MoQClient>(evb, std::move(url))) {} |
| 23 | + |
| 24 | +folly::coro::Task<void> MoQTestClient::connect(folly::EventBase* evb) { |
| 25 | + co_await moqClient_->setupMoQSession( |
| 26 | + std::chrono::milliseconds(FLAGS_connect_timeout), |
| 27 | + std::chrono::seconds(FLAGS_transaction_timeout), |
| 28 | + nullptr, |
| 29 | + shared_from_this()); |
| 30 | + co_return; |
| 31 | +} |
| 32 | + |
| 33 | +void MoQTestClient::initialize() { |
| 34 | + // Create a receiver for the client |
| 35 | + subReceiver_ = std::make_shared<ObjectReceiver>( |
| 36 | + ObjectReceiver::SUBSCRIBE, |
| 37 | + std::shared_ptr<MoQTestClient>(shared_from_this())); |
| 38 | + fetchReceiver_ = std::make_shared<ObjectReceiver>( |
| 39 | + ObjectReceiver::FETCH, |
| 40 | + std::shared_ptr<MoQTestClient>(shared_from_this())); |
| 41 | +} |
| 42 | + |
| 43 | +folly::coro::Task<moxygen::TrackNamespace> MoQTestClient::subscribe( |
| 44 | + MoQTestParameters params) { |
| 45 | + auto trackNamespace = convertMoqTestParamToTrackNamespace(¶ms); |
| 46 | + |
| 47 | + // Create a SubRequest with the created TrackNamespace as its fullTrackName |
| 48 | + SubscribeRequest sub; |
| 49 | + sub.requestID = kDefaultRequestId; |
| 50 | + |
| 51 | + FullTrackName ftn; |
| 52 | + ftn.trackNamespace = trackNamespace.value(); |
| 53 | + ftn.trackName = kDefaultTrackName; |
| 54 | + |
| 55 | + sub.fullTrackName = ftn; |
| 56 | + sub.trackAlias = TrackAlias(); |
| 57 | + sub.groupOrder = kDefaultGroupOrder; |
| 58 | + sub.locType = kDefaultLocationType; |
| 59 | + sub.endGroup = kDefaultEndGroup; |
| 60 | + |
| 61 | + // Subscribe to the reciever |
| 62 | + auto res = co_await moqClient_->moqSession_->subscribe(sub, subReceiver_); |
| 63 | + |
| 64 | + co_return trackNamespace.value(); |
| 65 | +} |
| 66 | + |
| 67 | +folly::coro::Task<moxygen::TrackNamespace> MoQTestClient::fetch( |
| 68 | + MoQTestParameters params) { |
| 69 | + auto trackNamespace = convertMoqTestParamToTrackNamespace(¶ms); |
| 70 | + |
| 71 | + // Create a Fetch with the created TrackNamespace as its fullTrackName |
| 72 | + Fetch fetch; |
| 73 | + fetch.requestID = kDefaultRequestId; |
| 74 | + |
| 75 | + FullTrackName ftn; |
| 76 | + ftn.trackNamespace = trackNamespace.value(); |
| 77 | + ftn.trackName = kDefaultTrackName; |
| 78 | + fetch.fullTrackName = ftn; |
| 79 | + fetch.groupOrder = kDefaultGroupOrder; |
| 80 | + |
| 81 | + // Fetch to the reciever |
| 82 | + auto res = co_await moqClient_->moqSession_->fetch(fetch, fetchReceiver_); |
| 83 | + |
| 84 | + co_return trackNamespace.value(); |
| 85 | +} |
| 86 | + |
| 87 | +ObjectReceiverCallback::FlowControlState MoQTestClient::onObject( |
| 88 | + const ObjectHeader& objHeader, |
| 89 | + Payload payload) { |
| 90 | + std::cout << "onObject" << std::endl; |
| 91 | + return ObjectReceiverCallback::FlowControlState::BLOCKED; |
| 92 | +} |
| 93 | + |
| 94 | +void MoQTestClient::onObjectStatus(const ObjectHeader& objHeader) { |
| 95 | + std::cout << "onObjectStatus" << std::endl; |
| 96 | +} |
| 97 | + |
| 98 | +void MoQTestClient::onEndOfStream() { |
| 99 | + std::cout << "onEndOfStream" << std::endl; |
| 100 | +} |
| 101 | + |
| 102 | +void MoQTestClient::onError(ResetStreamErrorCode) { |
| 103 | + std::cout << "onError" << std::endl; |
| 104 | +} |
| 105 | +void MoQTestClient::onSubscribeDone(SubscribeDone done) { |
| 106 | + std::cout << "onSubscribeDone" << std::endl; |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace moxygen |
| 110 | + |
| 111 | +int main(int argc, char** argv) { |
| 112 | + gflags::ParseCommandLineFlags(&argc, &argv, false); |
| 113 | + folly::Init init(&argc, &argv); |
| 114 | + |
| 115 | + folly::ScopedEventBaseThread evb; |
| 116 | + |
| 117 | + // Initialize Client with url |
| 118 | + moxygen::MoQTestParameters kDefaultMoqParams; |
| 119 | + auto url = proxygen::URL(moxygen::FLAGS_url); |
| 120 | + std::shared_ptr<moxygen::MoQTestClient> client = |
| 121 | + std::make_shared<moxygen::MoQTestClient>(evb.getEventBase(), url); |
| 122 | + client->initialize(); |
| 123 | + |
| 124 | + // Connect Client to Server |
| 125 | + folly::coro::blockingWait( |
| 126 | + client->connect(evb.getEventBase()).scheduleOn(evb.getEventBase())); |
| 127 | + |
| 128 | + // Test a Subscribe Call |
| 129 | + folly::coro::blockingWait( |
| 130 | + client->subscribe(kDefaultMoqParams).scheduleOn(evb.getEventBase())); |
| 131 | + |
| 132 | + sleep(10); |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
0 commit comments