Struct RocketClient

Source
pub struct RocketClient { /* private fields */ }
Expand description

The RocketClient type. This contains the connected socket and other fields.

Implementations§

Source§

impl RocketClient

Source

pub fn new() -> Result<Self, Error>

Construct a new RocketClient.

This constructs a new Rocket client and connects to localhost on port 1338.

§Errors

Error::Connect if connection cannot be established, or Error::Handshake if the handshake fails.

§Examples
let mut rocket = RocketClient::new()?;
Examples found in repository?
examples/edit.rs (line 9)
8fn main() -> Result<(), Box<dyn Error>> {
9    let mut rocket = RocketClient::new()?;
10    rocket.get_track_mut("test")?;
11    rocket.get_track_mut("test2")?;
12    rocket.get_track_mut("a:test2")?;
13
14    let mut current_row = 0;
15    let mut paused = true;
16
17    loop {
18        if let Some(event) = rocket.poll_events()? {
19            match event {
20                Event::SetRow(row) => {
21                    println!("SetRow (row: {:?})", row);
22                    current_row = row;
23                }
24                Event::Pause(state) => {
25                    paused = state;
26
27                    let track1 = rocket.get_track("test").unwrap();
28                    println!(
29                        "Pause (value: {:?}) (row: {:?})",
30                        track1.get_value(current_row as f32),
31                        current_row
32                    );
33                }
34                Event::SaveTracks => {
35                    // Obtain a clone of current track state
36                    let tracks = rocket.save_tracks();
37
38                    // Open a file for writing, create if not present,
39                    // truncate (overwrite) in case it has previous contents.
40                    let mut file = OpenOptions::new()
41                        .write(true)
42                        .create(true)
43                        .truncate(true)
44                        .open(TRACKS_FILE)?;
45
46                    // Serialize tracks into the file using bincode
47                    let bincode_conf = bincode::config::standard();
48                    bincode::encode_into_std_write(tracks, &mut file, bincode_conf)?;
49                    // See examples/play.rs for deserializing and playback
50                    println!("Tracks saved to {}", TRACKS_FILE);
51                }
52            }
53            println!("{:?}", event);
54        }
55
56        if !paused {
57            current_row += 1;
58            rocket.set_row(current_row)?;
59        }
60
61        std::thread::sleep(Duration::from_millis(32));
62    }
63}
Source

pub fn connect(addr: impl ToSocketAddrs) -> Result<Self, Error>

Construct a new RocketClient.

This constructs a new Rocket client and connects to a specified host and port.

§Errors

Error::Connect if connection cannot be established, or Error::Handshake if the handshake fails.

§Examples
let mut rocket = RocketClient::connect(("localhost", 1338))?;
Source

pub fn get_track_mut(&mut self, name: &str) -> Result<&mut Track, Error>

Get track by name.

If the track does not yet exist it will be created.

§Errors

This method can return an Error::IOError if Rocket tracker disconnects.

§Panics

Will panic if name’s length exceeds u32::MAX.

§Examples
let track = rocket.get_track_mut("namespace:track")?;
track.get_value(3.5);
Examples found in repository?
examples/edit.rs (line 10)
8fn main() -> Result<(), Box<dyn Error>> {
9    let mut rocket = RocketClient::new()?;
10    rocket.get_track_mut("test")?;
11    rocket.get_track_mut("test2")?;
12    rocket.get_track_mut("a:test2")?;
13
14    let mut current_row = 0;
15    let mut paused = true;
16
17    loop {
18        if let Some(event) = rocket.poll_events()? {
19            match event {
20                Event::SetRow(row) => {
21                    println!("SetRow (row: {:?})", row);
22                    current_row = row;
23                }
24                Event::Pause(state) => {
25                    paused = state;
26
27                    let track1 = rocket.get_track("test").unwrap();
28                    println!(
29                        "Pause (value: {:?}) (row: {:?})",
30                        track1.get_value(current_row as f32),
31                        current_row
32                    );
33                }
34                Event::SaveTracks => {
35                    // Obtain a clone of current track state
36                    let tracks = rocket.save_tracks();
37
38                    // Open a file for writing, create if not present,
39                    // truncate (overwrite) in case it has previous contents.
40                    let mut file = OpenOptions::new()
41                        .write(true)
42                        .create(true)
43                        .truncate(true)
44                        .open(TRACKS_FILE)?;
45
46                    // Serialize tracks into the file using bincode
47                    let bincode_conf = bincode::config::standard();
48                    bincode::encode_into_std_write(tracks, &mut file, bincode_conf)?;
49                    // See examples/play.rs for deserializing and playback
50                    println!("Tracks saved to {}", TRACKS_FILE);
51                }
52            }
53            println!("{:?}", event);
54        }
55
56        if !paused {
57            current_row += 1;
58            rocket.set_row(current_row)?;
59        }
60
61        std::thread::sleep(Duration::from_millis(32));
62    }
63}
Source

pub fn get_track(&self, name: &str) -> Option<&Track>

Get track by name.

You should use get_track_mut to create a track.

Examples found in repository?
examples/edit.rs (line 27)
8fn main() -> Result<(), Box<dyn Error>> {
9    let mut rocket = RocketClient::new()?;
10    rocket.get_track_mut("test")?;
11    rocket.get_track_mut("test2")?;
12    rocket.get_track_mut("a:test2")?;
13
14    let mut current_row = 0;
15    let mut paused = true;
16
17    loop {
18        if let Some(event) = rocket.poll_events()? {
19            match event {
20                Event::SetRow(row) => {
21                    println!("SetRow (row: {:?})", row);
22                    current_row = row;
23                }
24                Event::Pause(state) => {
25                    paused = state;
26
27                    let track1 = rocket.get_track("test").unwrap();
28                    println!(
29                        "Pause (value: {:?}) (row: {:?})",
30                        track1.get_value(current_row as f32),
31                        current_row
32                    );
33                }
34                Event::SaveTracks => {
35                    // Obtain a clone of current track state
36                    let tracks = rocket.save_tracks();
37
38                    // Open a file for writing, create if not present,
39                    // truncate (overwrite) in case it has previous contents.
40                    let mut file = OpenOptions::new()
41                        .write(true)
42                        .create(true)
43                        .truncate(true)
44                        .open(TRACKS_FILE)?;
45
46                    // Serialize tracks into the file using bincode
47                    let bincode_conf = bincode::config::standard();
48                    bincode::encode_into_std_write(tracks, &mut file, bincode_conf)?;
49                    // See examples/play.rs for deserializing and playback
50                    println!("Tracks saved to {}", TRACKS_FILE);
51                }
52            }
53            println!("{:?}", event);
54        }
55
56        if !paused {
57            current_row += 1;
58            rocket.set_row(current_row)?;
59        }
60
61        std::thread::sleep(Duration::from_millis(32));
62    }
63}
Source

pub fn save_tracks(&self) -> &Tracks

Get a snapshot of the tracks in the session.

The returned Tracks can be dumped to a file in any supported format. The counterpart to this function is RocketPlayer::new, which loads tracks for playback.

§Example
let mut rocket = RocketClient::new()?;

// Create tracks, call poll_events, etc...

// Open a file for writing
let mut file = OpenOptions::new()
    .write(true)
    .create(true)
    .truncate(true)
    .open("tracks.bin")
    .expect("Failed to open tracks.bin for writing");

// Save a snapshot of the client to a file for playback in release builds
let tracks = rocket.save_tracks();
bincode::encode_into_std_write(tracks, &mut file, bincode::config::standard())
    .expect("Failed to encode tracks.bin");
Examples found in repository?
examples/edit.rs (line 36)
8fn main() -> Result<(), Box<dyn Error>> {
9    let mut rocket = RocketClient::new()?;
10    rocket.get_track_mut("test")?;
11    rocket.get_track_mut("test2")?;
12    rocket.get_track_mut("a:test2")?;
13
14    let mut current_row = 0;
15    let mut paused = true;
16
17    loop {
18        if let Some(event) = rocket.poll_events()? {
19            match event {
20                Event::SetRow(row) => {
21                    println!("SetRow (row: {:?})", row);
22                    current_row = row;
23                }
24                Event::Pause(state) => {
25                    paused = state;
26
27                    let track1 = rocket.get_track("test").unwrap();
28                    println!(
29                        "Pause (value: {:?}) (row: {:?})",
30                        track1.get_value(current_row as f32),
31                        current_row
32                    );
33                }
34                Event::SaveTracks => {
35                    // Obtain a clone of current track state
36                    let tracks = rocket.save_tracks();
37
38                    // Open a file for writing, create if not present,
39                    // truncate (overwrite) in case it has previous contents.
40                    let mut file = OpenOptions::new()
41                        .write(true)
42                        .create(true)
43                        .truncate(true)
44                        .open(TRACKS_FILE)?;
45
46                    // Serialize tracks into the file using bincode
47                    let bincode_conf = bincode::config::standard();
48                    bincode::encode_into_std_write(tracks, &mut file, bincode_conf)?;
49                    // See examples/play.rs for deserializing and playback
50                    println!("Tracks saved to {}", TRACKS_FILE);
51                }
52            }
53            println!("{:?}", event);
54        }
55
56        if !paused {
57            current_row += 1;
58            rocket.set_row(current_row)?;
59        }
60
61        std::thread::sleep(Duration::from_millis(32));
62    }
63}
Source

pub fn set_row(&mut self, row: u32) -> Result<(), Error>

Send a SetRow message.

This changes the current row on the tracker side.

§Errors

This method can return an Error::IOError if Rocket tracker disconnects.

Examples found in repository?
examples/edit.rs (line 58)
8fn main() -> Result<(), Box<dyn Error>> {
9    let mut rocket = RocketClient::new()?;
10    rocket.get_track_mut("test")?;
11    rocket.get_track_mut("test2")?;
12    rocket.get_track_mut("a:test2")?;
13
14    let mut current_row = 0;
15    let mut paused = true;
16
17    loop {
18        if let Some(event) = rocket.poll_events()? {
19            match event {
20                Event::SetRow(row) => {
21                    println!("SetRow (row: {:?})", row);
22                    current_row = row;
23                }
24                Event::Pause(state) => {
25                    paused = state;
26
27                    let track1 = rocket.get_track("test").unwrap();
28                    println!(
29                        "Pause (value: {:?}) (row: {:?})",
30                        track1.get_value(current_row as f32),
31                        current_row
32                    );
33                }
34                Event::SaveTracks => {
35                    // Obtain a clone of current track state
36                    let tracks = rocket.save_tracks();
37
38                    // Open a file for writing, create if not present,
39                    // truncate (overwrite) in case it has previous contents.
40                    let mut file = OpenOptions::new()
41                        .write(true)
42                        .create(true)
43                        .truncate(true)
44                        .open(TRACKS_FILE)?;
45
46                    // Serialize tracks into the file using bincode
47                    let bincode_conf = bincode::config::standard();
48                    bincode::encode_into_std_write(tracks, &mut file, bincode_conf)?;
49                    // See examples/play.rs for deserializing and playback
50                    println!("Tracks saved to {}", TRACKS_FILE);
51                }
52            }
53            println!("{:?}", event);
54        }
55
56        if !paused {
57            current_row += 1;
58            rocket.set_row(current_row)?;
59        }
60
61        std::thread::sleep(Duration::from_millis(32));
62    }
63}
Source

pub fn poll_events(&mut self) -> Result<Option<Event>, Error>

Poll for new events from the tracker.

This polls from events from the tracker. You should call this fairly often your main loop. It is recommended to keep calling this as long as your receive Some(Event).

§Errors

This method can return an Error::IOError if the rocket tracker disconnects.

§Examples
while let Some(event) = rocket.poll_events()? {
    match event {
        // Do something with the various events.
        _ => (),
    }
}
Examples found in repository?
examples/edit.rs (line 18)
8fn main() -> Result<(), Box<dyn Error>> {
9    let mut rocket = RocketClient::new()?;
10    rocket.get_track_mut("test")?;
11    rocket.get_track_mut("test2")?;
12    rocket.get_track_mut("a:test2")?;
13
14    let mut current_row = 0;
15    let mut paused = true;
16
17    loop {
18        if let Some(event) = rocket.poll_events()? {
19            match event {
20                Event::SetRow(row) => {
21                    println!("SetRow (row: {:?})", row);
22                    current_row = row;
23                }
24                Event::Pause(state) => {
25                    paused = state;
26
27                    let track1 = rocket.get_track("test").unwrap();
28                    println!(
29                        "Pause (value: {:?}) (row: {:?})",
30                        track1.get_value(current_row as f32),
31                        current_row
32                    );
33                }
34                Event::SaveTracks => {
35                    // Obtain a clone of current track state
36                    let tracks = rocket.save_tracks();
37
38                    // Open a file for writing, create if not present,
39                    // truncate (overwrite) in case it has previous contents.
40                    let mut file = OpenOptions::new()
41                        .write(true)
42                        .create(true)
43                        .truncate(true)
44                        .open(TRACKS_FILE)?;
45
46                    // Serialize tracks into the file using bincode
47                    let bincode_conf = bincode::config::standard();
48                    bincode::encode_into_std_write(tracks, &mut file, bincode_conf)?;
49                    // See examples/play.rs for deserializing and playback
50                    println!("Tracks saved to {}", TRACKS_FILE);
51                }
52            }
53            println!("{:?}", event);
54        }
55
56        if !paused {
57            current_row += 1;
58            rocket.set_row(current_row)?;
59        }
60
61        std::thread::sleep(Duration::from_millis(32));
62    }
63}

Trait Implementations§

Source§

impl Debug for RocketClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.