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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#[macro_use]
extern crate log;
extern crate env_logger;

extern crate rocket_sync;

use std::{fmt, str};
use std::fmt::Display;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::error::Error;

use rocket_sync::{TrackKey, SyncDevice, code_to_key};

pub mod utils;
use utils::*;

const CLIENT_GREET: &'static str = "hello, synctracker!";
const SERVER_GREET: &'static str = "hello, demo!";
const SERVER_GREET_LEN: usize = 12;

pub struct SyncClient {
    stream: TcpStream,
}

pub enum SyncCmd {
    SetKey,
    DeleteKey,
    GetTrack,
    SetRow,
    Pause,
    SaveTracks,
    NOOP,
}

impl SyncClient {

    /// Connects to the Rocket Editor server and shakes hand with it
    pub fn new(address: &str) -> Result<SyncClient, Box<Error>> {
        info!("Attempt to connect to Rocket");
        let mut stream = match TcpStream::connect(address) {
            Ok(x) => {
                info!("Connected to Rocket");
                x
            },
            Err(e) => {
                info!("Couldn't connect to Rocket");
                return Err(Box::new(e));
            },
        };

        // handshake

        // send greeting
        stream.write(CLIENT_GREET.as_bytes())?;

        // receive response
        let mut buf = [0; SERVER_GREET_LEN];
        match stream.read_exact(&mut buf) {
            Ok(_) => {
                match str::from_utf8(&buf) {
                    Ok(x) => {
                        let resp = String::from(x);
                        if String::from(SERVER_GREET) != resp {
                            return Err(Box::new(SyncError::BadServerGreeting));
                        }
                    },
                    Err(_) => {
                        // invalid response, can't parse as utf8
                        return Err(Box::new(SyncError::CantParseGreeting));
                    }
                }
            },

            Err(_) => {
                return Err(Box::new(SyncError::CouldNotReadFromServer));
            }
        }

        info!("Handshake completed");

        Ok(SyncClient{stream: stream})
    }

    /// Read from the stream and process commands until the server runs out of
    /// things to send.
    ///
    /// Returns an Ok(true) if there should be a redraw.
    pub fn update(&mut self, mut device: &mut SyncDevice) -> Result<bool, Box<Error>> {

        let mut draw_anyway = false;

        // Use nonblocking when receiving commands, otherwise it stalls the
        // application until the user does something in the editor again. When
        // the server is out of things to say, reading will error and we will
        // know to stop.
        //
        // Stop nonblocking in the command handlers, take as many bytes as
        // expected for the command, then resume nonblocking until the end of
        // update().

        self.stream.set_nonblocking(true)?;

        let mut is_sending = true;
        while is_sending {
            // this better be an u8 command code
            let mut cmd_buf = [0; 1];

            match self.stream.read(&mut cmd_buf) {
                Ok(n) => {
                    // nothing to read
                    if n == 0 {
                        is_sending = false;
                    }

                    use self::SyncCmd::*;
                    match code_to_cmd(cmd_buf[0]) {
                        NOOP => info!("Received: CMD NOOP, byte {}", cmd_buf[0]),

                        SetKey => self.handle_set_key_cmd(&mut device)?,

                        DeleteKey => self.handle_del_key_cmd(&mut device)?,

                        GetTrack => {
                            info!("Received: CMD GetTrack");
                            info!("TODO handle GetTrack");
                        },

                        SetRow => self.handle_set_row_cmd(&mut device)?,

                        Pause => self.handle_pause_cmd(&mut device)?,

                        SaveTracks => {
                            info!("Received: CMD SaveTracks");
                            info!("TODO handle SaveTracks");
                        },
                    }

                    draw_anyway = match code_to_cmd(cmd_buf[0]) {
                        NOOP => false,
                        SetKey => true,
                        DeleteKey => true,
                        GetTrack => false,
                        SetRow => true,
                        Pause => true,
                        SaveTracks => false,
                    };
                },

                // read error, probably nothing to read
                Err(_) => is_sending = false,
            }
        }

        self.stream.set_nonblocking(false)?;

        Ok(draw_anyway)
    }

    pub fn send_row(&mut self, device: &SyncDevice) -> Result<(), Box<Error>> {
        let buf = [cmd_to_code(&SyncCmd::SetRow)];
        self.stream.write(&buf)?;

        let buf = u32_to_net(device.row);

        info!("Send row: {}, bytes: {:?}", device.row, buf);
        self.stream.write(&buf)?;

        Ok(())
    }

    /// Send track names to Rocket, including group prefix
    pub fn send_track_names(&mut self, track_names: &Vec<String>) -> Result<(), Box<Error>> {
        info!("Sending track names: {:#?}", track_names);

        for name in track_names.iter() {
            info!("Send CMD Get Track: \"{}\"", name);

            // send get track command
            let buf = [cmd_to_code(&SyncCmd::GetTrack)];
            self.stream.write(&buf)?;

            // send track name length
            let buf = u32_to_net(name.len() as u32);
            self.stream.write(&buf)?;

            // send track name
            let buf = name.as_bytes();
            self.stream.write(buf)?;
        }

        Ok(())
    }

    /// Adds a key frame to a track
    pub fn handle_set_key_cmd(&mut self, device: &mut SyncDevice) -> Result<(), Box<Error>> {
        info!("Received: CMD SetKey");

        self.stream.set_nonblocking(false)?;

        let mut track_key: TrackKey = TrackKey::new();

        // track index (to indetify in the Vec<SyncTrack>)
        let mut buf = [0; 4];
        self.stream.read_exact(&mut buf)?;
        let track_idx: usize = net_to_u32(&buf) as usize;

        // row of the key
        let mut buf = [0; 4];
        self.stream.read_exact(&mut buf)?;
        track_key.row = net_to_u32(&buf);

        // value of the key
        let mut buf = [0; 4];
        self.stream.read_exact(&mut buf)?;
        track_key.value = net_to_f32(&buf);

        // interpolation type of the key
        let mut buf = [0; 1];
        self.stream.read_exact(&mut buf)?;
        track_key.key_type = code_to_key(buf[0]);

        self.stream.set_nonblocking(true)?;

        // add the key to the track if it exists

        if device.tracks.len() == 0 {
            return Err(Box::new(SyncError::NoTracks));
        }

        if track_idx >= device.tracks.len() {
            return Err(Box::new(SyncError::TrackNotFound));
        }

        device.tracks[track_idx].add_key(track_key);

        Ok(())
    }

    /// Deletes a key from a track
    pub fn handle_del_key_cmd(&mut self, device: &mut SyncDevice) -> Result<(), Box<Error>> {
        info!("Received: CMD DeleteKey");

        self.stream.set_nonblocking(false)?;

        // track index (to indetify in the Vec<SyncTrack>)
        let mut buf = [0; 4];
        self.stream.read_exact(&mut buf)?;
        let track_idx: usize = net_to_u32(&buf) as usize;

        // row of the key
        let mut buf = [0; 4];
        self.stream.read_exact(&mut buf)?;
        let row: u32 = net_to_u32(&buf);

        self.stream.set_nonblocking(true)?;

        if device.tracks.len() == 0 {
            return Err(Box::new(SyncError::NoTracks));
        }

        if track_idx >= device.tracks.len() {
            return Err(Box::new(SyncError::TrackNotFound));
        }

        device.tracks[track_idx].delete_key(row);

        Ok(())
    }

    /// Sets the current row from server. Sets the current time based on the row
    /// and rps.
    pub fn handle_set_row_cmd(&mut self, device: &mut SyncDevice) -> Result<(), Box<Error>> {
        info!("Received: CMD SetRow");

        self.stream.set_nonblocking(false)?;

        // get four bytes, sent as big-endian (network byte order)
        let mut buf = [0; 4];
        self.stream.read_exact(&mut buf)?;

        self.stream.set_nonblocking(true)?;

        device.row = net_to_u32(&buf);
        device.time = ms_from_row_rps(device.row, device.rps);
        info!("bytes: {:?}", buf);
        info!("row: {}", device.row);
        info!("time: {}", device.time);

        Ok(())
    }

    pub fn handle_pause_cmd(&mut self, device: &mut SyncDevice) -> Result<(), Box<Error>> {
        info!("Received: CMD Pause");

        self.stream.set_nonblocking(false)?;

        // get one byte, value 1 means paused
        let mut buf = [0; 1];
        self.stream.read_exact(&mut buf)?;

        self.stream.set_nonblocking(true)?;

        device.is_paused = { buf[0] == 1 };
        info!("bytes: {:?}", buf);
        info!("is_paused: {:?}", device.is_paused);

        Ok(())
    }
}

#[derive(Debug)]
pub enum SyncError {
    NotConnected,
    CouldNotConnect,
    BadServerGreeting,
    CantParseGreeting,
    CouldNotReadFromServer,
    TrackNotFound,
    NoTracks,
}

impl Display for SyncError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "SyncError: {:?}", self)
    }
}

impl Error for SyncError {
    fn description (&self) -> &str {
        "tell me about it"
    }
}