|
| 1 | +// Copyright (c) ZeroC, Inc. |
| 2 | + |
| 3 | +#include "BidirWakeUpService.h" |
| 4 | + |
| 5 | +#include <algorithm> |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +using namespace EarlyRiser; |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +namespace |
| 12 | +{ |
| 13 | + // Converts a time stamp to a time point. |
| 14 | + chrono::system_clock::time_point toTimePoint(int64_t timeStamp) |
| 15 | + { |
| 16 | + const int daysBeforeEpoch = 719162; |
| 17 | + |
| 18 | + chrono::microseconds timePointMicro{timeStamp / 10}; // timeStamp is in ticks (100 nanoseconds) |
| 19 | + return chrono::system_clock::time_point{timePointMicro - daysBeforeEpoch * 24h}; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +Server::BidirWakeUpService::~BidirWakeUpService() |
| 24 | +{ |
| 25 | + // Waits for all outstanding tasks to complete. |
| 26 | + for (auto& task : _tasks) |
| 27 | + { |
| 28 | + task.wait(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void |
| 33 | +Server::BidirWakeUpService::wakeMeUp(int64_t timeStamp, const Ice::Current& current) |
| 34 | +{ |
| 35 | + // With C++20, we'll be able to print the time point easily--but not with C++17. |
| 36 | + cout << "Dispatching wakeMeUp request { timeStamp = " << timeStamp << " ticks }" << endl; |
| 37 | + |
| 38 | + chrono::system_clock::time_point timePoint = toTimePoint(timeStamp); |
| 39 | + |
| 40 | + Ice::ConnectionPtr connection = current.con; // The connection from the client to the server. |
| 41 | + if (!connection) |
| 42 | + { |
| 43 | + // Should never happen, but in case it does, the Ice runtime will send an Ice::UnknownException to the client. |
| 44 | + throw std::invalid_argument{"BidirWakeUpService does not support collocated calls"}; |
| 45 | + } |
| 46 | + |
| 47 | + // Create a proxy to the client's alarm clock. This connection-bound proxy is called a "fixed proxy". |
| 48 | + auto alarmClock = connection->createProxy<AlarmClockPrx>(Ice::stringToIdentity("alarmClock")); |
| 49 | + |
| 50 | + // Schedule a wake-up call in a background task. |
| 51 | + _tasks.emplace_back(std::async( |
| 52 | + std::launch::async, |
| 53 | + [alarmClock = std::move(alarmClock), timePoint]() |
| 54 | + { |
| 55 | + // Sleep until the specified time point. |
| 56 | + this_thread::sleep_until(timePoint); |
| 57 | + |
| 58 | + // First ring. |
| 59 | + ButtonPressed buttonPressed = alarmClock->ring("It's time to wake up!"); |
| 60 | + |
| 61 | + // Keep ringing every 10 seconds until the user presses the stop button. |
| 62 | + while (buttonPressed == ButtonPressed::Snooze) |
| 63 | + { |
| 64 | + this_thread::sleep_for(10s); |
| 65 | + buttonPressed = alarmClock->ring("No more snoozing!"); |
| 66 | + } |
| 67 | + })); |
| 68 | + |
| 69 | + // We don't want the _tasks vector to grow forever so remove all completed tasks here, without waiting. |
| 70 | + // TODO: switch to std::erase_if when we can use C++20. |
| 71 | + _tasks.erase( |
| 72 | + std::remove_if( |
| 73 | + _tasks.begin(), |
| 74 | + _tasks.end(), |
| 75 | + [](const auto& task) { return task.wait_for(std::chrono::seconds(0)) == std::future_status::ready; }), |
| 76 | + _tasks.end()); |
| 77 | +} |
0 commit comments