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
use crateMessageRejectErrorResult;
use crateMessage;
use crateSessionID;
use SimpleResult;
use Arc;
/// The primary interface for a FIX application.
///
/// Implement this trait to handle session lifecycle events and incoming/outgoing
/// FIX messages. An `Application` is shared across all sessions managed by an
/// [`Acceptor`](crate::acceptor::Acceptor) or
/// [`Initiator`](crate::initiator::Initiator).
///
/// # Threading Model
///
/// All callbacks take `&self` (not `&mut self`). The engine calls your
/// application from the session's run loop, so callbacks are **synchronous** —
/// you cannot `.await` inside them. If you need to perform async work (such as
/// calling [`send_to_target`](crate::registry::send_to_target)), queue the work
/// onto a channel and process it in a background task. See the `echo_server`
/// and `trade_client` examples for this pattern.
///
/// If you need mutable state, use interior mutability (`Mutex`, `RwLock`,
/// `AtomicXxx`, etc.).
///
/// # Callback Sequence
///
/// For a typical session the callbacks fire in this order:
///
/// 1. [`on_create`](Application::on_create) — session object created (not yet connected)
/// 2. [`to_admin`](Application::to_admin) — outgoing Logon about to be sent
/// 3. [`from_admin`](Application::from_admin) — incoming Logon received
/// 4. [`on_logon`](Application::on_logon) — logon complete, safe to send messages
/// 5. [`from_app`](Application::from_app) / [`to_app`](Application::to_app) — application messages flow
/// 6. [`on_logout`](Application::on_logout) — session disconnected or logged out
///
/// # Example
///
/// ```rust
/// use fixer::application::Application;
/// use fixer::errors::MessageRejectErrorResult;
/// use fixer::message::Message;
/// use fixer::session::session_id::SessionID;
/// use simple_error::SimpleResult;
/// use std::sync::Arc;
///
/// struct MyApp;
///
/// impl Application for MyApp {
/// fn on_create(&self, session_id: &Arc<SessionID>) {
/// println!("Session created: {session_id}");
/// }
/// fn on_logon(&self, session_id: &Arc<SessionID>) {
/// println!("Logon: {session_id}");
/// }
/// fn on_logout(&self, session_id: &Arc<SessionID>) {
/// println!("Logout: {session_id}");
/// }
/// fn to_admin(&self, _msg: &mut Message, _session_id: &Arc<SessionID>) {}
/// fn to_app(
/// &self, _msg: &mut Message, _session_id: &Arc<SessionID>,
/// ) -> SimpleResult<()> {
/// Ok(())
/// }
/// fn from_admin(
/// &self, _msg: &Message, _session_id: &Arc<SessionID>,
/// ) -> MessageRejectErrorResult {
/// Ok(())
/// }
/// fn from_app(
/// &self, _msg: &Message, _session_id: &Arc<SessionID>,
/// ) -> MessageRejectErrorResult {
/// println!("Received application message");
/// Ok(())
/// }
/// }
/// ```