dxFeed Graal CXX API v5.0.0
Loading...
Searching...
No Matches
InstrumentProfileConnection.hpp
1// Copyright (c) 2025 Devexperts LLC.
2// SPDX-License-Identifier: MPL-2.0
3
4#pragma once
5
6#include "../../internal/Conf.hpp"
7
9
10#include "../../entity/SharedEntity.hpp"
11#include "../../internal/Common.hpp"
12#include "../../internal/Id.hpp"
13#include "../../internal/JavaObjectHandle.hpp"
14#include "./InstrumentProfileCollector.hpp"
15
17
18/**
19 * Connects to an instrument profile URL and reads instrument profiles with support of
20 * streaming live updates.
21 * Please see <b>Instrument Profile Format</b> documentation for complete description.
22 */
23class DXFCPP_EXPORT InstrumentProfileConnection final : public SharedEntity {
24 public:
25 /// The alias to a type of shared pointer to the InstrumentProfileConnection object
26 using Ptr = std::shared_ptr<InstrumentProfileConnection>;
27
28 /// The alias to a type of unique pointer to the InstrumentProfileConnection object
29 using Unique = std::unique_ptr<InstrumentProfileConnection>;
30
31 /**
32 * Instrument profile connection state.
33 */
34 enum class State : std::int32_t {
35 /**
36 * Instrument profile connection is not started yet.
37 * @ref InstrumentProfileConnection::start() "start" was not invoked yet.
38 */
40
41 /**
42 * Connection is being established.
43 */
45
46 /**
47 * Connection was established.
48 */
50
51 /**
52 * Initial instrument profiles snapshot was fully read (this state is set only once).
53 */
55
56 /**
57 * Instrument profile connection was @ref InstrumentProfileConnection::close() "closed".
58 */
60 };
61
62 private:
63 Id<InstrumentProfileConnection> id_;
64 JavaObjectHandle<InstrumentProfileConnection> handle_{};
65 JavaObjectHandle<IpfPropertyChangeListener> stateChangeListenerHandle_{};
66 SimpleHandler<void(State, State)> onStateChange_{};
67
68 InstrumentProfileConnection() noexcept;
69
70 struct Impl;
71
72 public:
73 static std::string stateToString(State state) noexcept {
74 switch (state) {
76 return "NOT_CONNECTED";
78 return "CONNECTING";
80 return "CONNECTED";
82 return "COMPLETED";
83 case State::CLOSED:
84 return "CLOSED";
85 }
86
87 return "";
88 }
89
90 /**
91 * Creates instrument profile connection with a specified address and collector.
92 * Address may be just "<host>:<port>" of server, URL, or a file path.
93 * The "[update=<period>]" clause can be optionally added at the end of the address to
94 * specify an @ref InstrumentProfileConnection::getUpdatePeriod() "update period" via an address string.
95 * Default update period is 1 minute.
96 *
97 * <p>Connection needs to be @ref InstrumentProfileConnection::start() "started" to begin an actual operation.
98 *
99 * @param address The address.
100 * @param collector The instrument profile collector to push updates into.
101 *
102 * @return new instrument profile connection.
103 */
104 static Ptr createConnection(const StringLike &address, InstrumentProfileCollector::Ptr collector);
105
106 /**
107 * Returns the address of this instrument profile connection.
108 * It does not include additional options specified as part of the address.
109 *
110 * @return The address of this instrument profile connection.
111 */
112 std::string getAddress() const;
113
114 /**
115 * Returns update period in milliseconds.
116 * It is period of an update check when the instrument profiles source does not support live updates
117 * and/or when connection is dropped.
118 * Default update period is 1 minute, unless overridden in an
119 * @ref InstrumentProfileConnection::createConnection() "address string".
120 *
121 * @return The update period in milliseconds.
122 */
123 std::int64_t getUpdatePeriod() const;
124
125 /**
126 * Returns update period in milliseconds as chrono::duration
127 * It is period of an update check when the instrument profiles source does not support live updates
128 * and/or when connection is dropped.
129 * Default update period is 1 minute, unless overridden in an
130 * @ref InstrumentProfileConnection::createConnection() "address string".
131 *
132 * @return The update period in milliseconds as chrono::duration.
133 */
134 std::chrono::milliseconds getUpdatePeriodAsDuration() const {
135 return std::chrono::milliseconds(getUpdatePeriod());
136 }
137
138 /**
139 * Changes the update period in milliseconds.
140 *
141 * @param updatePeriod The update period in milliseconds.
142 * @see InstrumentProfileConnection::getUpdatePeriod()
143 */
144 void setUpdatePeriod(std::int64_t updatePeriod) const;
145
146 /**
147 * Changes the update period in milliseconds as chrono::duration.
148 *
149 * @param updatePeriod The update period in milliseconds as chrono::duration.
150 * @see InstrumentProfileConnection::getUpdatePeriod()
151 */
152 void setUpdatePeriod(std::chrono::milliseconds updatePeriod) const {
153 setUpdatePeriod(updatePeriod.count());
154 }
155
156 /**
157 * Returns state of this instrument profile connections.
158 *
159 * @return The state of this instrument profile connections.
160 */
161 State getState() const;
162
163 /**
164 * Returns last modification time (in milliseconds) of instrument profiles or zero if it is unknown.
165 * Note, that while the time is represented in milliseconds, the actual granularity of time here is a second.
166 *
167 * @return The last modification time (in milliseconds) of instrument profiles or zero if it is unknown.
168 */
169 std::int64_t getLastModified() const;
170
171 /**
172 * Starts this instrument profile connection. This connection's state immediately changes to
173 * @ref InstrumentProfileConnection::State::CONNECTING "CONNECTING" and the actual connection establishment proceeds
174 * in the background.
175 */
176 void start() const;
177
178 /**
179 * Closes this instrument profile connection. This connection's state immediately changes to
180 * @ref InstrumentProfileConnection::State::CLOSED "CLOSED" and the background update procedures are terminated.
181 */
182 void close() const;
183
184 /**
185 * Adds listener that is notified about changes in @ref InstrumentProfileConnection::getState() "state" property.
186 *
187 * <p>Installed listener can be removed by `id` with InstrumentProfileConnection::removeStateChangeListener method
188 * or by call `InstrumentProfileConnection::onStateChange() -= id`;
189 *
190 * @tparam StateChangeListener The listener type. It can be any callable with signature: `void(State, State)`
191 * @param listener The listener to add
192 * @return the listener id
193 */
194 template <typename StateChangeListener>
195 std::size_t addStateChangeListener(StateChangeListener &&listener)
196#if __cpp_concepts
198 { listener(State{}, State{}) } -> std::same_as<void>;
199 }
200#endif
201 {
202 return onStateChange_ += listener;
203 }
204
205 /**
206 * Removes listener that is notified about changes in @ref InstrumentProfileConnection::getState() "state" property.
207 * It removes the listener that was previously installed with InstrumentProfileConnection::addStateChangeListener
208 * method.
209 *
210 * @param listenerId The listener id to remove
211 */
212 void removeStateChangeListener(std::size_t listenerId) {
213 onStateChange_ -= listenerId;
214 }
215
216 /**
217 * Returns the onStateChange @ref SimpleHandler<void(ArgTypes...)> "handler" that can be used to add or remove
218 * listeners.
219 *
220 * @return onStateChange handler with `void(State, State)` signature
221 */
223 return onStateChange_;
224 }
225
226 /**
227 * Synchronously waits for full first snapshot read with the specified timeout.
228 *
229 * @param timeout The maximum time (in millis) to wait
230 * @return `true` if @ref InstrumentProfileConnection::State::COMPLETED "COMPLETED" state was reached and `false`
231 * if the waiting time elapsed before snapshot was fully read.
232 */
233 bool waitUntilCompleted(std::int64_t timeout) const;
234
235 /**
236 * Synchronously waits for full first snapshot read with the specified timeout.
237 *
238 * @param timeout The maximum time to wait
239 * @return `true` if @ref InstrumentProfileConnection::State::COMPLETED "COMPLETED" state was reached and `false`
240 * if the waiting time elapsed before snapshot was fully read.
241 */
242 bool waitUntilCompleted(std::chrono::milliseconds timeout) const {
243 return waitUntilCompleted(timeout.count());
244 }
245};
246
248
#define DXFCXX_DISABLE_MSC_WARNINGS_POP()
Definition Conf.hpp:31
#define DXFCPP_END_NAMESPACE
Definition Conf.hpp:97
#define DXFCPP_BEGIN_NAMESPACE
Definition Conf.hpp:94
#define DXFCXX_DISABLE_MSC_WARNINGS_PUSH(warnings)
Definition Conf.hpp:30
#define DXFCPP_EXPORT
Definition api.h:35
auto & onStateChange()
Returns the onStateChange handler that can be used to add or remove listeners.
Definition InstrumentProfileConnection.hpp:222
static Ptr createConnection(const StringLike &address, InstrumentProfileCollector::Ptr collector)
Creates instrument profile connection with a specified address and collector.
Definition InstrumentProfileConnection.cpp:61
bool waitUntilCompleted(std::chrono::milliseconds timeout) const
Synchronously waits for full first snapshot read with the specified timeout.
Definition InstrumentProfileConnection.hpp:242
State getState() const
Returns state of this instrument profile connections.
Definition InstrumentProfileConnection.cpp:91
void removeStateChangeListener(std::size_t listenerId)
Removes listener that is notified about changes in state property.
Definition InstrumentProfileConnection.hpp:212
void close() const
Closes this instrument profile connection.
Definition InstrumentProfileConnection.cpp:103
std::int64_t getLastModified() const
Returns last modification time (in milliseconds) of instrument profiles or zero if it is unknown.
Definition InstrumentProfileConnection.cpp:95
State
Instrument profile connection state.
Definition InstrumentProfileConnection.hpp:34
@ CLOSED
Instrument profile connection was closed.
Definition InstrumentProfileConnection.hpp:59
@ COMPLETED
Initial instrument profiles snapshot was fully read (this state is set only once).
Definition InstrumentProfileConnection.hpp:54
@ CONNECTING
Connection is being established.
Definition InstrumentProfileConnection.hpp:44
@ CONNECTED
Connection was established.
Definition InstrumentProfileConnection.hpp:49
@ NOT_CONNECTED
Instrument profile connection is not started yet.
Definition InstrumentProfileConnection.hpp:39
std::string getAddress() const
Returns the address of this instrument profile connection.
Definition InstrumentProfileConnection.cpp:79
std::int64_t getUpdatePeriod() const
Returns update period in milliseconds.
Definition InstrumentProfileConnection.cpp:83
void start() const
Starts this instrument profile connection.
Definition InstrumentProfileConnection.cpp:99
void setUpdatePeriod(std::int64_t updatePeriod) const
Changes the update period in milliseconds.
Definition InstrumentProfileConnection.cpp:87
std::size_t addStateChangeListener(StateChangeListener &&listener)
Adds listener that is notified about changes in state property.
Definition InstrumentProfileConnection.hpp:195
void setUpdatePeriod(std::chrono::milliseconds updatePeriod) const
Changes the update period in milliseconds as chrono::duration.
Definition InstrumentProfileConnection.hpp:152
std::chrono::milliseconds getUpdatePeriodAsDuration() const
Returns update period in milliseconds as chrono::duration It is period of an update check when the in...
Definition InstrumentProfileConnection.hpp:134
bool waitUntilCompleted(std::int64_t timeout) const
Synchronously waits for full first snapshot read with the specified timeout.
Definition InstrumentProfileConnection.cpp:107
A base abstract "shared entity" class. Has some helpers for dynamic polymorphism.
Definition SharedEntity.hpp:20
A lightweight wrapper around strings or string-like inputs.
Definition StringUtils.hpp:27