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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Folding@home client API wrapper for Rust. Use
//! [`API::connect_timeout()`](./struct.API.html#method.connect_timeout) to connect to your FAH
//! client.
//!
//! [rust-fahapi on Github](https://github.com/MakotoE/rust-fahapi)

mod connection;
mod types;

pub use connection::*;
pub use types::*;

pub use anyhow::{Error, Result};
use std::net;

lazy_static::lazy_static! {
    /// Default TCP address of the FAH client.
    pub static ref DEFAULT_ADDR: net::SocketAddr = {
        net::SocketAddr::V4(net::SocketAddrV4::new(net::Ipv4Addr::LOCALHOST, 36330))
    };
}

/// Wrapper for the FAH API. Use API::connect_timeout() to initialize.
///
/// Example
/// ```no_run
/// fn example() -> fahapi::Result<()> {
///     let mut api = fahapi::API::connect_timeout(&fahapi::DEFAULT_ADDR, std::time::Duration::from_secs(1))?;
///     api.pause_all()?;
///     api.unpause_all()
/// }
/// ```
#[derive(Debug)]
pub struct API {
    pub conn: Connection,
    pub buf: Vec<u8>,
}

impl API {
    /// Connects to your FAH client with a timeout. `DEFAULT_ADDR` is the default address.
    pub fn connect_timeout(addr: &net::SocketAddr, timeout: core::time::Duration) -> Result<API> {
        Ok(API {
            conn: Connection::connect_timeout(addr, timeout)?,
            buf: Vec::new(),
        })
    }

    /// Returns a listing of the FAH API commands.
    pub fn help(&mut self) -> Result<String> {
        self.conn.exec("help", &mut self.buf)?;
        Ok(std::str::from_utf8(self.buf.as_slice())?.to_string())
    }

    /// Enables or disables log updates. Returns current log.
    pub fn log_updates(&mut self, arg: LogUpdatesArg) -> Result<String> {
        /*
            This command is weird. It returns the log after the next prompt, like this:
            > log-updates start

            >
            PyON 1 log-update...
        */

        let command = format!("log-updates {}", arg);
        self.conn.exec(command.as_str(), &mut self.buf)?;
        self.conn.exec_eval("eval", &mut self.buf)?;

        // The string contains a bunch of \x00 sequences that are not valid JSON and cannot be
        // parsed using parse_pyon().
        parse_log(std::str::from_utf8(&self.buf)?)
    }

    /// Unpauses all slots which are paused waiting for a screensaver and pause them again on
    /// disconnect.
    pub fn screensaver(&mut self) -> Result<()> {
        self.conn.exec("screensaver", &mut self.buf)
    }

    /// Sets a slot to be always on.
    pub fn always_on(&mut self, slot: i64) -> Result<()> {
        let command = format!("always_on {}", slot);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    /// Returns true if the client has set a user, team or passkey.
    pub fn configured(&mut self) -> Result<bool> {
        self.conn.exec("configured", &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(&s)?.as_str())?)
    }

    /// Runs one client cycle.
    pub fn do_cycle(&mut self) -> Result<()> {
        self.conn.exec("do-cycle", &mut self.buf)
    }

    /// Pauses a slot when its current work unit is completed.
    pub fn finish_slot(&mut self, slot: i64) -> Result<()> {
        let command = format!("finish {}", slot);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    /// Pauses all slots one-by-one when their current work unit is completed.
    pub fn finish_all(&mut self) -> Result<()> {
        self.conn.exec("finish", &mut self.buf)
    }

    /// Returns FAH build and machine info. See `info_struct()`.
    pub fn info(&mut self) -> Result<Vec<Vec<serde_json::Value>>> {
        self.conn.exec("info", &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(s)?.as_str())?)
    }

    /// Converts Info() data into a structure. Consider this interface to be very unstable.
    pub fn info_struct(&mut self) -> Result<Info> {
        Info::new(self.info()?)
    }

    /// Returns the number of slots.
    pub fn num_slots(&mut self) -> Result<i64> {
        self.conn.exec("num-slots", &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(s)?.as_str())?)
    }

    /// Sets a slot to run only when idle.
    pub fn on_idle(&mut self, slot: i64) -> Result<()> {
        let command = format!("on_idle {}", slot);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    /// Sets all slots to run only when idle.
    pub fn on_idle_all(&mut self) -> Result<()> {
        self.conn.exec("on_idle", &mut self.buf)
    }

    /// Returns the FAH client options.
    pub fn options_get(&mut self) -> Result<Options> {
        self.conn.exec("options -a", &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(s)?.as_str())?)
    }

    /// Sets an option.
    pub fn options_set<N>(&mut self, key: &str, value: N) -> Result<()>
    where
        N: std::fmt::Display,
    {
        let value_str = format!("{}", value);

        if key.contains(&['=', ' ', '!'] as &[char]) || value_str.contains(' ') {
            return Err(Error::msg(format!(
                "key or value contains bad character: {}={}",
                key, value
            )));
        }

        let command = format!("options {}={}", key, value_str);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    /// Pauses all slots.
    pub fn pause_all(&mut self) -> Result<()> {
        self.conn.exec("pause", &mut self.buf)
    }

    /// Pauses a slot.
    pub fn pause_slot(&mut self, slot: i64) -> Result<()> {
        let command = format!("pause {}", slot);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    // Returns the total estimated points per day.
    pub fn ppd(&mut self) -> Result<f64> {
        self.conn.exec("ppd", &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(s)?.as_str())?)
    }

    /// Returns info about the current work unit.
    pub fn queue_info(&mut self) -> Result<Vec<SlotQueueInfo>> {
        self.conn.exec("queue-info", &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        let json = pyon_to_json(s)?;
        serde_json::from_str(&json).map_err(|e| Error::new(e).context(json))
    }

    /// Requests an ID from the assignment server.
    pub fn request_id(&mut self) -> Result<()> {
        self.conn.exec("request-id", &mut self.buf)
    }

    /// Requests work server assignment from the assignment server.
    pub fn request_ws(&mut self) -> Result<()> {
        self.conn.exec("request-ws", &mut self.buf)
    }

    /// Ends all FAH processes.
    pub fn shutdown(&mut self) -> Result<()> {
        self.conn.exec("shutdown", &mut self.buf)
    }

    /// Returns the simulation information for a slot.
    pub fn simulation_info(&mut self, slot: i64) -> Result<SimulationInfo> {
        // "just like the simulations"
        let command = format!("simulation-info {}", slot);
        self.conn.exec(command.as_str(), &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(s)?.as_str())?)
    }

    /// Deletes a slot.
    pub fn slot_delete(&mut self, slot: i64) -> Result<()> {
        let command = format!("slot-delete {}", slot);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    /// Returns information about each slot.
    pub fn slot_info(&mut self) -> Result<Vec<SlotInfo>> {
        self.conn.exec("slot-info", &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(s)?.as_str())?)
    }

    /// Returns slot options.
    pub fn slot_options_get(&mut self, slot: i64) -> Result<SlotOptions> {
        let command = format!("slot-options {} -a", slot);
        self.conn.exec(command.as_str(), &mut self.buf)?;
        let s = std::str::from_utf8(&self.buf)?;
        Ok(serde_json::from_str(pyon_to_json(s)?.as_str())?)
    }

    /// Sets slot option.
    pub fn slot_options_set<N>(&mut self, slot: i64, key: &str, value: N) -> Result<()>
    where
        N: std::fmt::Display,
    {
        let command = format!("slot-options {} {} {}", slot, key, value);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    /// Unpauses all slots.
    pub fn unpause_all(&mut self) -> Result<()> {
        self.conn.exec("unpause", &mut self.buf)
    }

    /// Unpauses a slot.
    pub fn unpause_slot(&mut self, slot: i64) -> Result<()> {
        let command = format!("unpause {}", slot);
        self.conn.exec(command.as_str(), &mut self.buf)
    }

    /// Returns FAH uptime.
    pub fn uptime(&mut self) -> Result<FAHDuration> {
        self.conn.exec_eval("uptime", &mut self.buf)?;
        let duration = humantime::parse_duration(std::str::from_utf8(&self.buf)?)?;
        Ok(chrono::Duration::from_std(duration)?.into())
    }

    /// Blocks until all slots are paused.
    pub fn wait_for_units(&mut self) -> Result<()> {
        self.conn.exec("wait-for-units", &mut self.buf)
    }
}

#[derive(Debug, Copy, Clone)]
pub enum LogUpdatesArg {
    Start,
    Restart,
    Stop,
}

impl std::fmt::Display for LogUpdatesArg {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> core::fmt::Result {
        let s = match self {
            LogUpdatesArg::Start => "start",
            LogUpdatesArg::Restart => "restart",
            LogUpdatesArg::Stop => "stop",
        };
        write!(f, "{}", s)
    }
}

pub fn parse_log(s: &str) -> Result<String> {
    // The log looks like this: PyON 1 log-update\n"..."\n---\n\n
    const SUFFIX: &str = "\n---\n\n";

    let mut removed_suffix = s;
    if s.len() > SUFFIX.len() && s[s.len() - SUFFIX.len()..] == *SUFFIX {
        removed_suffix = &s[..s.len() - SUFFIX.len()]
    }

    let start = match removed_suffix.find('\n') {
        Some(i) => i + 1,
        None => 0,
    };
    parse_pyon_string(&removed_suffix[start..])
}

pub fn parse_pyon_string(s: &str) -> Result<String> {
    if s.len() < 2 || s.bytes().next().unwrap() != b'"' || s.bytes().nth_back(0).unwrap() != b'"' {
        return Err(Error::msg(format!("cannot parse {}", s)));
    }

    lazy_static::lazy_static! {
        static ref MATCH_ESCAPED: regex::Regex = regex::Regex::new(r#"\\x..|\\n|\\r|\\"|\\\\"#).unwrap();
    }

    let replace_fn: fn(&regex::Captures) -> String = |caps: &regex::Captures| {
        let capture = &caps[0];
        if capture.bytes().next().unwrap() == b'\\' {
            return match capture.bytes().nth(1).unwrap() {
                b'n' => "\n".to_string(),
                b'r' => "\r".to_string(),
                b'"' => "\"".to_string(),
                b'\\' => "\\".to_string(),
                b'x' => {
                    let hex: String = capture.chars().skip(2).collect();
                    let n = match u32::from_str_radix(hex.as_str(), 16) {
                        Ok(n) => n,
                        Err(_) => return capture.to_string(),
                    };

                    match std::char::from_u32(n) {
                        Some(c) => c.to_string(),
                        None => capture.to_string(),
                    }
                }
                _ => capture.to_string(),
            };
        }

        capture.to_string()
    };

    Ok((*MATCH_ESCAPED.replace_all(&s[1..s.len() - 1], replace_fn)).to_string())
}

pub fn pyon_to_json(s: &str) -> Result<String> {
    // https://pypi.org/project/pon/
    const PREFIX: &str = "PyON";
    const SUFFIX: &str = "\n---";
    if s.len() < PREFIX.len()
        || s.bytes().take(PREFIX.len()).ne(PREFIX.bytes())
        || s.len() < SUFFIX.len()
        || s.bytes().skip(s.len() - SUFFIX.len()).ne(SUFFIX.bytes())
    {
        return Err(Error::msg(format!("invalid PyON format: {}", s)));
    }

    let mut start = match s.find('\n') {
        Some(i) => i + 1,
        None => 0,
    };

    let end = s.len() - SUFFIX.len();
    if start > end {
        start = end;
    }

    Ok(match &s[start..end] {
        "True" => "true".to_string(),
        "False" => "false".to_string(),
        _ => s[start..end]
            .replace(": None", r#": """#)
            .replace(": False", ": false")
            .replace(": True", ": true"),
    })
}

#[cfg(test)]
mod tests {
    #[allow(unused_imports)]
    use super::*;

    #[test]
    fn test_parse_log() {
        struct Test {
            s: &'static str,
            expected: &'static str,
            expect_error: bool,
        }

        let tests = vec![
            Test {
                s: "",
                expected: "",
                expect_error: true,
            },
            Test {
                s: r#"PyON 1 log-update"#,
                expected: "",
                expect_error: true,
            },
            Test {
                s: r#""""#,
                expected: "",
                expect_error: false,
            },
            Test {
                s: r#"\n---\n\n"#,
                expected: "",
                expect_error: true,
            },
            Test {
                s: "\n\"\"\n---\n\n",
                expected: "",
                expect_error: false,
            },
            Test {
                s: "PyON 1 log-update\n\n---\n\n",
                expected: "",
                expect_error: true,
            },
            Test {
                s: "PyON 1 log-update\n\"a\"\n---\n\n",
                expected: "a",
                expect_error: false,
            },
        ];

        for (i, test) in tests.iter().enumerate() {
            let result = parse_log(test.s);
            assert_eq!(result.is_err(), test.expect_error, "{}", i);
            if !test.expect_error {
                assert_eq!(result.unwrap(), test.expected, "{}", i);
            }
        }
    }

    #[test]
    fn test_parse_pyon_string() {
        struct Test {
            s: &'static str,
            expected: &'static str,
            expect_error: bool,
        }

        let tests = vec![
            Test {
                s: "",
                expected: "",
                expect_error: true,
            },
            Test {
                s: r#""""#,
                expected: "",
                expect_error: false,
            },
            Test {
                s: r#""\n\"\\\x01""#,
                expected: "\n\"\\\x01",
                expect_error: false,
            },
            Test {
                s: r#""a\x01a""#,
                expected: "a\x01a",
                expect_error: false,
            },
        ];

        for (i, test) in tests.iter().enumerate() {
            let result = parse_pyon_string(test.s);
            assert_eq!(result.is_err(), test.expect_error, "{}", i);
            if !test.expect_error {
                assert_eq!(result.unwrap(), test.expected, "{}", i);
            }
        }
    }

    #[test]
    fn test_pyon_to_json() {
        struct Test {
            s: &'static str,
            expected: &'static str,
            expect_error: bool,
        }

        let tests = vec![
            Test {
                s: "",
                expected: "",
                expect_error: true,
            },
            Test {
                s: "PyON",
                expected: "",
                expect_error: true,
            },
            Test {
                s: "PyON\n---",
                expected: "",
                expect_error: false,
            },
            Test {
                s: "PyON\n\n---",
                expected: "",
                expect_error: false,
            },
            Test {
                s: "PyON\n1\n---",
                expected: "1",
                expect_error: false,
            },
            Test {
                s: "PyON\nTrue\n---",
                expected: "true",
                expect_error: false,
            },
            Test {
                s: "PyON\n{\"\": None}\n---",
                expected: "{\"\": \"\"}",
                expect_error: false,
            },
            Test {
                s: "\n}÷",
                expected: "",
                expect_error: true,
            },
        ];

        for (i, test) in tests.iter().enumerate() {
            let result = pyon_to_json(test.s);
            assert_eq!(result.is_err(), test.expect_error, "{}", i);
            if !test.expect_error {
                assert_eq!(result.unwrap(), test.expected, "{}", i);
            }
        }
    }
}

bencher::benchmark_group!(benches, bench_pyon_to_json);
bencher::benchmark_main!(benches);

fn bench_pyon_to_json(b: &mut bencher::Bencher) {
    // test bench_pyon_to_json ... bench:          33 ns/iter (+/- 1)
    b.iter(|| pyon_to_json("PyON\nFalse\n---"))
}

#[cfg(test)]
mod integration_tests;