Skip to content

Commit 16266b2

Browse files
committed
Notify application on state changes
1 parent a53736e commit 16266b2

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

crates/hotfix/src/application.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::message::OutboundMessage;
2+
use crate::session::Status;
23
use hotfix_message::message::Message;
34

45
#[async_trait::async_trait]
@@ -18,6 +19,11 @@ pub trait Application: Send + Sync + 'static {
1819
async fn on_logout(&mut self, reason: &str);
1920
/// Called when the session is logged on.
2021
async fn on_logon(&mut self);
22+
/// Called when the session state changes.
23+
///
24+
/// This is invoked after every state transition, providing the previous
25+
/// and new status. The default implementation does nothing.
26+
async fn on_state_change(&self, from: &Status, to: &Status);
2127
}
2228

2329
/// Standard FIX Business Reject Reason values (tag 380).

crates/hotfix/src/session.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,15 @@ where
482482

483483
async fn apply_transition(&mut self, result: TransitionResult) {
484484
if let TransitionResult::TransitionTo(new_state) = result {
485+
let old_status = self.state.as_status();
485486
self.state = new_state;
487+
let new_status = self.state.as_status();
488+
if old_status != new_status {
489+
self.ctx
490+
.application
491+
.on_state_change(&old_status, &new_status)
492+
.await;
493+
}
486494
}
487495
}
488496

examples/load-testing/src/application.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::messages::{ExecutionReport, InboundMsg, OutboundMsg};
22
use hotfix::Application;
33
use hotfix::Message;
44
use hotfix::application::{InboundDecision, OutboundDecision};
5+
use hotfix::session::Status;
56
use tokio::sync::mpsc::UnboundedSender;
67
use tracing::info;
78

@@ -51,4 +52,8 @@ impl Application for LoadTestingApplication {
5152
async fn on_logon(&mut self) {
5253
info!("we've been logged in");
5354
}
55+
56+
async fn on_state_change(&self, from: &Status, to: &Status) {
57+
info!("we've changed from {:?} to {:?}", from, to);
58+
}
5459
}

examples/order-entry/src/application.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::sync::{Arc, Mutex};
22

3+
use crate::messages::OutboundMsg;
34
use hotfix::Application;
45
use hotfix::Message;
56
use hotfix::application::{InboundDecision, OutboundDecision};
67
use hotfix::message::OutboundMessage;
8+
use hotfix::session::Status;
79
use hotfix_message::message::Config as EncodeConfig;
810
use serde::Serialize;
911
use tracing::info;
1012

11-
use crate::messages::OutboundMsg;
12-
1313
#[derive(Clone, Serialize)]
1414
pub struct MessageLogEntry {
1515
pub id: u64,
@@ -91,4 +91,8 @@ impl Application for TestApplication {
9191
async fn on_logon(&mut self) {
9292
info!("we've been logged in");
9393
}
94+
95+
async fn on_state_change(&self, from: &Status, to: &Status) {
96+
info!("we've changed from {:?} to {:?}", from, to);
97+
}
9498
}

0 commit comments

Comments
 (0)