|
1 | 1 | // Copyright (c) ZeroC, Inc. |
2 | 2 |
|
3 | | -#include "Callback.h" |
4 | | -#include <Ice/Ice.h> |
| 3 | +#include "MockAlarmClock.h" |
| 4 | + |
5 | 5 | #include <iostream> |
6 | 6 |
|
| 7 | +using namespace EarlyRiser; |
7 | 8 | using namespace std; |
8 | | -using namespace Demo; |
9 | | - |
10 | | -class CallbackReceiverI final : public CallbackReceiver |
11 | | -{ |
12 | | -public: |
13 | | - void callback(const Ice::Current&) final { cout << "received callback" << endl; } |
14 | | -}; |
15 | 9 |
|
16 | | -int run(const shared_ptr<Ice::Communicator>&); |
17 | | - |
18 | | -int |
19 | | -main(int argc, char* argv[]) |
| 10 | +namespace |
20 | 11 | { |
21 | | - int status = 0; |
22 | | - |
23 | | - try |
| 12 | + // Converts a time point to a time stamp. |
| 13 | + int64_t toTimeStamp(const chrono::system_clock::time_point& timePoint) |
24 | 14 | { |
25 | | - // |
26 | | - // CommunicatorHolder's ctor initializes an Ice communicator, |
27 | | - // and its dtor destroys this communicator. |
28 | | - // |
29 | | - const Ice::CommunicatorHolder ich(argc, argv, "config.client"); |
| 15 | + const int daysBeforeEpoch = 719162; |
30 | 16 |
|
31 | | - // |
32 | | - // The communicator initialization removes all Ice-related arguments from argc/argv |
33 | | - // |
34 | | - if (argc > 1) |
35 | | - { |
36 | | - cerr << argv[0] << ": too many arguments" << endl; |
37 | | - status = 1; |
38 | | - } |
39 | | - else |
40 | | - { |
41 | | - status = run(ich.communicator()); |
42 | | - } |
43 | | - } |
44 | | - catch (const std::exception& ex) |
45 | | - { |
46 | | - cerr << argv[0] << ": " << ex.what() << endl; |
47 | | - status = 1; |
48 | | - } |
| 17 | + int64_t timeStampMicro = |
| 18 | + chrono::duration_cast<chrono::microseconds>(timePoint.time_since_epoch() + daysBeforeEpoch * 24h).count(); |
49 | 19 |
|
50 | | - return status; |
| 20 | + // The time stamp is in ticks, where each tick is 100 nanoseconds = 0.1 microsecond. |
| 21 | + return timeStampMicro * 10; |
| 22 | + } |
51 | 23 | } |
52 | 24 |
|
53 | | -void menu(); |
54 | | - |
55 | 25 | int |
56 | | -run(const shared_ptr<Ice::Communicator>& communicator) |
| 26 | +main(int argc, char* argv[]) |
57 | 27 | { |
58 | | - auto sender = Ice::checkedCast<CallbackSenderPrx>( |
59 | | - communicator->propertyToProxy("CallbackSender.Proxy")->ice_twoway()->ice_secure(false)); |
60 | | - if (!sender) |
61 | | - { |
62 | | - cerr << "invalid proxy" << endl; |
63 | | - return 1; |
64 | | - } |
| 28 | + // Create an Ice communicator to initialize the Ice runtime. |
| 29 | + const Ice::CommunicatorHolder communicatorHolder{argc, argv}; |
| 30 | + const Ice::CommunicatorPtr& communicator = communicatorHolder.communicator(); |
| 31 | + |
| 32 | + // Create an object adapter that listens for incoming requests and dispatches them to servants. |
| 33 | + // Since we don't specify a port, the OS will choose an ephemeral port. This allows multiple client applications to |
| 34 | + // run concurrently on the same host. |
| 35 | + Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapterWithEndpoints("AlarmClockAdapter", "tcp"); |
65 | 36 |
|
66 | | - auto adapter = communicator->createObjectAdapter("Callback.Client"); |
67 | | - adapter->add(make_shared<CallbackReceiverI>(), Ice::stringToIdentity("callbackReceiver")); |
| 37 | + // Register the MockAlarmClock servant with the adapter, and get an alarm clock proxy. |
| 38 | + auto mockAlarmClock = make_shared<Client::MockAlarmClock>(); |
| 39 | + auto alarmClock = adapter->add<AlarmClockPrx>(mockAlarmClock, Ice::stringToIdentity("alarmClock")); |
| 40 | + |
| 41 | + // Start dispatching requests. |
68 | 42 | adapter->activate(); |
| 43 | + cout << "Listening on ephemeral port..." << endl; |
69 | 44 |
|
70 | | - auto receiver = |
71 | | - Ice::uncheckedCast<CallbackReceiverPrx>(adapter->createProxy(Ice::stringToIdentity("callbackReceiver"))); |
| 45 | + // Create a proxy to the wake-up service. |
| 46 | + WakeUpServicePrx wakeUpService{communicator, "wakeUpService:tcp -h localhost -p 4061"}; |
72 | 47 |
|
73 | | - menu(); |
| 48 | + // Schedule a wake-up call in 5 seconds. |
| 49 | + wakeUpService->wakeMeUp(alarmClock, toTimeStamp(chrono::system_clock::now() + chrono::seconds{5})); |
| 50 | + cout << "Wake-up call scheduled, falling asleep..." << endl; |
74 | 51 |
|
75 | | - char c = 'x'; |
76 | | - do |
77 | | - { |
78 | | - try |
79 | | - { |
80 | | - cout << "==> "; |
81 | | - cin >> c; |
82 | | - if (c == 't') |
83 | | - { |
84 | | - sender->initiateCallback(receiver); |
85 | | - } |
86 | | - else if (c == 's') |
87 | | - { |
88 | | - sender->shutdown(); |
89 | | - } |
90 | | - else if (c == 'x') |
91 | | - { |
92 | | - // Nothing to do |
93 | | - } |
94 | | - else if (c == '?') |
95 | | - { |
96 | | - menu(); |
97 | | - } |
98 | | - else |
99 | | - { |
100 | | - cout << "unknown command `" << c << "'" << endl; |
101 | | - menu(); |
102 | | - } |
103 | | - } |
104 | | - catch (const Ice::Exception& ex) |
105 | | - { |
106 | | - cerr << ex << endl; |
107 | | - } |
108 | | - } while (cin.good() && c != 'x'); |
| 52 | + // Wait until the "stop" button is pressed on the mock alarm clock. |
| 53 | + mockAlarmClock->wait(); |
| 54 | + cout << "Stop button pressed, exiting..." << endl; |
109 | 55 |
|
110 | 56 | return 0; |
111 | 57 | } |
112 | | - |
113 | | -void |
114 | | -menu() |
115 | | -{ |
116 | | - cout << "usage:\n" |
117 | | - "t: send callback\n" |
118 | | - "s: shutdown server\n" |
119 | | - "x: exit\n" |
120 | | - "?: help\n"; |
121 | | -} |
0 commit comments