Advertisement
Guest User

Abomination

a guest
Apr 19th, 2024
50
0
351 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.61 KB | Source Code | 0 0
  1. use image::Rgba;
  2. use ping::dgramsock::ping;
  3. use std::{
  4.     env,
  5.     net::{IpAddr, Ipv6Addr},
  6.     path::Path,
  7.     sync::Arc,
  8.     thread::{self},
  9. };
  10.  
  11. fn parse_args() -> Result<(String, u16, u16, u8), &'static str> {
  12.    let args: Vec<_> = env::args().collect();
  13.  
  14.    if args.len() != 4 && args.len() != 5 {
  15.        return Err("Usage: pinger <path> <X> <Y> <thread count>");
  16.    }
  17.  
  18.    let path = args[1].to_string();
  19.    if !Path::new(&path).exists() {
  20.        return Err("File does not exist");
  21.    }
  22.  
  23.    let x = args[2].parse::<u16>();
  24.    let y = args[3].parse::<u16>();
  25.    if x.is_err() | y.is_err() {
  26.        return Err("Invalid coordinates");
  27.    }
  28.  
  29.    let thread_c = if args.len() == 5 {
  30.        args[4].parse().unwrap_or(1)
  31.    } else {
  32.        1
  33.    };
  34.  
  35.    Ok((path, x.unwrap(), y.unwrap(), thread_c))
  36. }
  37.  
  38. fn ping_pixel(x: u16, y: u16, r: u8, g: u8, b: u8, a: u8) {
  39.    let addr =
  40.    IpAddr::V6(Ipv6Addr::new(
  41.        0x2A01,0x4F8,0x1C1E,0x85CD, //address
  42.        x,y, // X, Y
  43.        (r as u16) << 8 | g as u16, // RG
  44.        (b as u16) << 8 | a as u16, // BA
  45.    ));
  46.    // dbg!(addr);
  47.    let _ = ping(addr, None, None, None, None, None);
  48. }
  49.  
  50. fn ping_chunk(
  51.    origin_x: u16,
  52.    origin_y: u16,
  53.    chunk: Arc<Vec<(u32, u32, Rgba<u8>)>>,
  54.    beg: usize,
  55.    end: usize,
  56. ) {
  57.    for (x, y, pix) in &chunk[beg..end] {
  58.        ping_pixel(
  59.            origin_x + *x as u16,
  60.            origin_y + *y as u16,
  61.            pix[0],
  62.            pix[1],
  63.            pix[2],
  64.            pix[3],
  65.        );
  66.    }
  67. }
  68.  
  69. fn main() {
  70.    let (image, x, y, n) = match parse_args() {
  71.        Ok(tup) => tup,
  72.        Err(msg) => {
  73.            println!("Error: {msg}");
  74.            std::process::exit(1)
  75.        }
  76.    };
  77.  
  78.    let pixels: Vec<(u32, u32, Rgba<u8>)> = image::open(image)
  79.        .expect("Failed to open the image")
  80.        .to_rgba8()
  81.        .enumerate_pixels()
  82.        .map(|(x, y, p)| (x, y, *p))
  83.        .collect();
  84.  
  85.    let (chunk, leftover): (u64, u64) = (
  86.        pixels.len() as u64 / n as u64,
  87.        pixels.len() as u64 % n as u64,
  88.    );
  89.    let pixels = Arc::new(pixels);
  90.  
  91.    let mut threads = Vec::with_capacity(n as usize);
  92.    for i in 0..n as u64 {
  93.        let beg = i * chunk;
  94.        let end = if i != n as u64 - 1 {
  95.            beg + chunk
  96.        } else {
  97.            beg + chunk + leftover
  98.        };
  99.  
  100.        let pixels = Arc::clone(&pixels);
  101.        threads.push(thread::spawn(move || {
  102.            ping_chunk(x, y, pixels, beg as usize, end as usize);
  103.        }));
  104.    }
  105.  
  106.    for t in threads {
  107.        let _ = t.join();
  108.    }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement