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