1
+ use core:: fmt;
1
2
use std:: collections:: HashMap ;
3
+ use std:: fmt:: Formatter ;
4
+
5
+ use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
6
+ use serde:: de:: { EnumAccess , Error , MapAccess , SeqAccess , Unexpected , Visitor } ;
7
+ use serde_json:: Value ;
2
8
3
9
use agency_client:: get_message:: { Message , MessageByConnection } ;
4
10
use agency_client:: MessageStatusCode ;
5
11
6
12
use crate :: aries:: handlers:: connection:: cloud_agent:: CloudAgentInfo ;
7
- use crate :: aries:: handlers:: connection:: invitee:: state_machine:: { InviteeFullState , SmConnectionInvitee , InviteeState } ;
8
- use crate :: aries:: handlers:: connection:: inviter:: state_machine:: { InviterFullState , SmConnectionInviter , InviterState } ;
13
+ use crate :: aries:: handlers:: connection:: invitee:: state_machine:: { InviteeFullState , InviteeState , SmConnectionInvitee } ;
14
+ use crate :: aries:: handlers:: connection:: inviter:: state_machine:: { InviterFullState , InviterState , SmConnectionInviter } ;
9
15
use crate :: aries:: handlers:: connection:: legacy_agent_info:: LegacyAgentInfo ;
10
16
use crate :: aries:: handlers:: connection:: pairwise_info:: PairwiseInfo ;
11
17
use crate :: aries:: messages:: a2a:: A2AMessage ;
12
18
use crate :: aries:: messages:: basic_message:: message:: BasicMessage ;
13
19
use crate :: aries:: messages:: connection:: did_doc:: DidDoc ;
14
20
use crate :: aries:: messages:: connection:: invite:: Invitation ;
15
21
use crate :: aries:: messages:: discovery:: disclose:: ProtocolDescriptor ;
22
+ use crate :: aries:: utils:: send_message;
16
23
use crate :: error:: prelude:: * ;
17
24
use crate :: utils:: serialization:: SerializableObjectWithState ;
18
25
19
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
26
+ #[ derive( Clone ) ]
20
27
pub struct Connection {
21
28
connection_sm : SmConnection ,
22
29
cloud_agent_info : CloudAgentInfo ,
23
30
autohop_enabled : bool ,
24
31
}
25
32
26
- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
33
+ #[ derive( Clone ) ]
27
34
pub enum SmConnection {
28
35
Inviter ( SmConnectionInviter ) ,
29
36
Invitee ( SmConnectionInvitee ) ,
@@ -44,7 +51,7 @@ struct ConnectionInfo {
44
51
#[ derive( Debug , PartialEq ) ]
45
52
pub enum ConnectionState {
46
53
Inviter ( InviterState ) ,
47
- Invitee ( InviteeState )
54
+ Invitee ( InviteeState ) ,
48
55
}
49
56
50
57
@@ -73,12 +80,9 @@ impl Connection {
73
80
trace ! ( "Connection::create >>> source_id: {}" , source_id) ;
74
81
let pairwise_info = PairwiseInfo :: create ( ) ?;
75
82
let cloud_agent_info = CloudAgentInfo :: create ( & pairwise_info) ?;
76
- let routing_keys = cloud_agent_info. routing_keys ( ) ?;
77
- let agency_endpoint = cloud_agent_info. service_endpoint ( ) ?;
78
-
79
83
Ok ( Connection {
80
84
cloud_agent_info,
81
- connection_sm : SmConnection :: Inviter ( SmConnectionInviter :: new ( source_id, pairwise_info) ) ,
85
+ connection_sm : SmConnection :: Inviter ( SmConnectionInviter :: new ( source_id, pairwise_info, send_message ) ) ,
82
86
autohop_enabled : autohop,
83
87
} )
84
88
}
@@ -90,12 +94,9 @@ impl Connection {
90
94
trace ! ( "Connection::create_with_invite >>> source_id: {}" , source_id) ;
91
95
let pairwise_info = PairwiseInfo :: create ( ) ?;
92
96
let cloud_agent_info = CloudAgentInfo :: create ( & pairwise_info) ?;
93
- let routing_keys = cloud_agent_info. routing_keys ( ) ?;
94
- let agency_endpoint = cloud_agent_info. service_endpoint ( ) ?;
95
-
96
97
let mut connection = Connection {
97
98
cloud_agent_info,
98
- connection_sm : SmConnection :: Invitee ( SmConnectionInvitee :: new ( source_id, pairwise_info) ) ,
99
+ connection_sm : SmConnection :: Invitee ( SmConnectionInvitee :: new ( source_id, pairwise_info, send_message ) ) ,
99
100
autohop_enabled,
100
101
} ;
101
102
connection. process_invite ( invitation) ?;
@@ -107,14 +108,14 @@ impl Connection {
107
108
SmConnectionState :: Inviter ( state) => {
108
109
Connection {
109
110
cloud_agent_info,
110
- connection_sm : SmConnection :: Inviter ( SmConnectionInviter :: from ( source_id, pairwise_info, state) ) ,
111
+ connection_sm : SmConnection :: Inviter ( SmConnectionInviter :: from ( source_id, pairwise_info, state, send_message ) ) ,
111
112
autohop_enabled,
112
113
}
113
114
}
114
115
SmConnectionState :: Invitee ( state) => {
115
116
Connection {
116
117
cloud_agent_info,
117
- connection_sm : SmConnection :: Invitee ( SmConnectionInvitee :: from ( source_id, pairwise_info, state) ) ,
118
+ connection_sm : SmConnection :: Invitee ( SmConnectionInvitee :: from ( source_id, pairwise_info, state, send_message ) ) ,
118
119
autohop_enabled,
119
120
}
120
121
}
@@ -577,7 +578,7 @@ Get messages received from connection counterparty.
577
578
. ok_or ( VcxError :: from_msg ( VcxErrorKind :: NotReady , "Cannot send message: Remote Connection information is not set" ) ) ?;
578
579
let sender_vk = self . pairwise_info ( ) . pw_vk . clone ( ) ;
579
580
return Ok ( move |a2a_message : & A2AMessage | {
580
- did_doc . send_message ( a2a_message , & sender_vk )
581
+ send_message ( & sender_vk , & did_doc , a2a_message )
581
582
} ) ;
582
583
}
583
584
@@ -677,7 +678,23 @@ Get messages received from connection counterparty.
677
678
Ok ( msgs)
678
679
}
679
680
680
- pub fn to_string ( & self ) -> String {
681
+ pub fn to_string ( & self ) -> VcxResult < String > {
682
+ serde_json:: to_string ( & self )
683
+ . map_err ( |err| VcxError :: from_msg ( VcxErrorKind :: SerializationError , format ! ( "Cannot serialize Connection: {:?}" , err) ) )
684
+ }
685
+
686
+ pub fn from_string ( connection_data : & str ) -> VcxResult < Connection > {
687
+ serde_json:: from_str ( connection_data)
688
+ . map_err ( |err| VcxError :: from_msg ( VcxErrorKind :: InvalidJson , format ! ( "Cannot deserialize Connection: {:?}" , err) ) )
689
+ }
690
+ }
691
+
692
+ impl Serialize for Connection
693
+ {
694
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
695
+ where
696
+ S : Serializer ,
697
+ {
681
698
let ( state, pairwise_info, cloud_agent_info, source_id) = self . to_owned ( ) . into ( ) ;
682
699
let data = LegacyAgentInfo {
683
700
pw_did : pairwise_info. pw_did ,
@@ -686,13 +703,31 @@ Get messages received from connection counterparty.
686
703
agent_vk : cloud_agent_info. agent_vk ,
687
704
} ;
688
705
let object = SerializableObjectWithState :: V1 { data, state, source_id } ;
689
- json ! ( object ) . to_string ( )
706
+ serializer . serialize_some ( & object )
690
707
}
708
+ }
691
709
692
- pub fn from_string ( connection_data : & str ) -> VcxResult < Connection > {
693
- let object: SerializableObjectWithState < LegacyAgentInfo , SmConnectionState > = serde_json:: from_str ( connection_data)
694
- . map_err ( |err| VcxError :: from_msg ( VcxErrorKind :: InvalidJson , format ! ( "Cannot deserialize Connection: {:?}" , err) ) ) ?;
695
- match object {
710
+ struct ConnectionVisitor ;
711
+
712
+ impl < ' de > Visitor < ' de > for ConnectionVisitor {
713
+ type Value = Connection ;
714
+
715
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
716
+ formatter. write_str ( "serialized Connection object" )
717
+ }
718
+
719
+ fn visit_map < A > ( self , mut map : A ) -> Result < Self :: Value , <A as MapAccess < ' de > >:: Error > where
720
+ A : MapAccess < ' de > , {
721
+ let mut map_value = serde_json:: Map :: new ( ) ;
722
+ while let Some ( key) = map. next_key ( ) ? {
723
+ let k: String = key;
724
+ let v: Value = map. next_value ( ) ?;
725
+ map_value. insert ( k, v) ;
726
+ }
727
+ let obj = Value :: from ( map_value) ;
728
+ let ver: SerializableObjectWithState < LegacyAgentInfo , SmConnectionState > = serde_json:: from_value ( obj)
729
+ . map_err ( |err| A :: Error :: custom ( err. to_string ( ) ) ) ?;
730
+ match ver {
696
731
SerializableObjectWithState :: V1 { data, state, source_id } => {
697
732
let pairwise_info = PairwiseInfo { pw_did : data. pw_did , pw_vk : data. pw_vk } ;
698
733
let cloud_agent_info = CloudAgentInfo { agent_did : data. agent_did , agent_vk : data. agent_vk } ;
@@ -702,6 +737,14 @@ Get messages received from connection counterparty.
702
737
}
703
738
}
704
739
740
+ impl < ' de > Deserialize < ' de > for Connection {
741
+ fn deserialize < D > ( deserializer : D ) -> Result < Connection , D :: Error >
742
+ where
743
+ D : Deserializer < ' de > ,
744
+ {
745
+ deserializer. deserialize_map ( ConnectionVisitor )
746
+ }
747
+ }
705
748
706
749
#[ cfg( test) ]
707
750
pub mod tests {
@@ -743,7 +786,7 @@ pub mod tests {
743
786
fn test_deserialize_and_serialize ( sm_serialized : & str ) {
744
787
let original_object: Value = serde_json:: from_str ( sm_serialized) . unwrap ( ) ;
745
788
let connection = Connection :: from_string ( sm_serialized) . unwrap ( ) ;
746
- let reserialized = connection. to_string ( ) ;
789
+ let reserialized = connection. to_string ( ) . unwrap ( ) ;
747
790
let reserialized_object: Value = serde_json:: from_str ( & reserialized) . unwrap ( ) ;
748
791
749
792
assert_eq ! ( original_object, reserialized_object) ;
@@ -766,14 +809,28 @@ pub mod tests {
766
809
let _setup = SetupMocks :: init ( ) ;
767
810
768
811
let connection = Connection :: create ( "test_serialize_deserialize" , true ) . unwrap ( ) ;
769
- let first_string = connection. to_string ( ) ;
812
+ let first_string = connection. to_string ( ) . unwrap ( ) ;
770
813
771
814
let connection2 = Connection :: from_string ( & first_string) . unwrap ( ) ;
772
- let second_string = connection2. to_string ( ) ;
815
+ let second_string = connection2. to_string ( ) . unwrap ( ) ;
773
816
774
817
assert_eq ! ( first_string, second_string) ;
775
818
}
776
819
820
+ #[ test]
821
+ #[ cfg( feature = "general_test" ) ]
822
+ fn test_serialize_deserialize_serde ( ) {
823
+ let _setup = SetupMocks :: init ( ) ;
824
+
825
+ let connection = Connection :: create ( "test_serialize_deserialize" , true ) . unwrap ( ) ;
826
+ let first_string = serde_json:: to_string ( & connection) . unwrap ( ) ;
827
+
828
+ let connection: Connection = serde_json:: from_str ( & first_string) . unwrap ( ) ;
829
+ let second_string = serde_json:: to_string ( & connection) . unwrap ( ) ;
830
+ assert_eq ! ( first_string, second_string) ;
831
+ }
832
+
833
+
777
834
pub fn create_connected_connections ( consumer : & mut Alice , institution : & mut Faber ) -> ( Connection , Connection ) {
778
835
debug ! ( "Institution is going to create connection." ) ;
779
836
institution. activate ( ) . unwrap ( ) ;
0 commit comments