1 #ifndef CPP_PCP_CLIENT_SRC_CONNECTOR_TIMINGS_H_
2 #define CPP_PCP_CLIENT_SRC_CONNECTOR_TIMINGS_H_
3 
4 #include <cpp-pcp-client/export.h>
5 
6 #include <boost/chrono/chrono.hpp>
7 
8 #include <string>
9 
10 namespace PCPClient {
11 
12 //
13 // ConnectionTimings
14 //
15 
16 struct LIBCPP_PCP_CLIENT_EXPORT ConnectionTimings {
17     using Duration_us  = boost::chrono::duration<int, boost::micro>;
18     using Duration_min = boost::chrono::minutes;
19 
20     boost::chrono::high_resolution_clock::time_point start;
21     boost::chrono::high_resolution_clock::time_point tcp_pre_init;
22     boost::chrono::high_resolution_clock::time_point tcp_post_init;
23     boost::chrono::high_resolution_clock::time_point open;
24     boost::chrono::high_resolution_clock::time_point closing_handshake;
25     boost::chrono::high_resolution_clock::time_point close;
26 
27     bool isOpen() const;
28     bool isClosingStarted() const;
29     bool isFailed() const;
30     bool isClosed() const;
31 
32     /// Sets the `start` time_point member to the current instant,
33     /// the other time_points to epoch, and all state flags to false
34     void reset();
35 
36     /// Sets the `open` time_point member to the current instant
37     /// and sets the related flag
38     void setOpen();
39 
40     /// Sets the `closing_handshake` time_point member to the
41     /// current instant and flags `closing_started`
42     void setClosing();
43 
44     /// Sets the `close` time_point member to the current instant,
45     /// flags `closed` and sets `connection_failed` to onFail_event
46     void setClosed(bool onFail_event = false);
47 
48     /// Time interval to establish the TCP connection [us]
49     Duration_us getTCPInterval() const;
50 
51     /// Time interval to perform the WebSocket Opening Handshake [us];
52     /// it will return:
53     ///  - a null duration, if the WebSocket is not open;
54     ///  - the (tcp_post_init - tcp_pre_init) duration, otherwise.
55     Duration_us getOpeningHandshakeInterval() const;
56 
57     /// Time interval to establish the overall WebSocket connection [us];
58     /// it will return:
59     ///  - a null duration, if the WebSocket is not open;
60     ///  - the (open - start) duration, otherwise.
61     Duration_us getWebSocketInterval() const;
62 
63     /// Time interval to perform the WebSocket Closing Handshake [us];
64     /// it will return:
65     ///  - a null duration, if:
66     ///        * the Websocket is not open;
67     ///        * the Closing Handshake was not started by this client;
68     ///        * the Closing Handshake did not complete.
69     ///  - the (close - closing_handshake) duration, otherwise.
70     Duration_us getClosingHandshakeInterval() const;
71 
72     /// Duration of the WebSocket connection [minutes]; it will return:
73     ///  - a null duration, if the WebSocket connection was not established;
74     ///  - the (close - start) duration, if the connection was established
75     ///    and, then, closed;
76     ///  - the (now - start) duration, otherwise.
77     Duration_min getOverallConnectionInterval_min() const;
78 
79     /// As getOverallConnectionInterval_min, but the duration is in us.
80     Duration_us getOverallConnectionInterval_us() const;
81 
82     /// Returns a string with the WebSocket Opening Handshake timings
83     std::string toString() const;
84 
85   private:
86     bool _open            { false };
87     bool _closing_started { false };
88     bool _failed          { false };
89     bool _closed          { false };
90 
91     std::string getOverallDurationTxt() const;
92 };
93 
94 //
95 // AssociationTimings
96 //
97 
98 struct LIBCPP_PCP_CLIENT_EXPORT AssociationTimings {
99     using Duration_ms  = boost::chrono::duration<int, boost::milli>;
100     using Duration_min = boost::chrono::minutes;
101 
102     boost::chrono::high_resolution_clock::time_point start;
103     boost::chrono::high_resolution_clock::time_point association;
104     boost::chrono::high_resolution_clock::time_point close;
105 
106     bool completed { false };
107     bool success { false };
108     bool closed { false };
109 
110     /// Sets the `start` time_point member to the current instant,
111     /// the other time_points to epoch, and all flags to false
112     void reset();
113 
114     /// Sets the Association time_point member to the current instant
115     /// if `closed` was not flagged, otherwise use the close one, then
116     /// flags `completed` and sets `success` as specified
117     void setCompleted(bool _success = true);
118 
119     /// Sets the session closure time_point member to the current instant
120     /// and flags `closed`
121     void setClosed();
122 
123     /// Time interval to perform the Session Association [ms];
124     /// it will return:
125     ///  - a null duration, if the Session Association was not completed;
126     ///  - the (association - start) duration, otherwise.
127     Duration_ms getAssociationInterval() const;
128 
129     /// Duration of the PCP Session [minutes]; it will return:
130     ///  - a null duration, in case the Association was not completed;
131     ///  - the (close - start) duration, in case the session was closed;
132     ///  - the (now - start) duration, otherwise.
133     Duration_min getOverallSessionInterval_min() const;
134 
135     /// As getOverallSessionInterval_min, but the duration is in ms.
136     Duration_ms getOverallSessionInterval_ms() const;
137 
138     /// Returns a string with the Association interval [ms];
139     /// if `include_completion` is flagged and the Association was
140     /// previously set as successfully completed, the string will
141     /// also include the duration of the PCP Session
142     std::string toString(bool include_completion = true) const;
143 };
144 
145 }  // namespace PCPClient
146 
147 #endif  // CPP_PCP_CLIENT_SRC_CONNECTOR_TIMINGS_H_
148