chiark / gitweb /
server: change type of checkn
[hippotat.git] / src / bin / client.rs
index 452873305947620ce07156a8a137a15cbbfe5e0c..d73eceb221277819c858df45c602171d7d6b12aa 100644 (file)
@@ -5,8 +5,6 @@
 use hippotat::prelude::*;
 use hippotat_macros::into_crlfs;
 
-const MAX_BATCH_DOWN_RESP_OVERHEAD: usize = 10_000;
-
 #[derive(StructOpt,Debug)]
 pub struct Opts {
   #[structopt(flatten)]
@@ -47,12 +45,10 @@ fn submit_request<'r, 'c:'r, C:HCC>(
     .saturating_add(Duration::from_nanos(999_999_999))
     .as_secs();
 
-  let time_t = SystemTime::now()
-    .duration_since(UNIX_EPOCH)
-    .unwrap_or_else(|_| Duration::default()) // clock is being weird
-    .as_secs();
+  let time_t = time_t_now();
   let time_t = format!("{:x}", time_t);
   let hmac = token_hmac(c.ic.secret.0.as_bytes(), time_t.as_bytes());
+  //dbg!(DumpHex(&hmac));
   let mut token = time_t;
   write!(token, " ").unwrap();
   base64::encode_config_buf(&hmac, BASE64_CONFIG, &mut token);
@@ -68,12 +64,14 @@ fn submit_request<'r, 'c:'r, C:HCC>(
        {}
        {}
        {}
+       {}
        {}"#),
                        &c.ic.link.client,
                        token,
                        c.ic.target_requests_outstanding,
                        show_timeout,
                        c.ic.max_batch_down,
+                       c.ic.max_batch_up,
   );
 
   let prefix2 = format!(into_crlfs!(
@@ -116,7 +114,7 @@ fn submit_request<'r, 'c:'r, C:HCC>(
     as_ref,
   ).map(|b| b.len()).sum();
 
-  trace!("{} #{}: req; tx bytes={} frames={}",
+  trace!("{} #{}: req; tx body_len={} frames={}",
          &c.ic, req_num, body_len, upbound.len());
 
   let body = hyper::body::Body::wrap_stream(
@@ -140,9 +138,12 @@ fn submit_request<'r, 'c:'r, C:HCC>(
     let r = async { tokio::time::timeout( c.ic.effective_http_timeout, async {
       let resp = resp.await.context("make request")?;
       let status = resp.status();
-      let resp = resp.into_body();
-      let max_body = c.ic.max_batch_down.sat() + MAX_BATCH_DOWN_RESP_OVERHEAD;
-      let resp = read_limited_body(max_body, resp).await?;
+      let mut resp = resp.into_body();
+      let max_body = c.ic.max_batch_down.sat() + MAX_OVERHEAD;
+      let resp = read_limited_bytes(
+        max_body, default(), default(), &mut resp
+      ).await
+        .discard_data().context("fetching response body")?;
 
       if ! status.is_success() {
         throw!(anyhow!("HTTP error status={} body={:?}",
@@ -247,7 +248,10 @@ async fn run_client<C:HCC>(
         {
           while let Some(TxQueued { data, expires }) = tx_queue.pop_front() {
             match upbound.add(ic.max_batch_up, data.into()/*todo:504*/) {
-              Err(data) => { tx_queue.push_front(TxQueued { data: data.into(), expires }); break; }
+              Err(data) => {
+                tx_queue.push_front(TxQueued { data: data.into(), expires });
+                break;
+              }
               Ok(()) => { },
             }
           }
@@ -270,14 +274,23 @@ async fn run_client<C:HCC>(
           reqs.swap_remove(goti);
 
           if let Some(got) = got {
-            reporter.lock().success();
+            
             //eprintln!("got={:?}", DumpHex(&got));
-            checkn(SlipNoConv,ic.mtu, &got, &mut rx_queue, |header| {
+            match checkn(SlipNoConv,ic.mtu, &got, |header| {
               let addr = ip_packet_addr::<true>(header)?;
               if addr != ic.link.client.0 { throw!(PE::Dst(addr)) }
               Ok(())
-            }, |e| error!("{} #{}: rx discarding: {}", &ic, req_num, e));
-          
+            }, |o| rx_queue.push(o),
+               |e| error!("{} #{}: rx discarding: {}", &ic, req_num, e))
+            {
+              Ok(()) => reporter.lock().success(),
+              Err(ErrorOnlyBad) => {
+                reqs.push(Box::pin(async {
+                  tokio::time::sleep(ic.http_retry).await;
+                  None
+                }));
+              },
+            }
           }
         },
 
@@ -300,10 +313,12 @@ async fn run_client<C:HCC>(
 async fn main() {
   let opts = Opts::from_args();
   let (ics,) = config::startup("hippotat", LinkEnd::Client,
-                               &opts.config, &opts.log);
+                               &opts.config, &opts.log, |ics|Ok((ics,)));
 
   let https = HttpsConnector::new();
-  let hclient = hyper::Client::builder().build::<_, hyper::Body>(https);
+  let hclient = hyper::Client::builder()
+    .http1_preserve_header_case(true)
+    .build::<_, hyper::Body>(https);
   let hclient = Arc::new(hclient);
 
   info!("starting");