1+ capsule Pinger {
2+ [[rt::header_preface]]
3+ `
4+ #include <cstdio>
5+ #include <cstdlib>
6+ #include <iostream>
7+ #include <ctime>
8+ #include <chrono>
9+ #include <iomanip>
10+ #include <cmath>
11+ #include <cstring>
12+ `
13+ [[rt::impl_preface]]
14+ `
15+ #include <stdlib.h>
16+ `
17+ [[rt::decl]]
18+ `
19+ public:
20+ int count = 0;
21+ RTTimespec startTime;
22+ RTTimespec prevTime;
23+ RTTimespec nowTime;
24+ int ROUNDS;
25+ void printReadableFormat(const RTTimespec& timeToConvert) {
26+ long long totalMilliseconds = (timeToConvert.tv_sec * 1000LL) + (timeToConvert.tv_nsec / 1000000LL);
27+ totalMilliseconds = std::abs(totalMilliseconds);
28+ std::chrono::milliseconds duration(totalMilliseconds);
29+ std::chrono::system_clock::time_point time_point = std::chrono::system_clock::time_point(duration);
30+ std::time_t timeFormat = std::chrono::system_clock::to_time_t(time_point);
31+ std::tm* timeValue = std::gmtime(&timeFormat);
32+ char buffer[80];
33+ std::strftime(buffer, sizeof(buffer), "%H:%M:%S", timeValue);
34+ std::cout << buffer << " " << std::setw(3) << std::setfill('0') << timeToConvert.tv_nsec / 1000000 << " msec";
35+ }
36+
37+ void printDifference(const RTTimespec& startTime, const RTTimespec& lastPongTime) {
38+ long long diffSeconds = lastPongTime.tv_sec - startTime.tv_sec;
39+ long long diffNanoseconds = lastPongTime.tv_nsec - startTime.tv_nsec;
40+ if (diffNanoseconds < 0) {
41+ diffSeconds--;
42+ diffNanoseconds += 1000000000LL;
43+ }
44+ std::cout << std::setw(2) << std::setfill('0') << (diffSeconds / 3600) << ":"; // hours
45+ std::cout << std::setw(2) << std::setfill('0') << ((diffSeconds % 3600) / 60) << ":"; // minutes
46+ std::cout << std::setw(2) << std::setfill('0') << (diffSeconds % 60); // seconds
47+ std::cout << " " << std::setw(3) << std::setfill('0') << (diffNanoseconds / 1000000) << " msec" << std::endl; // milliseconds
48+ }
49+
50+ `
51+ behavior port log:Log;
52+ service behavior port pingPort : PingPongPort;
53+
54+ statemachine {
55+ choice _myChoice;
56+ state State1, Done{
57+ entry `
58+ log.log("[Pinger] stopping");
59+ `;
60+ };
61+ initial -> State1 `
62+ std::cout << "Enter number of Rounds" << std::endl;
63+ std::cin >> ROUNDS;
64+
65+ RTTimespec::getclock(startTime);
66+ prevTime = startTime;
67+
68+ // Start the game by sending a "ping" to the other player
69+ log.log("[Pinger] starting game");
70+ pingPort.ping().send();
71+ log.show("[Pinger] ping sent ... ");
72+ `;
73+ State1 -> _myChoice on pingPort.pong
74+ `
75+ log.log("[Pinger] pong received!");
76+
77+ // grab current time and store in now
78+ RTTimespec::getclock(nowTime);
79+
80+ // RTTimespec diff = nowTime - prevTime;
81+ // std::cout << "[Pinger] time of previous pong:" << prevTime.tv_sec << " Seconds, " << prevTime.tv_nsec << " NanoSeconds" << std::endl;
82+ // std::cout << "[Pinger] time of this pong :" << nowTime.tv_sec << " Seconds, " << nowTime.tv_nsec << " NanoSeconds" << std::endl;
83+ // std::cout << "[Pinger] difference : " << diff.tv_sec << " Seconds, " << diff.tv_nsec << " NanoSeconds" << std::endl;
84+
85+ `;
86+ _myChoice -> State1 when `return (count<ROUNDS);`
87+ `
88+ prevTime = nowTime;
89+ count++;
90+ log.log("[Pinger] ------");
91+ pingPort.ping().send();
92+ log.show("[Pinger] ping sent ... \n");
93+ `;
94+ _myChoice -> Done when `else`
95+ `
96+ log.log("[Pinger] -----");
97+ log.log("[Pinger] time to stop");
98+ std::cout << "[Pinger] start time :"; printReadableFormat(startTime); std::cout << std::endl;
99+ std::cout << "[Pinger] time of last pong :"; printReadableFormat(nowTime); std::cout << std::endl;
100+ std::cout << "[Pinger] difference :"; printDifference(startTime, nowTime);
101+
102+ `;
103+ };
104+ };
0 commit comments