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: Option<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) -> Self {
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(_) => (),
}
MyApp{ settings: settings
, out_stream: None // Not yet initialized
, big_count: 0
}
}
fn handle_connect(&mut self, connection: &mut Connection<Self>) -> Result<(), Error> {
for stream in self.settings.input_streams.iter() {
connection.connect_to_stream(stream)?;
}
let out_stream = connection.register_out_stream("Output")?;
self.out_stream = Some(out_stream);
Ok(())
}
fn handle_packet(&mut self, _connection: &mut Connection<Self>, id: StreamId, d: Datagram) -> Result<(), Error> {
println!("StreamId: {id}; Data: {d:?}");
let mut v = 0;
if d.values[0] > 1000 {
let v = 1;
self.big_count += 1;
}
self.out_stream
.as_mut()
.expect("We're connected")
.send(Datagram{ timestamp : d.timestamp
, sample_count: d.sample_count
, values: vec![v]
, flags: None})
}
}
pub fn main() -> Result<(), std::io::Error> {
Sinusoidal::<MyApp>::new()?.run()
}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
- pulser
- stream_
info
Structs§
- Connection
- OutStream
- Sinusoidal
- Handles communication with the framework and calls the App callbacks.
Constants§
Traits§
- App
- Callbacks implemented by an app.
Functions§
- 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.
- open_
persistent_ file - Open a persistent file that’s guaranteed to exist in normal
writemode.