1+ use std:: collections:: btree_map:: Entry ;
2+
13use binprot:: BinProtRead ;
24use mina_p2p_messages:: { gossip, v2} ;
35
46use super :: {
57 p2p_network_pubsub_state:: P2pNetworkPubsubClientMeshAddingState , pb, P2pNetworkPubsubAction ,
6- P2pNetworkPubsubClientMeshState , P2pNetworkPubsubClientState , P2pNetworkPubsubState ,
8+ P2pNetworkPubsubClientState , P2pNetworkPubsubClientTopicState , P2pNetworkPubsubState ,
79} ;
810
911impl P2pNetworkPubsubState {
@@ -16,20 +18,23 @@ impl P2pNetworkPubsubState {
1618 protocol,
1719 ..
1820 } => {
19- let state =
20- self . clients
21- . entry ( * peer_id)
22- . or_insert_with ( || P2pNetworkPubsubClientState {
23- protocol : * protocol,
24- addr : * addr,
25- outgoing_stream_id : None ,
26- message : pb:: Rpc {
27- subscriptions : vec ! [ ] ,
28- publish : vec ! [ ] ,
29- control : None ,
30- } ,
31- buffer : vec ! [ ] ,
32- } ) ;
21+ let entry = self . clients . entry ( * peer_id) ;
22+ // preserve it
23+ let outgoing_stream_id = match & entry {
24+ Entry :: Occupied ( v) => v. get ( ) . outgoing_stream_id ,
25+ Entry :: Vacant ( _) => None ,
26+ } ;
27+ let state = entry. or_insert_with ( || P2pNetworkPubsubClientState {
28+ protocol : * protocol,
29+ addr : * addr,
30+ outgoing_stream_id,
31+ message : pb:: Rpc {
32+ subscriptions : vec ! [ ] ,
33+ publish : vec ! [ ] ,
34+ control : None ,
35+ } ,
36+ buffer : vec ! [ ] ,
37+ } ) ;
3338 state. protocol = * protocol;
3439 state. addr = * addr;
3540 }
@@ -83,10 +88,9 @@ impl P2pNetworkPubsubState {
8388 let topic = self . topics . entry ( topic_id) . or_default ( ) ;
8489
8590 if subscription. subscribe ( ) {
86- topic. insert (
87- peer_id. clone ( ) ,
88- P2pNetworkPubsubClientMeshState :: default ( ) ,
89- ) ;
91+ if let Entry :: Vacant ( v) = topic. entry ( peer_id. clone ( ) ) {
92+ v. insert ( P2pNetworkPubsubClientTopicState :: default ( ) ) ;
93+ }
9094 } else {
9195 topic. remove ( & peer_id) ;
9296 }
@@ -111,9 +115,10 @@ impl P2pNetworkPubsubState {
111115 . filter ( |( c, _) | {
112116 // don't send back to who sent this
113117 * * c != * peer_id
114- && topic
115- . get ( c)
116- . map_or ( false , P2pNetworkPubsubClientMeshState :: on_mesh)
118+ && topic. get ( c) . map_or (
119+ false ,
120+ P2pNetworkPubsubClientTopicState :: on_mesh,
121+ )
117122 } )
118123 . for_each ( |( _, state) | state. message . publish . push ( message. clone ( ) ) ) ;
119124
@@ -155,7 +160,6 @@ impl P2pNetworkPubsubState {
155160 . and_then ( |m| m. get_mut ( & peer_id) )
156161 {
157162 mesh_state. mesh = P2pNetworkPubsubClientMeshAddingState :: Added ;
158- // TODO: prune if above the limit
159163 }
160164 }
161165 for prune in & control. prune {
@@ -192,6 +196,17 @@ impl P2pNetworkPubsubState {
192196
193197 state. mesh = P2pNetworkPubsubClientMeshAddingState :: Added ;
194198 }
199+ P2pNetworkPubsubAction :: Prune { peer_id, topic_id } => {
200+ let Some ( state) = self
201+ . topics
202+ . get_mut ( topic_id)
203+ . and_then ( |m| m. get_mut ( peer_id) )
204+ else {
205+ return ;
206+ } ;
207+
208+ state. mesh = P2pNetworkPubsubClientMeshAddingState :: WeRefused ;
209+ }
195210 P2pNetworkPubsubAction :: OutgoingMessage { peer_id, .. } => {
196211 if let Some ( v) = self . clients . get_mut ( peer_id) {
197212 v. message . subscriptions . clear ( ) ;
@@ -227,7 +242,7 @@ impl P2pNetworkPubsubState {
227242 . filter ( |( c, _) | {
228243 topic
229244 . get ( c)
230- . map_or ( false , P2pNetworkPubsubClientMeshState :: on_mesh)
245+ . map_or ( false , P2pNetworkPubsubClientTopicState :: on_mesh)
231246 } )
232247 . for_each ( |( _, state) | state. message . publish . push ( message. clone ( ) ) ) ;
233248 }
0 commit comments