fn listen(
    ctxt: zmq::Context,
    ep: String,
    rcp: sync::Arc<sync::Mutex<Vec<String>>>,
    msg_rcp: sync::Arc<sync::Mutex<Vec<String>>>,
) {
    let mut done: bool = false;
    let list: zmq::Socket = ctxt.socket(zmq::REP).unwrap();
    list.bind(&ep).expect("Could not bind...");

    while !done {
        let msg: zmq::Message = list.recv_msg(0).unwrap();
        let tmp: String = msg.as_str().unwrap().to_string();
        list.send("0", 0).unwrap();

        let split = tmp.split('@');
        let data = split.collect::<Vec<&str>>();

        if data[0] == "P" {
            let mut peers: sync::MutexGuard<Vec<String>> = rcp.lock().unwrap();
            for peer in data[1].to_string().split(',') {
                if !peers.contains(&peer.to_string()) {
                    peers.push(peer.to_string());
                }
            }
            drop(peers);
        } else if data[0] == "M" {
            let mut msgs: sync::MutexGuard<Vec<String>> = msg_rcp.lock().unwrap();
            msgs.push(String::from(data[1]));
            drop(msgs);
        } else if data[0] == "K" {
            let mut peers: sync::MutexGuard<Vec<String>> = rcp.lock().unwrap();
            peers.clear();
            drop(peers);
            done = true;
        }
    }
}
/// Listen to incoming messages and act accordingly.
Listen to incoming messages and act accordingly.
Listen to incoming messages and act accordingly .
fn listen(
    ctxt: zmq::Context,
    ep: String,
    rcp: sync::Arc<sync::Mutex<Vec<String>>>,
    msg_rcp: sync::Arc<sync::Mutex<Vec<String>>>,
) {
    let mut done: bool = false;
    let list: zmq::Socket = ctxt.socket(zmq::REP).unwrap();
    list.bind(&ep).expect("Could not bind...");
    while !done {
        let msg: zmq::Message = list.recv_msg(0).unwrap();
        let tmp: String = msg.as_str().unwrap().to_string();
        list.send("0", 0).unwrap();
        let split = tmp.split('@');
        let data = split.collect::<Vec<&str>>();
        if data[0] == "P" {
            let mut peers: sync::MutexGuard<Vec<String>> = rcp.lock().unwrap();
            for peer in data[1].to_string().split(',') {
                if !peers.contains(&peer.to_string()) {
                    peers.push(peer.to_string());
                }
            }
            drop(peers);
        } else if data[0] == "M" {
            let mut msgs: sync::MutexGuard<Vec<String>> = msg_rcp.lock().unwrap();
            msgs.push(String::from(data[1]));
            drop(msgs);
        } else if data[0] == "K" {
            let mut peers: sync::MutexGuard<Vec<String>> = rcp.lock().unwrap();
            peers.clear();
            drop(peers);
            done = true;
        }
    }
}
fn listen ( ctxt : zmq :: Context , ep : String , rcp : sync :: Arc < sync :: Mutex < Vec < String > > > , msg_rcp : sync :: Arc < sync :: Mutex < Vec < String > > > , ) { let mut done : bool = false ; let list : zmq :: Socket = ctxt . socket ( zmq :: REP ) . unwrap ( ) ; list . bind ( & ep ) . expect ( " " ) ; while ! done { let msg : zmq :: Message = list . recv_msg ( 0 ) . unwrap ( ) ; let tmp : String = msg . as_str ( ) . unwrap ( ) . to_string ( ) ; list . send ( " " , 0 ) . unwrap ( ) ; let split = tmp . split ( '@' ) ; let data = split . collect :: < Vec < & str > > ( ) ; if data [ 0 ] == " " { let mut peers : sync :: MutexGuard < Vec < String > > = rcp . lock ( ) . unwrap ( ) ; for peer in data [ 1 ] . to_string ( ) . split ( ',' ) { if ! peers . contains ( & peer . to_string ( ) ) { peers . push ( peer . to_string ( ) ) ; } } drop ( peers ) ; } else if data [ 0 ] == " " { let mut msgs : sync :: MutexGuard < Vec < String > > = msg_rcp . lock ( ) . unwrap ( ) ; msgs . push ( String :: from ( data [ 1 ] ) ) ; drop ( msgs ) ; } else if data [ 0 ] == " " { let mut peers : sync :: MutexGuard < Vec < String > > = rcp . lock ( ) . unwrap ( ) ; peers . clear ( ) ; drop ( peers ) ; done = true ; } } }
Listen to incoming messages and act accordingly.
Listen to incoming messages and act accordingly .
ctxt
zmq::Context
ep
String
rcp
sync::Arc<sync::Mutex<Vec<String>>>
msg_rcp
sync::Arc<sync::Mutex<Vec<String>>>
ctxt
zmq::Context
ep
String
rcp
sync::Arc<sync::Mutex<Vec<String>>>
msg_rcp
sync::Arc<sync::Mutex<Vec<String>>>