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
use std::task::Poll;
use std::ops::Drop;

use futures::{Stream, StreamExt};
use ib_tws_core::CommandChannel;
use ib_tws_core::message::response::*;
use ib_tws_core::message::request::*;

#[derive(Debug)]
pub struct TwsClient {
    pub channel: CommandChannel<Request, Response>,
    pub server_version: i32,
    //pub account: String,
    //pub next_valid_id: i32,
}

impl TwsClient {
    pub fn send_request(&self, req: Request) {
        trace!(?req, "sending request");
        let _ = self.channel.tx.unbounded_send(req);
    }
}

impl Stream for TwsClient {
    type Item = Response;

    fn poll_next(mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Option<Self::Item>> {
        self.channel.rx.poll_next_unpin(cx)
    }
}

/*impl Sink for TwsClient {
    type SinkItem = Request;
    type SinkError = mpsc::SendError<Self::SinkItem>;

    fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
        self.channel.tx.start_send(item)
    }

    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        self.channel.tx.poll_complete()
    }

    fn close(&mut self) -> Poll<(), Self::SinkError> {
        self.channel.tx.close()
    }
}*/

impl Drop for TwsClient {
    fn drop(&mut self) {
        trace!("drop client");
    }
}