feat: initial node code
This commit is contained in:
parent
fc5cbdb361
commit
279c48980a
5 changed files with 626 additions and 1 deletions
94
domo_node/src/main.rs
Normal file
94
domo_node/src/main.rs
Normal file
|
@ -0,0 +1,94 @@
|
|||
use std::io;
|
||||
use std::time::Duration;
|
||||
use log::{error, info, LevelFilter, trace};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpSocket;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
use tokio::time::sleep;
|
||||
use domo_proto::packet::identifier::Identifier;
|
||||
use domo_proto::packet::Packet;
|
||||
use domo_proto::packet::packet_data::PacketData;
|
||||
|
||||
async fn app(mut recv: Receiver<Packet>) -> io::Result<()> {
|
||||
trace!("Entered runtime!");
|
||||
|
||||
let sock = TcpSocket::new_v4()?;
|
||||
let mut stream = sock.connect("127.0.0.1:2000".parse().unwrap()).await?;
|
||||
|
||||
while let Some(p) = recv.recv().await {
|
||||
let bytes = p.build_full_packet();
|
||||
match stream.write(bytes.as_slice()).await {
|
||||
Ok(s) => trace!("Packet [id: {}] was sent. [sent bytes: {}/{}]", p.packet_id(), s, bytes.len()),
|
||||
Err(e) => error!("Packet [id: {}] was not sent properly: {}", p.packet_id(), e)
|
||||
}
|
||||
}
|
||||
|
||||
stream.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
setup_logging()
|
||||
.expect("could not setup logging");
|
||||
trace!("Building runtime...");
|
||||
|
||||
let (send, recv) = tokio::sync::mpsc::channel::<Packet>(128);
|
||||
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
runtime.spawn(async move {
|
||||
sleep(Duration::new(1, 0)).await;
|
||||
let p =Packet::V1 {
|
||||
src: Identifier::from(0x00000000),
|
||||
dest: Identifier::from(0x00000000),
|
||||
packet_id: Identifier::from(0x00000000),
|
||||
reply_to: Identifier::from(0x00000000),
|
||||
command: 0x00,
|
||||
data: PacketData::new(
|
||||
vec![0x1,0x11,0xDD]
|
||||
)
|
||||
};
|
||||
info!("{:X?}", p.build_full_packet());
|
||||
send.send(p).await
|
||||
});
|
||||
|
||||
runtime.block_on(async {
|
||||
info!("Starting app...");
|
||||
match app(recv).await {
|
||||
Ok(()) => info!("App exited."),
|
||||
Err(e) => error!("App exited with error: {}", e)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn setup_logging() -> Result<(), fern::InitError> {
|
||||
let stdout = fern::Dispatch::new()
|
||||
.level(LevelFilter::Debug)
|
||||
.chain(io::stdout());
|
||||
|
||||
let file = fern::Dispatch::new()
|
||||
.level(LevelFilter::Trace)
|
||||
.chain(fern::log_file("node.log")?);
|
||||
|
||||
fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
match record.metadata().target() {
|
||||
_ => out.finish(format_args!(
|
||||
"[{} - {}] <{}> {}: {}",
|
||||
humantime::format_rfc3339(std::time::SystemTime::now()),
|
||||
record.level(),
|
||||
record.target(),
|
||||
record.file().unwrap_or("??"),
|
||||
message
|
||||
))
|
||||
}
|
||||
})
|
||||
.chain(stdout)
|
||||
.chain(file)
|
||||
.apply()?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue