use std::io::{Read, Write};
use std::time::Instant;
use supermachine::{Image, Vm, VmConfig};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let snapshot_path = std::env::args()
.nth(1)
.ok_or("usage: embed_simple <snapshot-path>")?;
let t0 = Instant::now();
let image = Image::from_snapshot(&snapshot_path)?;
let vm = Vm::start(&image, &VmConfig::new())?;
eprintln!("vm started in {:?}", t0.elapsed());
eprintln!("vsock path: {}", vm.vsock_path().display());
let mut sock = vm.connect()?;
sock.write_all(
b"GET / HTTP/1.1\r\n\
Host: workload\r\n\
Connection: close\r\n\
\r\n",
)?;
let mut response = Vec::new();
sock.read_to_end(&mut response)?;
eprintln!("response: {} bytes", response.len());
let preview = String::from_utf8_lossy(&response);
for line in preview.lines().take(6) {
eprintln!(" {line}");
}
vm.stop()?;
eprintln!("vm stopped after {:?}", t0.elapsed());
Ok(())
}