use rtp::trader::{TraderSpi};
use rtp::trader::{DisconnectionReason, RspResult};
use rtp::trader::{
CThostFtdcRspAuthenticateField, CThostFtdcRspUserLoginField
};
use rtp::binding::TThostFtdcRequestIDType;
struct AtpTraderSpi {
connected: bool,
authenticated: bool,
login_success: bool,
}
impl AtpTraderSpi {
fn new() -> Self {
AtpTraderSpi {
connected: false,
authenticated: false,
login_success: false,
}
}
}
impl TraderSpi for AtpTraderSpi {
fn on_front_connected(&mut self) {
println!("ATP API: Connected to trading server");
self.connected = true;
}
fn on_front_disconnected(&mut self, reason: DisconnectionReason) {
println!("ATP API: Disconnected from trading server: {:?}", reason);
self.connected = false;
}
fn on_rsp_authenticate(
&mut self,
_rsp_authenticate: Option<&CThostFtdcRspAuthenticateField>,
result: RspResult,
_request_id: TThostFtdcRequestIDType,
_is_last: bool,
) {
match result {
Ok(()) => {
println!("Authentication successful");
},
Err(e) => {
println!("Authentication failed: [{0}] {1}", e.id, e.msg);
}
}
}
fn on_rsp_user_login(
&mut self,
login_field: Option<&CThostFtdcRspUserLoginField>,
result: RspResult,
_request_id: TThostFtdcRequestIDType,
_is_last: bool,
) {
match result {
Ok(()) => {
let trading_day = rtp::trader::gb18030_cstr_to_str(&login_field.unwrap().TradingDay);
println!("Login successful! Trading day: {}", trading_day);
},
Err(e) => {
println!("Login failed: [{0}] {1}", e.id, e.msg);
}
}
}
}
#[cfg(test)]
#[cfg(feature = "atp")]
mod tests {
use std::ffi::CString;
use std::thread;
use std::time::Duration;
use rtp::trader::{TraderApi, ResumeType};
use super::*;
#[test]
#[ignore] fn test_atp_basic_connection() {
let flow_path = CString::new("./flow_path_atp").unwrap();
let mut trader = TraderApi::new(flow_path);
let version = trader.version();
println!("ATP API Version: {}", version);
let trader_spi = Box::new(AtpTraderSpi::new());
trader.register_spi(trader_spi);
trader.subscribe_private_topic(ResumeType::Resume);
trader.subscribe_public_topic(ResumeType::Resume);
trader.init();
println!("ATP Trader API initialized");
thread::sleep(Duration::from_millis(500));
println!("ATP test completed");
}
}