1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
use hyper::client;
use lightstreamer_client::item_update::ItemUpdate;
use lightstreamer_client::ls_client::LightstreamerClient;
use lightstreamer_client::subscription::{Snapshot, Subscription, SubscriptionMode};
use lightstreamer_client::subscription_listener::SubscriptionListener;
use futures::stream::StreamExt;
use futures::SinkExt;
use reqwest::Client;
use serde_urlencoded;
use signal_hook::low_level::signal_name;
use signal_hook::{consts::SIGINT, consts::SIGTERM, iterator::Signals};
use std::error::Error;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
/// Sets up a signal hook for SIGINT and SIGTERM.
///
/// Creates a signal hook for the specified signals and spawns a thread to handle them.
/// When a signal is received, it logs the signal name and performs cleanup before exiting with 0 code
/// to indicate orderly shutdown.
///
/// # Arguments
///
/// * `full_path` - The full path to the application configuration file.
///
/// # Panics
///
/// The function panics if it fails to create the signal iterator.
///
async fn setup_signal_hook(client: Arc<Mutex<LightstreamerClient>>) {
// Create a signal set of signals to be handled and a signal iterator to monitor them.
let signals = &[SIGINT, SIGTERM];
let mut signals_iterator = Signals::new(signals).expect("Failed to create signal iterator");
// Create a new thread to handle signals sent to the process
tokio::spawn(async move {
for signal in signals_iterator.forever() {
println!("Received signal: {}", signal_name(signal).unwrap());
//
// Clean up and prepare to exit...
// ...
let mut client = client.lock().await;
client.disconnect();
// Exit with 0 code to indicate orderly shutdown.
std::process::exit(0);
}
});
}
async fn establish_persistent_http_connection(
session_id_shared: Arc<Mutex<String>>,
) -> Result<(), reqwest::Error> {
let client = Client::new();
let params = [
("LS_adapter_set", "DEMO"),
("LS_cid", "mgQkwtwdysogQz2BJ4Ji%20kOj2Bg"),
];
let request_url =
"http://push.lightstreamer.com/lightstreamer/create_session.txt?LS_protocol=TLCP-2.0.0";
let response = client.post(request_url).form(¶ms).send().await?;
if response.status().is_success() {
let mut stream = response.bytes_stream();
while let Some(item) = stream.next().await {
match item {
Ok(bytes) => {
let response_text = String::from_utf8(bytes.to_vec())
.expect("Failed to convert bytes to string");
if let Some(start) = response_text.find("CONOK,") {
if let Some(end) = response_text.find(",50000,5000,*\r\n") {
let session_id = &response_text[start + 6..end];
println!("Session ID: {}", session_id);
let mut session_id_lock = session_id_shared.lock().await;
*session_id_lock = session_id.to_string();
}
} else {
println!("New message: {}", response_text);
}
}
Err(e) => println!("Error while receiving: {:?}", e),
}
}
} else {
println!("Response was not successful: {}", response.status());
}
Ok(())
}
/*
// Establish a persistent WebSocket connection and handle the session creation
async fn establish_persistent_ws_connection(
session_id_shared: Arc<Mutex<String>>,
) -> Result<(), Box<dyn Error>> {
let ws_url = "wss://push.lightstreamer.com/lightstreamer";
let (ws_stream, _) = tokio_tungstenite::connect_async_with_config(
tokio_tungstenite::tungstenite::protocol::handshake::client::Request::from((ws_url, [("Sec-WebSocket-Protocol", "your-subprotocol")].iter().cloned()))
).await.expect("Failed to connect");
let (mut write, mut read) = ws_stream.split();
// Session creation parameters
let params = [
("LS_op2", "create_session"),
("LS_cid", "mgQkwtwdysogQz2BJ4Ji kOj2Bg"),
("LS_adapter_set", "DEMO"),
];
let encoded_params = serde_urlencoded::to_string(¶ms)?;
// Send the create session message
write
.send(Message::Text(format!("{}\n", encoded_params)))
.await?;
// Listen for messages from the server
while let Some(message) = read.next().await {
match message? {
Message::Text(text) => {
if text.starts_with("CONOK") {
let session_info: Vec<&str> = text.split(",").collect();
let session_id = session_info.get(1).unwrap_or(&"").to_string();
*session_id_shared.lock().await = session_id.clone();
println!("Session established with ID: {}", session_id);
subscribe_to_channel_ws(session_id, write).await?;
break; // Exit after successful subscription
}
}
_ => {}
}
}
Ok(())
}
*/
async fn subscribe_to_channel(session_id: String) -> Result<(), reqwest::Error> {
let client = Client::new();
//let subscribe_url = "http://push.lightstreamer.com/lightstreamer/bind_session.txt";
//let params = [("LS_session", &session_id)];
let subscribe_url =
"http://push.lightstreamer.com/lightstreamer/control.txt?LS_protocol=TLCP-2.0.0";
let params = [
("LS_session", &session_id),
("LS_op", &"add".to_string()),
("LS_subId", &"1".to_string()),
("LS_data_adapter", &"CHAT_ROOM".to_string()),
("LS_group", &"chat_room".to_string()),
("LS_schema", &"timestamp message".to_string()),
("LS_mode", &"DISTINCT".to_string()),
("LS_reqId", &"1".to_string()),
];
let response = client.post(subscribe_url).form(¶ms).send().await?;
if response.status().is_success() {
println!("Subscription successful!");
} else {
println!("Subscription failed: {}", response.status());
}
Ok(())
}
// Function to subscribe to a channel using WebSocket
async fn subscribe_to_channel_ws(
session_id: String,
mut write: futures::stream::SplitSink<
tokio_tungstenite::WebSocketStream<
tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>,
>,
tokio_tungstenite::tungstenite::protocol::Message,
>,
) -> Result<(), Box<dyn Error>> {
// Example subscription to ITEM1 in MERGE mode from the DEMO adapter set
let sub_params = [
("LS_table", "1"),
("LS_op2", "add"),
("LS_session", &session_id),
("LS_id", "item1"),
("LS_schema", "stock_name last_price"),
("LS_mode", "MERGE"),
];
let encoded_sub_params = serde_urlencoded::to_string(&sub_params)?;
// Send the subscription message
write.send(Message::Text(encoded_sub_params)).await?;
println!("Subscribed to channel with session ID: {}", session_id);
Ok(())
}
/*
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let session_id_shared = Arc::new(Mutex::new(String::new()));
let session_id_shared_clone = session_id_shared.clone();
let task1 = tokio::spawn(async move {
establish_persistent_http_connection(session_id_shared_clone).await.unwrap();
});
println!("Established connection to Lightstreamer server");
let task2 = tokio::spawn(async move {
let mut session_established = false;
loop {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let session_id;
{
session_id = session_id_shared.lock().await.clone();
}
if !session_established && !session_id.is_empty() {
println!("Accessed Session ID from another thread: {}", session_id);
session_established = true;
subscribe_to_channel(session_id).await.unwrap();
}
}
});
task1.await?;
task2.await?;
Ok(())
}
*/
pub struct MySubscriptionListener {}
impl SubscriptionListener for MySubscriptionListener {
fn on_item_update(&mut self, update: ItemUpdate) {
println!(
"UPDATE {} {}",
update.get_value("stock_name").unwrap(),
update.get_value("last_price").unwrap()
);
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
//
// Create a new subscription instance.
//
let mut my_subscription = Subscription::new(
SubscriptionMode::Merge,
Some(vec![
"item1".to_string(),
"item2".to_string(),
"item3".to_string(),
]),
Some(vec!["stock_name".to_string(), "last_price".to_string()]),
)?;
my_subscription.set_data_adapter(Some(String::from("QUOTE_ADAPTER")))?;
my_subscription.set_requested_snapshot(Some(Snapshot::Yes))?;
my_subscription.add_listener(Box::new(MySubscriptionListener {}));
// Create a new Lightstreamer client instance and wrap it in an Arc<Mutex<>> so it can be shared across threads.
let client = Arc::new(Mutex::new(LightstreamerClient::new(
Some("http://push.lightstreamer.com/lightstreamer"),
Some("DEMO"),
)?));
//
// Add the subscription to the client.
//
{
let mut client = client.lock().await;
client.subscribe(my_subscription);
}
// Spawn a new thread to handle SIGINT and SIGTERM process signals.
setup_signal_hook(client.clone()).await;
//
// Infinite loop that will indefinitely retry failed connections unless
// a SIGTERM or SIGINT signal is received.
//
let mut retry_interval_milis: u64 = 0;
let mut retry_counter: u64 = 0;
loop {
let mut client = client.lock().await;
match client.connect() {
Ok(_) => {}
Err(e) => {
println!("Failed to connect: {:?}", e);
tokio::time::sleep(std::time::Duration::from_millis(retry_interval_milis)).await;
retry_interval_milis = (retry_interval_milis + (200 * retry_counter)).min(5000);
retry_counter += 1;
println!(
"Retrying connection in {} seconds...",
format!("{:.2}", retry_interval_milis as f64 / 1000.0)
);
}
}
}
}
|