Skip to content

Commit

Permalink
chore(iads): change to absolute time
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbo-miao committed Nov 22, 2024
1 parent af357b7 commit 387f402
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ make kubernetes-clean
- **P4** - Programming Protocol-independent Packet Processors
- **ZeroMQ** - High-performance asynchronous messaging library
- **NetMQ** - C# implementation of ZeroMQ
- **zmq.rs** - Rust implementation of ZeroMQ

### Communication Standards

Expand Down
12 changes: 7 additions & 5 deletions data-visualization/iads/iads-data-producer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rand::Rng;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;
use tokio::time::{sleep, Duration};
Expand Down Expand Up @@ -41,10 +42,6 @@ async fn send_data_stream(

// Calculate the interval in milliseconds from Hz
let interval_ms = (1000.0 / config.rate_hz) as u64;
// Calculate time increment in nanoseconds from Hz
let time_increment_ns = (1.0 / config.rate_hz * 1_000_000_000.0) as i64;

let mut time_ns = 0i64;
let mut packet_counter = 0i32;
let mut buffer = vec![0u8; packet_size_byte as usize];

Expand Down Expand Up @@ -74,6 +71,12 @@ async fn send_data_stream(
offset += TAG_SIZE_BYTE as usize;
}

// Get current absolute time in nanoseconds
let time_ns = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos() as i64;

let time_high = (time_ns >> 32) as u32;
let time_low = (time_ns & 0xFFFFFFFF) as u32;
buffer[offset..offset + VALUE_SIZE_BYTE as usize].copy_from_slice(&time_high.to_le_bytes());
Expand Down Expand Up @@ -107,7 +110,6 @@ async fn send_data_stream(
}

packet_counter += 1;
time_ns += time_increment_ns;
sleep(Duration::from_millis(interval_ms)).await;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use zeromq::{Socket, SocketRecv};
use zeromq::SubSocket;
use zeromq::{Socket, SocketRecv, SubSocket};
pub mod production {
pub mod iot {
include!(concat!(env!("OUT_DIR"), "/production.iot.rs"));
Expand Down

0 comments on commit 387f402

Please sign in to comment.