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