Skip to content

Commit

Permalink
Add ping interval argument to example client
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed Dec 19, 2017
1 parent b38cf16 commit 7168d65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 19 additions & 3 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,23 @@ fn main() {

const ARG_PATH: &'static str = "path";
const ARG_AUTHTOKEN: &'static str = "authtoken";
const ARG_PING_INTERVAL: &'static str = "ping_interval";

// Set up CLI arguments
let arg_ping_interval = Arg::with_name(ARG_PING_INTERVAL)
.short("i")
.takes_value(true)
.value_name("SECONDS")
.required(false)
.default_value("0")
.help("The WebSocket ping interval (set to 0 to disable pings)");
let app = App::new("SaltyRTC Test Client")
.version(VERSION)
.author("Danilo Bargen <mail@dbrgn.ch>")
.about("Test client for SaltyRTC.")
.subcommand(SubCommand::with_name("initiator")
.about("Start client as initiator"))
.about("Start client as initiator")
.arg(arg_ping_interval.clone()))
.subcommand(SubCommand::with_name("responder")
.about("Start client as responder")
.arg(Arg::with_name(ARG_PATH)
Expand All @@ -76,7 +85,8 @@ fn main() {
.takes_value(true)
.value_name("AUTHTOKEN")
.required(true)
.help("The auth token (hex encoded)")));
.help("The auth token (hex encoded)"))
.arg(arg_ping_interval));

// Parse arguments
let subcommand = app.get_matches().subcommand.unwrap_or_else(|| {
Expand Down Expand Up @@ -131,13 +141,19 @@ fn main() {
Role::Responder => args.value_of(ARG_PATH).expect("Path not supplied").to_lowercase(),
};

// Determine ping interval
let ping_interval = {
let seconds: u64 = args.value_of(ARG_PING_INTERVAL).expect("Ping interval not supplied")
.parse().expect("Could not parse interval seconds to a number");
Duration::from_secs(seconds)
};
// Create new SaltyRTC client instance
let (salty, auth_token_hex) = match role {
Role::Initiator => {
let task = ChatTask::new("initiat0r");
let salty = SaltyClientBuilder::new(keypair)
.add_task(Box::new(task))
.with_ping_interval(Some(Duration::from_secs(30)))
.with_ping_interval(Some(ping_interval))
.initiator()
.expect("Could not create SaltyClient instance");
let auth_token_hex = HEXLOWER.encode(salty.auth_token().unwrap().secret_key_bytes());
Expand Down
5 changes: 5 additions & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,16 @@ pub(crate) trait Signaling {
.ping_interval
.map(|duration| duration.as_secs())
.map(|secs| if secs > ::std::u32::MAX as u64 {
warn!("Ping interval is too large. Truncating it to {} seconds.", ::std::u32::MAX);
::std::u32::MAX
} else {
secs as u32
})
.unwrap_or(0u32);
match ping_interval {
0 => debug!("Requesting WebSocket ping messages to be disabled"),
n => debug!("Requesting WebSocket ping messages every {}s", n),
};
let client_auth = ClientAuth {
your_cookie: self.server().cookie_pair().theirs.clone().unwrap(),
subprotocols: vec![::SUBPROTOCOL.into()],
Expand Down

0 comments on commit 7168d65

Please sign in to comment.