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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use crate::{api, request::request};

use itertools::Itertools;
use num_rational::Rational64;
use serde::Deserialize;

use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use url::Url;

const MAX_SUBSCRIPTION_SIZE: usize = 500;

pub struct Client {
    base_url: String,
    access_token: String,
    client_id: Option<String>,
    socket: Option<tungstenite::protocol::WebSocket<tungstenite::client::AutoStream>>,
    next_message_id: i32,
    subscription_fields: HashMap<String, Vec<String>>,
}

impl Client {
    pub async fn new() -> Result<Self, Box<dyn Error>> {
        #[derive(Debug, Deserialize)]
        #[serde(rename_all = "kebab-case")]
        struct Data {
            websocket_url: String,
            token: String,
        }

        let response = request("quote-streamer-tokens", "").await?;
        let api::Response { data, .. } = response.json::<api::Response<Data>>().await?;

        let base_url = format!("{}/cometd", data.websocket_url).replace("https", "wss");
        let access_token = data.token;

        Ok(Client {
            base_url,
            access_token,
            client_id: None,
            socket: None,
            next_message_id: 1,
            subscription_fields: HashMap::new(),
        })
    }

    pub fn connect(&mut self) -> Result<(), Box<dyn Error>> {
        log::debug!("Connecting to dxfeed");
        let (socket, response) = tungstenite::connect(Url::parse(&self.base_url)?)?;
        log::debug!("Connected to dxfeed: {}", response.status());

        self.socket = Some(socket);
        self.send_message(&format!(
            r#"
[
  {{
    "ext": {{
      "com.devexperts.auth.AuthToken": "{auth_token}"
    }},
    "id": "{id}",
    "version": "1.0",
    "minimumVersion": "1.0",
    "channel": "/meta/handshake",
    "supportedConnectionTypes": [
      "websocket",
      "long-polling",
      "callback-polling"
    ],
    "advice": {{
      "timeout": 60000,
      "interval": 0
    }}
  }}
]
"#,
            id = self.next_message_id,
            auth_token = self.access_token
        ))?;
        self.next_message_id += 1;

        #[derive(Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct DxFeedConnectResponse {
            client_id: String,
        }

        let msg = self.read_message(true)?.ok_or(ClientIdResponseError)?;
        let msg_json = msg.to_text()?;
        let client_id = serde_json::from_str::<Vec<DxFeedConnectResponse>>(msg_json)?
            .drain(..)
            .next()
            .ok_or(ClientIdResponseError)?
            .client_id;

        log::debug!("Parsed dxfeed clientId: {}", client_id);
        self.client_id = Some(client_id);

        Ok(())
    }

    pub fn add_subscription(
        &mut self,
        name: &str,
        symbols: &[String],
    ) -> Result<(), Box<dyn Error>> {
        if self.socket.is_none() {
            return Err(NotConnectedError.into());
        }

        for chunk in symbols.chunks(MAX_SUBSCRIPTION_SIZE) {
            self.send_message(&format!(
                r#"
[
  {{
    "id": "{id}",
    "channel": "/service/sub",
    "data": {{
      "add": {{
        "{name}": [
          {symbols}
        ]
      }}
    }},
    "clientId": "{client_id}"
  }}
]
"#,
                id = self.next_message_id,
                client_id = self.client_id.as_ref().unwrap(),
                name = name,
                symbols = chunk.iter().map(|s| format!("\"{}\"", s)).join(",")
            ))?;
            self.next_message_id += 1;

            // TODO: replace with something more reliable, probably need to handle heartbeat
            std::thread::sleep(std::time::Duration::from_millis(200));
        }

        Ok(())
    }

    pub fn poll_subscriptions(
        &mut self,
    ) -> Result<HashMap<String, SubscriptionData>, Box<dyn Error>> {
        if self.socket.is_none() {
            return Err(NotConnectedError.into());
        }

        let mut subscription_data = HashMap::new();
        while let Some(msg) = self.read_message(false)? {
            let msg_json = msg.to_text()?;
            let result = serde_json::from_str::<Vec<DxFeedData>>(msg_json).unwrap_or_default();

            for data in &result {
                let (name, data_seq) = data.parse_data_seq(&mut self.subscription_fields)?;

                let data = subscription_data
                    .entry(name.to_string())
                    .or_insert(SubscriptionData {
                        subscription_fields: vec![],
                        data_seq: vec![],
                    });

                data.data_seq.append(&mut data_seq.clone());
            }
        }

        for (name, data) in &mut subscription_data {
            data.subscription_fields = self
                .subscription_fields
                .get(name)
                .ok_or_else(|| ResponseParseError("missing subscription fields".to_string()))?
                .clone();
        }

        self.ping()?;

        Ok(subscription_data)
    }

    fn ping(&mut self) -> Result<(), Box<dyn Error>> {
        if self.socket.is_none() {
            return Err(NotConnectedError.into());
        }

        self.send_message(&format!(
            r#"
[
  {{
    "id": "{id}",
    "channel": "/meta/connect",
    "connectionType": "websocket",
    "advice": {{
      "timeout": 0
    }},
    "clientId": "{client_id}"
  }}
]
"#,
            id = self.next_message_id,
            client_id = self.client_id.as_ref().unwrap(),
        ))?;
        self.next_message_id += 1;

        Ok(())
    }

    fn send_message(&mut self, msg: &str) -> Result<(), Box<dyn Error>> {
        let socket = self.socket.as_mut().ok_or(NotConnectedError)?;
        let msg = msg.replace("\n", "").replace(" ", "");
        log::debug!("Sending message: {}", msg);
        socket
            .write_message(tungstenite::Message::Text(msg))
            .map_err(Into::into)
    }

    fn read_message(
        &mut self,
        blocking: bool,
    ) -> Result<Option<tungstenite::Message>, Box<dyn Error>> {
        let socket = self.socket.as_mut().ok_or(NotConnectedError)?;

        // see https://github.com/snapview/tungstenite-rs/issues/103
        let stream = match socket.get_mut() {
            tungstenite::stream::Stream::Plain(stream) => stream,
            tungstenite::stream::Stream::Tls(stream) => stream.get_mut(),
        };
        stream.set_nonblocking(!blocking)?;

        let message = socket.read_message();
        match message {
            Ok(msg) => {
                log::debug!("Received message: {}", msg);
                Ok(Some(msg))
            }
            Err(tungstenite::Error::Io(ref e)) if e.kind() == std::io::ErrorKind::WouldBlock => {
                Ok(None)
            }
            Err(e) => Err(e.into()),
        }
    }
}

#[derive(Debug)]
pub struct SubscriptionData {
    subscription_fields: Vec<String>,
    data_seq: Vec<serde_json::Value>,
}

impl SubscriptionData {
    pub fn iter_field(&self, field: &str) -> impl Iterator<Item = &serde_json::Value> + '_ {
        let index = self
            .subscription_fields
            .iter()
            .position(|f| f == field)
            .unwrap_or_else(|| panic!("Missing index for field: {}", field));

        self.data_seq
            .chunks(self.subscription_fields.len())
            .map(move |chunk| &chunk[index])
    }
}

pub trait SubscriptionValue {
    fn to_price(&self) -> Option<Rational64>;
}

impl SubscriptionValue for serde_json::Value {
    fn to_price(&self) -> Option<Rational64> {
        if let Some("NaN") = self.as_str() {
            None
        } else {
            self.as_f64().and_then(Rational64::approximate_float)
        }
    }
}

#[derive(Debug)]
pub struct Price {
    pub symbol: String,
    pub price: Rational64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DxFeedData {
    data: Vec<serde_json::Value>,
}

impl DxFeedData {
    fn parse_data_seq(
        &self,
        subscription_fields: &mut HashMap<String, Vec<String>>,
    ) -> Result<(String, &Vec<serde_json::Value>), ResponseParseError> {
        if self.data.len() != 2 {
            return Err(ResponseParseError("data length".to_string()));
        }

        // the first data frame includes both the field names and first data sequence.
        // subsequent frames contain the remaining data sequences without field names.
        let has_header = self
            .data
            .get(0)
            .filter(|header| header.is_array())
            .is_some();

        if has_header {
            let name = self
                .data
                .get(0)
                .and_then(|header| header.as_array())
                .and_then(|header| {
                    match (
                        header.get(0).and_then(|v| v.as_str()),
                        header.get(1).and_then(|v| v.as_array()),
                    ) {
                        (Some(name), Some(fields)) => {
                            let mut arr_str = vec![];
                            for v in fields {
                                if let Some(s) = v.as_str() {
                                    arr_str.push(s.to_string());
                                } else {
                                    return None;
                                }
                            }
                            subscription_fields.insert(name.to_string(), arr_str);
                            Some(name)
                        }
                        _ => None,
                    }
                })
                .ok_or_else(|| ResponseParseError("header name".to_string()))?;
            let data_seq = self
                .data
                .get(1)
                .and_then(|seq| seq.as_array())
                .ok_or_else(|| ResponseParseError("header data seq".to_string()))?;

            Ok((name.to_string(), data_seq))
        } else {
            let name = self
                .data
                .get(0)
                .and_then(|name| name.as_str())
                .ok_or_else(|| ResponseParseError("name".to_string()))?;
            let data_seq = self
                .data
                .get(1)
                .and_then(|seq| seq.as_array())
                .ok_or_else(|| ResponseParseError("data seq".to_string()))?;
            Ok((name.to_string(), data_seq))
        }
    }
}

#[derive(Debug, Clone)]
struct ClientIdResponseError;

impl Error for ClientIdResponseError {}

impl fmt::Display for ClientIdResponseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Could not find clientId in response")
    }
}

#[derive(Debug, Clone)]
struct NotConnectedError;

impl Error for NotConnectedError {}

impl fmt::Display for NotConnectedError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "The streamer client is not connected")
    }
}

#[derive(Debug, Clone)]
struct ResponseParseError(String);

impl Error for ResponseParseError {}

impl fmt::Display for ResponseParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Response could not be parsed: {}", self.0)
    }
}