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