Skip to content

Commit 69bc7ee

Browse files
Sindhu1143Sivasindhura Mule
authored andcommitted
RIC-1064 PingPong11 Example (#55)
* PingPong11 * PingPong11 * PingPong11 * PingPongTimeDeltaTracker * PingPongTimeDeltaTracker --------- Co-authored-by: Sivasindhura Mule <sivasindhura.mule@prod.hclpnp.com>
1 parent 6126a05 commit 69bc7ee

7 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let tc = TCF.define(TCF.CPP_TRANSFORM);
2+
tc.topCapsule = 'Top';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
protocol PingPongPort {
2+
/* In Events */
3+
in pong();
4+
/* Out Events */
5+
out ping();
6+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
capsule Ponger {
2+
[[rt::header_preface]]
3+
`
4+
#include <iostream>
5+
`
6+
[[rt::decl]]
7+
`
8+
public:
9+
static const long int DELAY = 0;
10+
`
11+
behavior port log:Log;
12+
service behavior port pongPort~ : PingPongPort;
13+
behavior service port timing:Timing;
14+
statemachine {
15+
state Playing, HitBack{
16+
entry `
17+
timing.informIn(RTTimespec(0,DELAY));
18+
`;
19+
};
20+
initial -> Playing
21+
`
22+
log.log("[Ponger] starting up");
23+
`;
24+
Playing -> HitBack on pongPort.ping
25+
`
26+
log.log("[Ponger] ping received!");
27+
`;
28+
HitBack -> Playing on timing.timeout
29+
`
30+
pongPort.pong().send();
31+
log.log("[Ponger] pong sent ...");
32+
`;
33+
};
34+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- model of PingPongTimeDeltaTracker
2+
Emphasizes tracking the difference (delta) between two time points.
3+
4+
Existing PingPong sample is used to demonstrate and count the number of messages exchanged, while PingPongTimeDeltaTracker measures the time taken for each round of communication to occur.
5+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
capsule Top {
2+
3+
part ping : Pinger, pong : Ponger;
4+
behavior port log : Log;
5+
/* Connectors */
6+
connect ping.pingPort with pong.pongPort;
7+
/* State Machine */
8+
statemachine {
9+
state State1;
10+
initial -> State1
11+
`
12+
log.log("[Top] starting up");
13+
`;
14+
};
15+
};
16+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This Art language model is contributed by Queen's University
2+
PingPong 11 : illustrates timers and how to measure response times
3+
[Link](https://research.cs.queensu.ca/home/dingel/cisc844_F23/sampleModels/sampleModels.html)

0 commit comments

Comments
 (0)