-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add daemonize flag * Nop out daemonization on windows
- Loading branch information
1 parent
342b08a
commit ffc4a43
Showing
2 changed files
with
48 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,70 @@ | ||
use std::net::IpAddr; | ||
use std::{any, net::IpAddr}; | ||
|
||
use clap::Parser; | ||
use futures::{future, prelude::*}; | ||
use tarpc::{ | ||
server::{self, Channel}, | ||
tokio_serde::formats::Json, | ||
}; | ||
use tokio::runtime; | ||
|
||
use bh_agent_common::BhAgentService; | ||
use bh_agent_server::BhAgentServer; | ||
|
||
#[derive(Parser)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
/// The address to listen on | ||
address: IpAddr, | ||
/// The port to listen on | ||
port: u16, | ||
#[cfg(not(target_os = "windows"))] | ||
#[arg(short, long, default_value = "false", help = "Daemonize the process")] | ||
daemonize: bool, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
fn main() -> anyhow::Result<()> { | ||
env_logger::init(); | ||
|
||
let args = Args::parse(); | ||
|
||
let mut listener = tarpc::serde_transport::tcp::listen(&(args.address, args.port), Json::default).await?; | ||
// Setup runtime | ||
let rt = runtime::Builder::new_multi_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap(); | ||
|
||
// Setup listener | ||
let mut listener = rt.block_on(async { | ||
return tarpc::serde_transport::tcp::listen(&(args.address, args.port), Json::default).await; | ||
})?; | ||
listener.config_mut().max_frame_length(usize::MAX); | ||
listener | ||
// Ignore accept errors. | ||
.filter_map(|r| future::ready(r.ok())) | ||
.map(server::BaseChannel::with_defaults) | ||
// serve is generated by the service attribute. It takes as input any type implementing | ||
// the generated World trait. | ||
.map(|channel| { | ||
let server = BhAgentServer::new(channel.transport().peer_addr().unwrap()); | ||
channel.execute(server.serve()) | ||
}) | ||
// Max 10 channels. | ||
.buffer_unordered(10) | ||
.for_each(|_| async {}) | ||
.await; | ||
|
||
// Daemonize | ||
#[cfg(not(target_os = "windows"))] | ||
if args.daemonize { | ||
daemonize::Daemonize::new() | ||
.pid_file("/tmp/bh_agent_server.pid") | ||
.start() | ||
.unwrap(); | ||
} | ||
|
||
// Run the listener | ||
rt.block_on(async { | ||
listener | ||
// Ignore accept errors. | ||
.filter_map(|r| future::ready(r.ok())) | ||
.map(server::BaseChannel::with_defaults) | ||
// serve is generated by the service attribute. It takes as input any type implementing | ||
// the generated World trait. | ||
.map(|channel| { | ||
let server = BhAgentServer::new(channel.transport().peer_addr().unwrap()); | ||
channel.execute(server.serve()) | ||
}) | ||
// Max 10 channels. | ||
.buffer_unordered(10) | ||
.for_each(|_| async {}) | ||
.await; | ||
}); | ||
|
||
Ok(()) | ||
} |