Skip to content

Commit

Permalink
add example for get_networks
Browse files Browse the repository at this point in the history
  • Loading branch information
dearfl committed Apr 7, 2024
1 parent 72b1070 commit bbda348
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples/wasi/get_networks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "get_networks"
version = "0.1.0"
edition = "2021"

[package.metadata.component]
package = "component:get-networks"

[package.metadata.component.dependencies]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wit-bindgen = { version = "0.20.0", default-features = false, features = ["realloc"] }
32 changes: 32 additions & 0 deletions examples/wasi/get_networks/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023-2024 Optimatist Technology Co., Ltd. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
//
// This file is part of PSH.
//
// PSH is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// PSH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Perf-event-rs. If not,
// see <https://www.gnu.org/licenses/>.
use std::fs;
use std::ops::Not;
use std::process::Command;

fn main() {
let _ = fs::remove_file("src/bindings.rs");
let mut cmd = Command::new("wit-bindgen");
cmd.args(["rust", "--stubs", "--out-dir", "src/", "../../../wit/"]);

let output = cmd
.output()
.unwrap_or_else(|it| panic!("Failed to generate bindings: \n{}", it));
if output.stderr.is_empty().not() {
panic!(
"Failed to generate bindings: \n{}",
String::from_utf8(output.stderr).unwrap()
);
}
}
29 changes: 29 additions & 0 deletions examples/wasi/get_networks/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2023-2024 Optimatist Technology Co., Ltd. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
//
// This file is part of PSH.
//
// PSH is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// PSH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Perf-event-rs. If not,
// see <https://www.gnu.org/licenses/>.

#[rustfmt::skip]
mod bindings;

use bindings::profiling::system::network;

fn main() {
for _ in 0..3 {
let networks = network::get_networks().unwrap();
for net in networks {
println!("NET: {} => {:?}", net.name, net);
}
println!("");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}

0 comments on commit bbda348

Please sign in to comment.