@@ -3,6 +3,7 @@ use super::header::{decode, encode, match_field};
33use super :: { Message , Topic } ;
44use crate :: rosmsg:: RosMsg ;
55use crate :: util:: lossy_channel:: { lossy_channel, LossyReceiver , LossySender } ;
6+ use crate :: SubscriptionHandler ;
67use byteorder:: { LittleEndian , ReadBytesExt , WriteBytesExt } ;
78use crossbeam:: channel:: { bounded, select, Receiver , Sender , TrySendError } ;
89use log:: error;
@@ -77,16 +78,10 @@ impl SubscriberRosConnection {
7778 // This creates a new thread to call on_message. Next API change should
7879 // allow subscribing with either callback or inline handler of the queue.
7980 // The queue is lossy, so it wouldn't be blocking.
80- pub fn add_subscriber < T , F , G > (
81- & mut self ,
82- queue_size : usize ,
83- on_message : F ,
84- on_connect : G ,
85- ) -> usize
81+ pub fn add_subscriber < T , H > ( & mut self , queue_size : usize , handler : H ) -> usize
8682 where
8783 T : Message ,
88- F : Fn ( T , & str ) + Send + ' static ,
89- G : Fn ( HashMap < String , String > ) + Send + ' static ,
84+ H : SubscriptionHandler < T > ,
9085 {
9186 let data_stream_id = self . next_data_stream_id ;
9287 self . connected_ids . insert ( data_stream_id) ;
@@ -105,9 +100,7 @@ impl SubscriberRosConnection {
105100 // TODO: we might want to panic here
106101 error ! ( "Subscriber failed to connect to data stream" ) ;
107102 }
108- thread:: spawn ( move || {
109- handle_data :: < T , F , G > ( data_rx, connection_rx, on_message, on_connect)
110- } ) ;
103+ thread:: spawn ( move || handle_data :: < T , H > ( data_rx, connection_rx, handler) ) ;
111104 data_stream_id
112105 }
113106
@@ -176,29 +169,27 @@ impl SubscriberRosConnection {
176169 }
177170}
178171
179- fn handle_data < T , F , G > (
172+ fn handle_data < T , H > (
180173 data : LossyReceiver < MessageInfo > ,
181174 connections : Receiver < HashMap < String , String > > ,
182- on_message : F ,
183- on_connect : G ,
175+ mut handler : H ,
184176) where
185177 T : Message ,
186- F : Fn ( T , & str ) ,
187- G : Fn ( HashMap < String , String > ) + Send + ' static ,
178+ H : SubscriptionHandler < T > ,
188179{
189180 loop {
190181 select ! {
191182 recv( data. kill_rx. kill_rx) -> _ => break ,
192183 recv( data. data_rx) -> msg => match msg {
193184 Err ( _) => break ,
194185 Ok ( buffer) => match RosMsg :: decode_slice( & buffer. data) {
195- Ok ( value) => on_message ( value, & buffer. caller_id) ,
186+ Ok ( value) => handler . message ( value, & buffer. caller_id) ,
196187 Err ( err) => error!( "Failed to decode message: {}" , err) ,
197188 } ,
198189 } ,
199190 recv( connections) -> msg => match msg {
200191 Err ( _) => break ,
201- Ok ( conn) => on_connect ( conn) ,
192+ Ok ( conn) => handler . connection ( conn) ,
202193 } ,
203194 }
204195 }
0 commit comments