1- use crate :: { Error , Result } ;
1+ use crate :: {
2+ transaction:: { key:: TransactionRole , transaction:: Transaction } ,
3+ Error , Result ,
4+ } ;
25use rsip:: {
36 prelude:: { HeadersExt , UntypedHeader } ,
47 Request , Response ,
@@ -47,46 +50,17 @@ mod tests;
4750/// - During early dialog establishment, `to_tag` may be an empty string
4851/// - Dialog ID remains constant throughout the dialog lifetime
4952/// - Used for managing and routing SIP messages at the dialog layer
50- #[ derive( Clone , Debug ) ]
53+ #[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
5154pub struct DialogId {
5255 pub call_id : String ,
53- pub from_tag : String ,
54- pub to_tag : String ,
55- }
56-
57- impl PartialEq for DialogId {
58- fn eq ( & self , other : & DialogId ) -> bool {
59- if self . call_id != other. call_id {
60- return false ;
61- }
62- if self . from_tag == other. from_tag && self . to_tag == other. to_tag {
63- return true ;
64- }
65- if self . from_tag == other. to_tag && self . to_tag == other. from_tag {
66- return true ;
67- }
68- false
69- }
56+ pub local_tag : String ,
57+ pub remote_tag : String ,
7058}
7159
72- impl Eq for DialogId { }
73- impl std:: hash:: Hash for DialogId {
74- fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
75- self . call_id . hash ( state) ;
76- if self . from_tag > self . to_tag {
77- self . from_tag . hash ( state) ;
78- self . to_tag . hash ( state) ;
79- } else {
80- self . to_tag . hash ( state) ;
81- self . from_tag . hash ( state) ;
82- }
83- }
84- }
85-
86- impl TryFrom < & Request > for DialogId {
60+ impl TryFrom < ( & Request , TransactionRole ) > for DialogId {
8761 type Error = crate :: Error ;
8862
89- fn try_from ( request : & Request ) -> Result < Self > {
63+ fn try_from ( ( request, direction ) : ( & Request , TransactionRole ) ) -> Result < Self > {
9064 let call_id = request. call_id_header ( ) ?. value ( ) . to_string ( ) ;
9165
9266 let from_tag = match request. from_header ( ) ?. tag ( ) ? {
@@ -99,18 +73,25 @@ impl TryFrom<&Request> for DialogId {
9973 None => "" . to_string ( ) ,
10074 } ;
10175
102- Ok ( DialogId {
103- call_id,
104- from_tag,
105- to_tag,
106- } )
76+ match direction {
77+ TransactionRole :: Client => Ok ( DialogId {
78+ call_id,
79+ local_tag : from_tag,
80+ remote_tag : to_tag,
81+ } ) ,
82+ TransactionRole :: Server => Ok ( DialogId {
83+ call_id,
84+ local_tag : to_tag,
85+ remote_tag : from_tag,
86+ } ) ,
87+ }
10788 }
10889}
10990
110- impl TryFrom < & Response > for DialogId {
91+ impl TryFrom < ( & Response , TransactionRole ) > for DialogId {
11192 type Error = crate :: Error ;
11293
113- fn try_from ( resp : & Response ) -> Result < Self > {
94+ fn try_from ( ( resp, direction ) : ( & Response , TransactionRole ) ) -> Result < Self > {
11495 let call_id = resp. call_id_header ( ) ?. value ( ) . to_string ( ) ;
11596
11697 let from_tag = match resp. from_header ( ) ?. tag ( ) ? {
@@ -123,24 +104,31 @@ impl TryFrom<&Response> for DialogId {
123104 None => return Err ( Error :: Error ( "to tag not found" . to_string ( ) ) ) ,
124105 } ;
125106
126- Ok ( DialogId {
127- call_id,
128- from_tag,
129- to_tag,
130- } )
107+ match direction {
108+ TransactionRole :: Client => Ok ( DialogId {
109+ call_id,
110+ local_tag : from_tag,
111+ remote_tag : to_tag,
112+ } ) ,
113+ TransactionRole :: Server => Ok ( DialogId {
114+ call_id,
115+ local_tag : to_tag,
116+ remote_tag : from_tag,
117+ } ) ,
118+ }
119+ }
120+ }
121+
122+ impl TryFrom < & Transaction > for DialogId {
123+ type Error = crate :: Error ;
124+
125+ fn try_from ( value : & Transaction ) -> std:: result:: Result < Self , Self :: Error > {
126+ DialogId :: try_from ( ( & value. original , value. role ( ) ) )
131127 }
132128}
133129
134130impl std:: fmt:: Display for DialogId {
135131 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
136- if self . from_tag > self . to_tag {
137- if self . to_tag . is_empty ( ) {
138- write ! ( f, "{}-{}" , self . call_id, self . from_tag)
139- } else {
140- write ! ( f, "{}-{}-{}" , self . call_id, self . from_tag, self . to_tag)
141- }
142- } else {
143- write ! ( f, "{}-{}-{}" , self . call_id, self . to_tag, self . from_tag)
144- }
132+ write ! ( f, "{}-{}-{}" , self . call_id, self . local_tag, self . remote_tag)
145133 }
146134}
0 commit comments