Skip to content

Commit 62db964

Browse files
committed
refactor: rename inspector to message_inspector and add transport_event_inspector trait
1 parent 9180764 commit 62db964

File tree

3 files changed

+68
-38
lines changed

3 files changed

+68
-38
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rsipstack"
3-
version = "0.2.82"
3+
version = "0.2.83"
44
edition = "2021"
55
description = "SIP Stack Rust library for building SIP applications"
66
license = "MIT"

src/transaction/endpoint.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ pub trait TargetLocator: Send + Sync {
3434
async fn locate(&self, uri: &rsip::Uri) -> Result<SipAddr>;
3535
}
3636

37+
#[async_trait]
38+
pub trait TransportEventInspector: Send + Sync {
39+
async fn handle(&self, event: &TransportEvent);
40+
}
41+
3742
pub struct EndpointOption {
3843
pub t1: Duration,
3944
pub t4: Duration,
@@ -104,8 +109,9 @@ pub struct EndpointInner {
104109
incoming_receiver: Mutex<Option<TransactionReceiver>>,
105110
cancel_token: CancellationToken,
106111
timer_interval: Duration,
107-
pub(super) inspector: Option<Box<dyn MessageInspector>>,
112+
pub(super) message_inspector: Option<Box<dyn MessageInspector>>,
108113
pub(super) locator: Option<Box<dyn TargetLocator>>,
114+
pub(super) transport_inspector: Option<Box<dyn TransportEventInspector>>,
109115
pub option: EndpointOption,
110116
}
111117
pub type EndpointInnerRef = Arc<EndpointInner>;
@@ -135,8 +141,9 @@ pub struct EndpointBuilder {
135141
cancel_token: Option<CancellationToken>,
136142
timer_interval: Option<Duration>,
137143
option: Option<EndpointOption>,
138-
inspector: Option<Box<dyn MessageInspector>>,
144+
message_inspector: Option<Box<dyn MessageInspector>>,
139145
target_locator: Option<Box<dyn TargetLocator>>,
146+
transport_inspector: Option<Box<dyn TransportEventInspector>>,
140147
}
141148

142149
/// SIP Endpoint
@@ -202,8 +209,9 @@ impl EndpointInner {
202209
timer_interval: Option<Duration>,
203210
allows: Vec<rsip::Method>,
204211
option: Option<EndpointOption>,
205-
inspector: Option<Box<dyn MessageInspector>>,
212+
message_inspector: Option<Box<dyn MessageInspector>>,
206213
locator: Option<Box<dyn TargetLocator>>,
214+
transport_inspector: Option<Box<dyn TransportEventInspector>>,
207215
) -> Arc<Self> {
208216
let (incoming_sender, incoming_receiver) = unbounded_channel();
209217
Arc::new(EndpointInner {
@@ -219,8 +227,9 @@ impl EndpointInner {
219227
incoming_sender,
220228
incoming_receiver: Mutex::new(Some(incoming_receiver)),
221229
option: option.unwrap_or_default(),
222-
inspector,
230+
message_inspector,
223231
locator,
232+
transport_inspector,
224233
})
225234
}
226235

@@ -257,6 +266,10 @@ impl EndpointInner {
257266
};
258267

259268
while let Some(event) = transport_rx.recv().await {
269+
if let Some(transport_inspector) = &self.transport_inspector {
270+
transport_inspector.handle(&event).await;
271+
}
272+
260273
match event {
261274
TransportEvent::Incoming(msg, connection, from) => {
262275
match self.on_received_message(msg, connection).await {
@@ -400,7 +413,7 @@ impl EndpointInner {
400413
}
401414
};
402415

403-
let msg = if let Some(inspector) = &self.inspector {
416+
let msg = if let Some(inspector) = &self.message_inspector {
404417
inspector.after_received(msg)
405418
} else {
406419
msg
@@ -429,7 +442,7 @@ impl EndpointInner {
429442
rsip::StatusCode::CallTransactionDoesNotExist,
430443
None,
431444
);
432-
let resp = if let Some(ref inspector) = self.inspector {
445+
let resp = if let Some(ref inspector) = self.message_inspector {
433446
inspector.before_send(resp.into())
434447
} else {
435448
resp.into()
@@ -558,8 +571,9 @@ impl EndpointBuilder {
558571
cancel_token: None,
559572
timer_interval: None,
560573
option: None,
561-
inspector: None,
574+
message_inspector: None,
562575
target_locator: None,
576+
transport_inspector: None,
563577
}
564578
}
565579
pub fn with_option(&mut self, option: EndpointOption) -> &mut Self {
@@ -590,13 +604,22 @@ impl EndpointBuilder {
590604
self
591605
}
592606
pub fn with_inspector(&mut self, inspector: Box<dyn MessageInspector>) -> &mut Self {
593-
self.inspector = Some(inspector);
607+
self.message_inspector = Some(inspector);
594608
self
595609
}
596610
pub fn with_target_locator(&mut self, locator: Box<dyn TargetLocator>) -> &mut Self {
597611
self.target_locator = Some(locator);
598612
self
599613
}
614+
615+
pub fn with_transport_inspector(
616+
&mut self,
617+
inspector: Box<dyn TransportEventInspector>,
618+
) -> &mut Self {
619+
self.transport_inspector = Some(inspector);
620+
self
621+
}
622+
600623
pub fn build(&mut self) -> Endpoint {
601624
let cancel_token = self.cancel_token.take().unwrap_or_default();
602625

@@ -609,8 +632,9 @@ impl EndpointBuilder {
609632
let user_agent = self.user_agent.to_owned();
610633
let timer_interval = self.timer_interval.to_owned();
611634
let option = self.option.take();
612-
let inspector = self.inspector.take();
635+
let message_inspector = self.message_inspector.take();
613636
let locator = self.target_locator.take();
637+
let transport_inspector = self.transport_inspector.take();
614638

615639
let core = EndpointInner::new(
616640
user_agent,
@@ -619,8 +643,9 @@ impl EndpointBuilder {
619643
timer_interval,
620644
allows,
621645
option,
622-
inspector,
646+
message_inspector,
623647
locator,
648+
transport_inspector,
624649
);
625650

626651
Endpoint { inner: core }

src/transaction/transaction.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl Transaction {
283283
.headers_mut()
284284
.unique_push(content_length_header);
285285

286-
let message = if let Some(ref inspector) = self.endpoint_inner.inspector {
286+
let message = if let Some(ref inspector) = self.endpoint_inner.message_inspector {
287287
inspector.before_send(self.original.to_owned().into())
288288
} else {
289289
self.original.to_owned().into()
@@ -350,7 +350,7 @@ impl Transaction {
350350
self.key.clone(),
351351
))?;
352352

353-
let response = if let Some(ref inspector) = self.endpoint_inner.inspector {
353+
let response = if let Some(ref inspector) = self.endpoint_inner.message_inspector {
354354
inspector.before_send(response.clone().to_owned().into())
355355
} else {
356356
response.to_owned().into()
@@ -408,7 +408,8 @@ impl Transaction {
408408
match self.state {
409409
TransactionState::Calling | TransactionState::Trying | TransactionState::Proceeding => {
410410
if let Some(connection) = &self.connection {
411-
let cancel = if let Some(ref inspector) = self.endpoint_inner.inspector {
411+
let cancel = if let Some(ref inspector) = self.endpoint_inner.message_inspector
412+
{
412413
inspector.before_send(cancel.to_owned().into())
413414
} else {
414415
cancel.to_owned().into()
@@ -457,7 +458,7 @@ impl Transaction {
457458
},
458459
};
459460

460-
let ack = if let Some(ref inspector) = self.endpoint_inner.inspector {
461+
let ack = if let Some(ref inspector) = self.endpoint_inner.message_inspector {
461462
inspector.before_send(ack.to_owned().into())
462463
} else {
463464
ack.to_owned().into()
@@ -487,7 +488,7 @@ impl Transaction {
487488
self.on_received_response(resp, connection).await
488489
}
489490
} {
490-
if let Some(ref inspector) = self.endpoint_inner.inspector {
491+
if let Some(ref inspector) = self.endpoint_inner.message_inspector {
491492
return Some(inspector.after_received(msg));
492493
}
493494
return Some(msg);
@@ -553,11 +554,12 @@ impl Transaction {
553554
.endpoint_inner
554555
.make_response(&req, StatusCode::OK, None);
555556

556-
let resp = if let Some(ref inspector) = self.endpoint_inner.inspector {
557-
inspector.before_send(resp.into())
558-
} else {
559-
resp.into()
560-
};
557+
let resp =
558+
if let Some(ref inspector) = self.endpoint_inner.message_inspector {
559+
inspector.before_send(resp.into())
560+
} else {
561+
resp.into()
562+
};
561563

562564
connection.send(resp, self.destination.as_ref()).await.ok();
563565
}
@@ -570,11 +572,12 @@ impl Transaction {
570572
StatusCode::CallTransactionDoesNotExist,
571573
None,
572574
);
573-
let resp = if let Some(ref inspector) = self.endpoint_inner.inspector {
574-
inspector.before_send(resp.into())
575-
} else {
576-
resp.into()
577-
};
575+
let resp =
576+
if let Some(ref inspector) = self.endpoint_inner.message_inspector {
577+
inspector.before_send(resp.into())
578+
} else {
579+
resp.into()
580+
};
578581
connection.send(resp, self.destination.as_ref()).await.ok();
579582
}
580583
}
@@ -648,12 +651,13 @@ impl Transaction {
648651
if let TransactionTimer::TimerA(key, duration) = timer {
649652
// Resend the INVITE request
650653
if let Some(connection) = &self.connection {
651-
let retry_message =
652-
if let Some(ref inspector) = self.endpoint_inner.inspector {
653-
inspector.before_send(self.original.to_owned().into())
654-
} else {
655-
self.original.to_owned().into()
656-
};
654+
let retry_message = if let Some(ref inspector) =
655+
self.endpoint_inner.message_inspector
656+
{
657+
inspector.before_send(self.original.to_owned().into())
658+
} else {
659+
self.original.to_owned().into()
660+
};
657661
connection
658662
.send(retry_message, self.destination.as_ref())
659663
.await?;
@@ -691,12 +695,13 @@ impl Transaction {
691695
// resend the response
692696
if let Some(last_response) = &self.last_response {
693697
if let Some(connection) = &self.connection {
694-
let last_response =
695-
if let Some(ref inspector) = self.endpoint_inner.inspector {
696-
inspector.before_send(last_response.to_owned().into())
697-
} else {
698-
last_response.to_owned().into()
699-
};
698+
let last_response = if let Some(ref inspector) =
699+
self.endpoint_inner.message_inspector
700+
{
701+
inspector.before_send(last_response.to_owned().into())
702+
} else {
703+
last_response.to_owned().into()
704+
};
700705
connection
701706
.send(last_response, self.destination.as_ref())
702707
.await?;

0 commit comments

Comments
 (0)