Skip to main content

Crate sinusoidal

Crate sinusoidal 

Source
Expand description

The Sinusoidal Framework Rust SDK

Apps are defined by implementing the App trait and run through a Sinusoidal object.

Example

use serde::Deserialize;
use sinusoidal::proto::streaming_api::Datagram;
use sinusoidal::stream_info::InputStream;
use sinusoidal::*;
use std::io::{Error, ErrorKind, Write};
use std::collections::HashMap;
use std::fs;

struct MyApp {
  settings: MySettings,
  out_stream: OutStream,
  big_count: i64
}

impl MyApp {
  fn drop(self: Self) -> () {
    let mut f = open_persistent_file("big_count").unwrap();
    f.write(format!("{}", self.big_count).as_bytes()).unwrap();
  }
}

#[derive(Debug, Clone, Deserialize)]
struct MySettings {
  #[serde(default)]
  input_streams: Vec<InputStream>,
}

impl App for MyApp {
  type Settings = MySettings;
  type AppEvent = (); //In this example we don't introduce any custom events

  fn new(settings: MySettings, connection: &mut Connection<Self>) -> Result<Self, Error> {
    let mut big = 0;
    match open_persistent_file("big_count") {
      Ok(f)  => {
        let contents = String::new();
        fs::read_to_string(&contents).unwrap();
        big = contents.trim().parse::<i64>().map_err(|e| {Error::new(ErrorKind::InvalidData, e)}).unwrap()
        },
      Err(_) => (),
    }
    for stream in settings.input_streams.iter() {
      connection.connect_to_stream(stream)?;
    }
    let out_stream = connection.register_out_stream("Output")?;
    Ok(MyApp{ settings, out_stream, big_count: 0 })
  }

  fn handle_packet(&mut self, _connection: &mut Connection<Self>, id: &StreamId, d: Datagram) -> Result<(), Error> {
    println!("StreamId: {id}; Data: {d:?}");
    let mut v = 0_i64;
    if d.values[0] > 1000 {
      v = 1;
      self.big_count += 1;
    }
    self.out_stream
      .send(Datagram{ timestamp : d.timestamp
                    , sample_count: d.sample_count
                    , values: vec![v]
                    , clock_synch: d.clock_synch
                    , flags: d.flags
                    , gm_identity: d.gm_identity })
  }
}

pub fn main() -> Result<(), std::io::Error> {
  Sinusoidal::<MyApp>::main()
}

Re-exports§

pub use stream_info::InputStream;
pub use stream_info::StreamId;
pub use stream_info::StreamInfo;
pub use stream_info::SysStream;

Modules§

goose
proto
Protobuf definitions
stream_info
sv
Custom binary encoding/decoding for datagrams Encoding and decoding of the custom binary datagram format used in the datagrams bytes field of StreamPacket.
utils

Structs§

Connection
OutStream
Sinusoidal
Handles communication with the framework and calls the App callbacks.
TriggerId
Qualified trigger id (i.e. Trigger:{app_name}:{trigger_name})
UnqualifiedTriggerName
Unqualified trigger name

Enums§

ClockSynch

Constants§

SS_DATA_DIR
SS_LOG_DIR
SS_PRIV_DIR

Traits§

App
Callbacks implemented by an app.

Functions§

collapse_datagram_flags
create_log_file
Create a new log file in write-only append mode, truncating it if it exists.
create_public_file
Create a new public file in write-only mode, truncating it if it exists.
decode_datagram_flags
encode_datagram_flags
merge_clock_synch
merge_datagram_flags
open_persistent_file
Open a persistent file that’s guaranteed to exist in normal write mode.
socket_token
Compute a fixed-size, file-system-safe token for a socket name.