fn publisher(url: &str) -> Result<(), nng::Error> {
    let s = Socket::new(Protocol::Pub0)?;
    let count = Arc::new(AtomicUsize::new(0));
    let count_clone = count.clone();

    s.pipe_notify(move |_, ev| {
        match ev {
            PipeEvent::AddPost => count_clone.fetch_add(1, Ordering::Relaxed),
            PipeEvent::RemovePost => count_clone.fetch_sub(1, Ordering::Relaxed),
            _ => 0,
        };
    })?;

    s.listen(url)?;

    loop {
        let mut rng = rand::thread_rng();
        let mut nums: Vec<u64> = (1..4).collect();
        nums.shuffle(&mut rng);
        println!(">>>> Sleep Duration: {:?}", nums[1]);
        // Sleep before sending the next message.
        thread::sleep(Duration::from_secs(nums[1]));

        let connect_psdb = connect_insert::main();
        // returns the id
        println!("PUBLISH INSERT EVENT RESPONSE ::  OK? {:?}", connect_psdb);

        let system_time = SystemTime::now();
        let datetime: DateTime<Utc> = system_time.into();

        if nums[1] == 1 {
            let mint_coin = MintCoins {
                time: datetime.format("%d/%m/%Y %T").to_string(),
                amount: 11.00,
                account_id: 1234,
            };
            let serialized = serde_json::to_string(&mint_coin).unwrap();
            println!("MESSAGE >> {:?}", serialized);
            // call DB
            //s.send(serialized.as_bytes())?;

            let data = count.load(Ordering::Relaxed) as u64;
            println!("PUBLISHER: SENDING {}", data);
            s.send(data.to_le_bytes())?;
        }

        if nums[1] == 2 {
            let transfer_coin = TransferCoins {
                time: datetime.format("%d/%m/%Y %T").to_string(),
                amount: 11.00,
                account_id: 1234,
            };
            let serialized = serde_json::to_string(&transfer_coin).unwrap();
            println!("MESSAGE >> {:?}", serialized);
            // call DB
            //s.send(serialized.as_bytes())?;

            let data = count.load(Ordering::Relaxed) as u64;
            println!("PUBLISHER: SENDING {}", data);
            s.send(data.to_le_bytes())?;
        }

        if nums[1] == 3 {
            let burn_coin = BurnCoins {
                time: datetime.format("%d/%m/%Y %T").to_string(),
                amount: 11.00,
                account_id: 1234,
            };

            let serialized = serde_json::to_string(&burn_coin).unwrap();
            println!("MESSAGE >> {:?}", serialized);
            // call DB
            //s.send(serialized.as_bytes())?;

            let data = count.load(Ordering::Relaxed) as u64;
            println!("PUBLISHER: SENDING {}", data);
            s.send(data.to_le_bytes())?;
        }
    }
}
/// Run the publisher portion of the program.
Run the publisher portion of the program.
Run the publisher portion of the program .
fn publisher(url: &str) -> Result<(), nng::Error> {
    let s = Socket::new(Protocol::Pub0)?;
    let count = Arc::new(AtomicUsize::new(0));
    let count_clone = count.clone();
    s.pipe_notify(move |_, ev| {
        match ev {
            PipeEvent::AddPost => count_clone.fetch_add(1, Ordering::Relaxed),
            PipeEvent::RemovePost => count_clone.fetch_sub(1, Ordering::Relaxed),
            _ => 0,
        };
    })?;
    s.listen(url)?;
    loop {
        let mut rng = rand::thread_rng();
        let mut nums: Vec<u64> = (1..4).collect();
        nums.shuffle(&mut rng);
        println!(">>>> Sleep Duration: {:?}", nums[1]);
        // Sleep before sending the next message.
        thread::sleep(Duration::from_secs(nums[1]));
        let connect_psdb = connect_insert::main();
        // returns the id
        println!("PUBLISH INSERT EVENT RESPONSE ::  OK? {:?}", connect_psdb);
        let system_time = SystemTime::now();
        let datetime: DateTime<Utc> = system_time.into();
        if nums[1] == 1 {
            let mint_coin = MintCoins {
                time: datetime.format("%d/%m/%Y %T").to_string(),
                amount: 11.00,
                account_id: 1234,
            };
            let serialized = serde_json::to_string(&mint_coin).unwrap();
            println!("MESSAGE >> {:?}", serialized);
            // call DB
            //s.send(serialized.as_bytes())?;
            let data = count.load(Ordering::Relaxed) as u64;
            println!("PUBLISHER: SENDING {}", data);
            s.send(data.to_le_bytes())?;
        }
        if nums[1] == 2 {
            let transfer_coin = TransferCoins {
                time: datetime.format("%d/%m/%Y %T").to_string(),
                amount: 11.00,
                account_id: 1234,
            };
            let serialized = serde_json::to_string(&transfer_coin).unwrap();
            println!("MESSAGE >> {:?}", serialized);
            // call DB
            //s.send(serialized.as_bytes())?;
            let data = count.load(Ordering::Relaxed) as u64;
            println!("PUBLISHER: SENDING {}", data);
            s.send(data.to_le_bytes())?;
        }
        if nums[1] == 3 {
            let burn_coin = BurnCoins {
                time: datetime.format("%d/%m/%Y %T").to_string(),
                amount: 11.00,
                account_id: 1234,
            };
            let serialized = serde_json::to_string(&burn_coin).unwrap();
            println!("MESSAGE >> {:?}", serialized);
            // call DB
            //s.send(serialized.as_bytes())?;
            let data = count.load(Ordering::Relaxed) as u64;
            println!("PUBLISHER: SENDING {}", data);
            s.send(data.to_le_bytes())?;
        }
    }
}
fn publisher ( url : & str ) -> Result < ( ) , nng :: Error > { let s = Socket :: new ( Protocol :: Pub0 ) ? ; let count = Arc :: new ( AtomicUsize :: new ( 0 ) ) ; let count_clone = count . clone ( ) ; s . pipe_notify ( move | _ , ev | { match ev { PipeEvent :: AddPost => count_clone . fetch_add ( 1 , Ordering :: Relaxed ) , PipeEvent :: RemovePost => count_clone . fetch_sub ( 1 , Ordering :: Relaxed ) , _ => 0 , } ; } ) ? ; s . listen ( url ) ? ; loop { let mut rng = rand :: thread_rng ( ) ; let mut nums : Vec < u64 > = ( 1 .. 4 ) . collect ( ) ; nums . shuffle ( & mut rng ) ; println ! ( " " nums [ 1 ] ) ; // Sleep before sending the next message. thread :: sleep ( Duration :: from_secs ( nums [ 1 ] ) ) ; let connect_psdb = connect_insert :: main ( ) ; // returns the id println ! ( " " connect_psdb ) ; let system_time = SystemTime :: now ( ) ; let datetime : DateTime < Utc > = system_time . into ( ) ; if nums [ 1 ] == 1 { let mint_coin = MintCoins { time : datetime . format ( " " ) . to_string ( ) , amount : 11.00 , account_id : 1234 , } ; let serialized = serde_json :: to_string ( & mint_coin ) . unwrap ( ) ; println ! ( " " serialized ) ; // call DB //s.send(serialized.as_bytes())?; let data = count . load ( Ordering :: Relaxed ) as u64 ; println ! ( " " data ) ; s . send ( data . to_le_bytes ( ) ) ? ; } if nums [ 1 ] == 2 { let transfer_coin = TransferCoins { time : datetime . format ( " " ) . to_string ( ) , amount : 11.00 , account_id : 1234 , } ; let serialized = serde_json :: to_string ( & transfer_coin ) . unwrap ( ) ; println ! ( " " serialized ) ; // call DB //s.send(serialized.as_bytes())?; let data = count . load ( Ordering :: Relaxed ) as u64 ; println ! ( " " data ) ; s . send ( data . to_le_bytes ( ) ) ? ; } if nums [ 1 ] == 3 { let burn_coin = BurnCoins { time : datetime . format ( " " ) . to_string ( ) , amount : 11.00 , account_id : 1234 , } ; let serialized = serde_json :: to_string ( & burn_coin ) . unwrap ( ) ; println ! ( " " serialized ) ; // call DB //s.send(serialized.as_bytes())?; let data = count . load ( Ordering :: Relaxed ) as u64 ; println ! ( " " data ) ; s . send ( data . to_le_bytes ( ) ) ? ; } } }
Run the publisher portion of the program.
Run the publisher portion of the program .
url
&str