1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Functions for sending messages.

use std::borrow::Borrow;
use std::fs;
use std::path::PathBuf;

use tokio::io::{AsyncRead, AsyncWrite};

use tokio_util::codec::Framed;

use futures::sink::SinkExt;

use bytes::Bytes;

use blather::{Params, Telegram};

use protwrap::ProtAddr;

use crate::auth::Auth;
use crate::conn;
use crate::types::AppChannel;

use crate::err::Error;


pub enum InputType {
  Params(Params),
  File(PathBuf),
  VecBuf(Vec<u8>),
  Bytes(Bytes)
}

impl InputType {
  fn get_size(&self) -> Result<usize, Error> {
    match self {
      InputType::Params(params) => Ok(params.calc_buf_size()),
      InputType::File(f) => {
        let metadata = fs::metadata(&f)?;
        Ok(metadata.len() as usize)
      }
      InputType::VecBuf(v) => Ok(v.len()),
      InputType::Bytes(b) => Ok(b.len())
    }
  }
}


pub struct Transport {
  pub ch: AppChannel
}

pub struct MsgInfo {
  pub cmd: u32,
  pub meta: Option<InputType>,
  pub payload: Option<InputType>
}


impl MsgInfo {
  fn get_meta_size(&self) -> Result<u32, Error> {
    let sz = match &self.meta {
      Some(meta) => meta.get_size()?,
      None => 0
    };

    if sz > u32::MAX as usize {
      // ToDo: Return out of bounds error
    }

    Ok(sz as u32)
  }


  fn get_payload_size(&self) -> Result<u64, Error> {
    let sz = match &self.payload {
      Some(payload) => payload.get_size()?,
      None => 0
    };

    Ok(sz as u64)
  }
}


/// Connect, optionally authenticate, send message and disconnect.
///
/// This is a convenience function for application that don't need to keep a
/// connection open, and only needs to send a message occasionally.
pub async fn connsend<P, X, M>(
  pa: &ProtAddr,
  auth: Option<&Auth>,
  xfer: X,
  mi: M
) -> Result<String, Error>
where
  X: Borrow<Transport>,
  M: Borrow<MsgInfo>
{
  let mut conn = conn::connect(pa, auth).await?;

  send(&mut conn, xfer, mi).await
}


/// Send a message, including (if applicable) its metadata and payload.
///
/// On successful completion returns the transfer identifier.
pub async fn send<T, X, M>(
  conn: &mut Framed<T, blather::Codec>,
  xfer: X,
  mi: M
) -> Result<String, Error>
where
  T: AsyncRead + AsyncWrite + Unpin,
  X: Borrow<Transport>,
  M: Borrow<MsgInfo>
{
  let xfer = xfer.borrow();
  let mi = mi.borrow();

  //
  // Determine length of metadata and payload
  //
  let metalen = mi.get_meta_size()?;
  let payloadlen = mi.get_payload_size()?;

  //
  // Prepare the Msg telegram
  //
  let mut tg = Telegram::new_topic("Msg")?;
  tg.add_param("_Ch", xfer.ch.to_string())?;
  if mi.cmd != 0 {
    tg.add_param("Cmd", mi.cmd)?;
  }
  if metalen != 0 {
    tg.add_param("MetaLen", metalen)?;
  }
  if payloadlen != 0 {
    tg.add_param("Len", payloadlen)?;
  }

  //
  // Request the message transfer
  //
  let params = crate::sendrecv(conn, &tg).await?;

  //
  // Extract the transfer identifier assigned to this message
  //
  let xferid = match params.get_str("XferId") {
    Some(xferid) => xferid.to_string(),
    None => {
      let e = "Missing expected transfer identifier from server reply";
      return Err(Error::MissingData(String::from(e)));
    }
  };

  //
  // Transmit metadata, if applicable, and wait for the server to ACK it
  //
  if let Some(meta) = &mi.meta {
    send_content(conn, meta).await?;
    crate::expect_okfail(conn).await?;
  }

  //
  // Transmit payload, if applicable, and wait for the server to ACK it
  //
  if let Some(payload) = &mi.payload {
    send_content(conn, payload).await?;
    crate::expect_okfail(conn).await?;
  }

  Ok(xferid)
}


async fn send_content<T>(
  conn: &mut Framed<T, blather::Codec>,
  data: &InputType
) -> Result<(), Error>
where
  T: AsyncRead + AsyncWrite + Unpin
{
  match data {
    InputType::Params(params) => Ok(conn.send(params).await?),
    InputType::File(fname) => {
      let mut f = tokio::fs::File::open(fname).await?;
      let _ = tokio::io::copy(&mut f, conn.get_mut()).await?;
      Ok(())
    }
    InputType::VecBuf(v) => Ok(conn.send(v.as_slice()).await?),
    InputType::Bytes(b) => Ok(conn.send(b.as_ref()).await?)
  }
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :