cg-local-app 0.1.2

Rust application for CG Local
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! Rust implementation of the client-side application for the [CG
//! Local](https://www.codingame.com/forum/t/cg-local/10359) extension. This is a drop-in
//! replacement for the original [Java application](https://github.com/jmerle/cg-local-app) which
//! works with the original [browser extension](https://github.com/jmerle/cg-local-ext).
//!
//! ## Install
//!
//! ### Pre-built packages
//!
//! Check the [releases](https://github.com/vtavernier/cg-local-app.rs/releases) for binaries from
//! your operating system.
//!
//! ### Using cargo
//!
//! ```bash
//! cargo install --force cg-local-app
//! ```
//!
//! ### From source
//!
//! ```bash
//! git clone https://github.com/vtavernier/cg-local-app.rs.git && cd cg-local-app.rs
//! cargo install --path .
//! ```
//!
//! ## Usage
//!
//! ```
//! cg-local-app 0.1.2
//! Vincent Tavernier <vince.tavernier@gmail.com>
//! Rust application for CG Local
//!
//! USAGE:
//!     cg-local-app [FLAGS] [OPTIONS] --target <target>
//!
//! FLAGS:
//!     -d, --download    Download the file from the IDE before synchronizing
//!     -h, --help        Prints help information
//!         --no-gui      Disable text user interface
//!     -p, --play        Auto-play questions on upload
//!     -V, --version     Prints version information
//!
//! OPTIONS:
//!     -b, --bind <bind>        Address to bind to for the extension. Shouldn't need to be changed [default: 127.0.0.1:53135]
//!     -t, --target <target>    Path to the target file to synchronize with the IDE
//! ```
//!
//! ## Examples
//!
//! ```bash
//! # Synchronize main.rs with the IDE, enable auto-play by default
//! cg-local-app -p -t main.rs
//! ```
//!
//! ## Status
//!
//! Missing features:
//! * Two-way synchronization

#![recursion_limit = "512"]

#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;

use std::net::SocketAddr;

use error_chain::error_chain;

use structopt::StructOpt;

use hotwatch::{Event, Hotwatch};

use futures_util::future::FutureExt;
use futures_util::select;
use futures_util::sink::SinkExt;

use async_std::{
    net::{TcpListener, TcpStream, ToSocketAddrs},
    path::PathBuf,
    prelude::*,
    sync::{Arc, Mutex},
    task,
};

use async_tungstenite::tungstenite;

#[derive(Debug, StructOpt)]
#[structopt(author, about)]
pub struct Opts {
    /// Address to bind to for the extension. Shouldn't need to be changed.
    #[structopt(short, long, default_value = "127.0.0.1:53135")]
    bind: String,

    /// Path to the target file to synchronize with the IDE.
    #[structopt(short, long)]
    target: PathBuf,

    /// Download the file from the IDE before synchronizing.
    #[structopt(short, long)]
    download: bool,

    /// Auto-play questions on upload.
    #[structopt(short, long)]
    play: bool,

    /// Disable text user interface
    #[structopt(long)]
    no_gui: bool,
}

error_chain! {
    foreign_links {
        Io(std::io::Error);
        Hotwatch(hotwatch::Error);
        WebSocket(tungstenite::Error);
        WorkerNotificationChannel(std::sync::mpsc::SendError<WorkerNotification>);
        ConnectedNotificationChannel(async_std::channel::SendError<ConnectedNotification>);
        WorkerMessageChannel(async_std::channel::SendError<WorkerMessage>);
        ConnectedMessageChannel(async_std::channel::SendError<ConnectedMessage>);
        ListenMessageChannel(async_std::channel::SendError<ListenMessage>);
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "action", content = "payload", rename_all = "kebab-case")]
pub enum ServerMessage {
    SendDetails,
    #[serde(rename_all = "camelCase")]
    Details {
        title: String,
        question_id: i32,
    },
    AppReady,
    AlreadyConnected,
    UpdateCode {
        code: String,
        play: bool,
    },
    SendCode,
    Code {
        code: String,
    },
    SetReadOnly {
        state: bool,
    },
    Error {
        message: String,
    },
}

impl Into<tungstenite::Message> for ServerMessage {
    fn into(self) -> tungstenite::Message {
        tungstenite::Message::Text(serde_json::to_string(&self).unwrap())
    }
}

pub struct State {
    opts: Arc<Mutex<Opts>>,
}

impl State {
    pub fn new(opts: Arc<Mutex<Opts>>) -> Self {
        Self { opts }
    }
}

async fn handle_accept(
    peer: SocketAddr,
    stream: TcpStream,
    rx_connected: Arc<Mutex<async_std::channel::Receiver<ConnectedMessage>>>,
    tx_conn_notification: async_std::channel::Sender<ConnectedNotification>,
) -> Result<()> {
    let mut ws_stream = async_tungstenite::accept_async(stream).await?;

    info!("accepting connection from {}", peer);

    ws_stream.send(ServerMessage::SendDetails.into()).await?;

    loop {
        let mut rx_ws_lock = rx_connected.lock().await;

        select! {
            msg = ws_stream.next().fuse() => {
                if let Some(msg) = msg {
                    let msg = msg?;
                    debug!("msg: {:?}", msg);

                    if let tungstenite::Message::Text(msg) = msg {
                        let parsed: std::result::Result<ServerMessage, _> = serde_json::from_str(&msg);

                        match parsed {
                            Ok(msg) => match msg {
                                ServerMessage::Details { title, question_id } => {
                                    tx_conn_notification.send(ConnectedNotification::Details { title, question_id }).await?
                                }
                                ServerMessage::Code { code } => {
                                    tx_conn_notification.send(ConnectedNotification::Code { code }).await?
                                }
                                other => {
                                    warn!("unexpected message: {:?}", other);
                                    ws_stream.send(ServerMessage::Error { message: format!("unexpected message") }.into()).await?
                                }
                            },
                            Err(err) => {
                                error!("failed to parse message: {}", err);
                                ws_stream.send(ServerMessage::Error { message: err.to_string() }.into()).await?
                            }
                        }
                    }
                } else {
                    break;
                }
            }

            msg = rx_ws_lock.next().fuse() => {
                drop(rx_ws_lock);

                if let Some(msg) = msg {
                    match msg {
                        ConnectedMessage::AppReady => {
                            ws_stream.send(ServerMessage::AppReady.into()).await?;
                        }
                        ConnectedMessage::UpdateCode { code, play } => {
                            ws_stream.send(ServerMessage::UpdateCode { code, play }.into()).await?;
                        }
                        ConnectedMessage::SendCode => {
                            ws_stream.send(ServerMessage::SendCode.into()).await?;
                        }
                        ConnectedMessage::Terminate => { break; }
                    }
                } else {
                    break;
                }
            }
        }
    }

    Ok(())
}

async fn accept_connection(
    peer: SocketAddr,
    stream: TcpStream,
    rx_connected: Arc<Mutex<async_std::channel::Receiver<ConnectedMessage>>>,
    tx_conn_notification: async_std::channel::Sender<ConnectedNotification>,
) -> Result<()> {
    if let Err(e) = handle_accept(peer, stream, rx_connected, tx_conn_notification).await {
        match e {
            Error(ErrorKind::WebSocket(tungstenite::Error::ConnectionClosed), _)
            | Error(ErrorKind::WebSocket(tungstenite::Error::Protocol(_)), _)
            | Error(ErrorKind::WebSocket(tungstenite::Error::Utf8), _) => (),
            err => error!("error processing connection: {:?}", err),
        }
    }

    Ok(())
}

async fn handle_deny(peer: SocketAddr, stream: TcpStream) -> Result<()> {
    let mut ws_stream = async_tungstenite::accept_async(stream).await?;

    info!("denying connection from {}", peer);
    ws_stream
        .send(ServerMessage::AlreadyConnected.into())
        .await?;

    ws_stream.close(None).await?;

    Ok(())
}

async fn deny_connection(peer: SocketAddr, stream: TcpStream) -> Result<()> {
    if let Err(e) = handle_deny(peer, stream).await {
        match e {
            Error(ErrorKind::WebSocket(tungstenite::Error::ConnectionClosed), _)
            | Error(ErrorKind::WebSocket(tungstenite::Error::Protocol(_)), _)
            | Error(ErrorKind::WebSocket(tungstenite::Error::Utf8), _) => (),
            err => error!("error processing connection: {:?}", err),
        }
    }

    Ok(())
}

#[derive(Debug)]
pub enum ConnectedMessage {
    AppReady,
    UpdateCode { code: String, play: bool },
    SendCode,
    Terminate,
}

#[derive(Debug)]
pub enum WorkerMessage {
    FileChanged { code: String },
    WatchError { error: std::io::Error },
    Start { download: bool },
    Stop,
    Terminate,
}

#[derive(Debug)]
pub enum WorkerNotification {
    Details { title: String, question_id: i32 },
    Initialized,
    Stopped,
    Terminate,
}

#[derive(Debug)]
pub enum ConnectedNotification {
    Details { title: String, question_id: i32 },
    Code { code: String },
}

#[derive(Debug)]
pub enum ListenMessage {
    Terminate,
}

async fn run_accept(
    rx_connected: async_std::channel::Receiver<ConnectedMessage>,
    mut rx_listen: async_std::channel::Receiver<ListenMessage>,
    tx_conn_notification: async_std::channel::Sender<ConnectedNotification>,
    addr: impl ToSocketAddrs + std::fmt::Display,
) -> Result<()> {
    let listener = TcpListener::bind(&addr).await?;
    info!("listening on {}", addr);

    let res = semaphore::Semaphore::new(1, ());
    let rx_connected = Arc::new(Mutex::new(rx_connected));

    loop {
        select! {
            accepted = listener.accept().fuse() => {
                if let Ok((stream, _)) = accepted {
                    let peer = stream.peer_addr()?;

                    match res.try_access() {
                        Ok(_) => {
                            task::spawn(accept_connection(
                                peer,
                                stream,
                                rx_connected.clone(),
                                tx_conn_notification.clone(),
                            ));
                        }
                        Err(semaphore::TryAccessError::NoCapacity) => {
                            task::spawn(deny_connection(peer, stream));
                        }
                        Err(_) => break,
                    }
                } else {
                    break;
                }
            },

            terminated = rx_listen.next().fuse() => {
                match terminated {
                    None | Some(ListenMessage::Terminate) => { break; }
                }
            }
        }
    }

    Ok(())
}

async fn run_controller(
    state: State,
    tx_connected: async_std::channel::Sender<ConnectedMessage>,
    tx_listen: async_std::channel::Sender<ListenMessage>,
    mut rx_controller: async_std::channel::Receiver<WorkerMessage>,
    tx_notification: std::sync::mpsc::Sender<WorkerNotification>,
    mut rx_conn_notification: async_std::channel::Receiver<ConnectedNotification>,
) -> Result<()> {
    let mut send_code_pending = false;

    loop {
        select! {
            msg = rx_controller.next().fuse() => {
                trace!("msg: {:?}", msg);

                if let Some(msg) = msg {
                    match msg {
                        WorkerMessage::FileChanged { code } => {
                            trace!("controller: file changed");

                            tx_connected.send(ConnectedMessage::UpdateCode { code, play: state.opts.lock().await.play }).await?;

                            trace!("controller: file changed end");
                        }
                        WorkerMessage::WatchError { error } => {
                            warn!("file watcher error: {}", error);
                        }
                        WorkerMessage::Start { download } => {
                            trace!("controller: start");

                            // Update local file if download was requested
                            send_code_pending = download;

                            // We are now ready
                            tx_connected.send(ConnectedMessage::AppReady).await?;

                            // Notify UI
                            tx_notification.send(WorkerNotification::Initialized)?;

                            trace!("controller: start end");
                        }
                        WorkerMessage::Stop => {
                            trace!("controller: stop");

                            // Discard any notifications from IDE
                            send_code_pending = false;

                            // Notify UI
                            tx_notification.send(WorkerNotification::Stopped)?;

                            trace!("controller: stop end");
                        }
                        WorkerMessage::Terminate => {
                            break;
                        }
                    }
                } else {
                    break;
                }
            },

            msg = rx_conn_notification.next().fuse() => {
                if let Some(msg) = msg {
                    match msg {
                        ConnectedNotification::Details { title, question_id } => {
                            trace!("controller: details");

                            // Notify the UI we now have a question
                            tx_notification.send(WorkerNotification::Details { title: title.clone(), question_id })?;

                            trace!("controller: details end");

                        },
                        ConnectedNotification::Code { code } => {
                            trace!("controller: code");

                            if send_code_pending {
                                match std::fs::write(&state.opts.lock().await.target, code) {
                                    Ok(_) => info!("updated code from IDE"),
                                    Err(err) => {
                                        let message = err.to_string();
                                        error!("{}", message);
                                    }
                                }

                                send_code_pending = false;
                            }

                            trace!("controller: code end");
                        }
                    }
                }
            }
        }
    }

    info!("controller terminating");

    // Terminate connected
    tx_connected.send(ConnectedMessage::Terminate).await?;

    // Terminate listener
    tx_listen.send(ListenMessage::Terminate).await?;

    // Terminate notification
    tx_notification.send(WorkerNotification::Terminate)?;

    Ok(())
}

fn spawn_worker(
    opts: Arc<Mutex<Opts>>,
) -> Result<(
    std::thread::JoinHandle<Result<()>>,
    async_std::channel::Sender<WorkerMessage>,
    std::sync::mpsc::Receiver<WorkerNotification>,
)> {
    let state = State::new(opts.clone());

    let mut hotwatch = Hotwatch::new()?;
    let path: PathBuf =
        task::block_on(async { opts.lock().await.target.parent().unwrap().to_owned() });

    let (tx_controller, rx_controller) = async_std::channel::bounded(1);
    let (tx_listen, rx_listen) = async_std::channel::bounded(1);
    let (tx_connected, rx_connected) = async_std::channel::bounded(1);
    let (tx_notification, rx_notification) = std::sync::mpsc::channel();
    let (tx_conn_notification, rx_conn_notification) = async_std::channel::bounded(1);

    {
        let opts = opts.clone();
        let tx_controller = tx_controller.clone();

        hotwatch.watch(path, move |event: Event| match event {
            Event::NoticeWrite(path) | Event::Create(path) | Event::Write(path) => {
                let tx_controller = tx_controller.clone();
                let opts = opts.clone();

                task::spawn(async move {
                    if let Ok(target) = async_std::fs::canonicalize(&opts.lock().await.target).await
                    {
                        if PathBuf::from(path) == target {
                            match async_std::fs::read_to_string(&target).await {
                                Ok(code) => {
                                    return tx_controller
                                        .send(WorkerMessage::FileChanged { code })
                                        .await
                                }
                                Err(error) => {
                                    return tx_controller
                                        .send(WorkerMessage::WatchError { error })
                                        .await
                                }
                            }
                        }
                    }

                    Ok(())
                });
            }
            _ => {}
        })?;
    }

    Ok((
        std::thread::spawn(move || {
            task::block_on(async move {
                task::spawn(run_accept(
                    rx_connected,
                    rx_listen,
                    tx_conn_notification,
                    opts.lock().await.bind.clone(),
                ));

                run_controller(
                    state,
                    tx_connected,
                    tx_listen,
                    rx_controller,
                    tx_notification,
                    rx_conn_notification,
                )
                .await
            })?;

            // Stop watching when the async worker completes
            drop(hotwatch);

            Ok(())
        }),
        tx_controller,
        rx_notification,
    ))
}

#[paw::main]
fn main(opts: Opts) -> Result<()> {
    let no_gui = opts.no_gui;
    if no_gui {
        env_logger::init_from_env(
            env_logger::Env::new()
                .filter_or("CG_LOCAL_LOG", "cg_local_app=debug")
                .write_style("CG_LOCAL_LOG_STYLE"),
        );
    }

    let opts = Arc::new(Mutex::new(opts));
    let (join_handle, tx_worker, rx_notification) = spawn_worker(opts.clone())?;

    if no_gui {
        for m in rx_notification.iter() {
            match m {
                WorkerNotification::Details { title, question_id } => {
                    info!("working on question '{}' (id: {})", title, question_id);

                    task::block_on(async {
                        trace!("sending Start");

                        tx_worker
                            .send(WorkerMessage::Start {
                                download: opts.lock().await.download,
                            })
                            .await
                    })?;
                }
                WorkerNotification::Initialized => {
                    info!("synchronization started");
                }
                WorkerNotification::Stopped => {
                    info!("synchronization stopped");
                }
                WorkerNotification::Terminate => {
                    break;
                }
            }
        }
    } else {
        use cursive::views::{Checkbox, Dialog, LinearLayout, TextView};
        use cursive::Cursive;

        fn dialog_waiting(s: &mut Cursive) {
            s.pop_layer();
            s.add_layer(
                Dialog::around(TextView::new("Waiting for IDE to connect."))
                    .title("cg-local-app.rs")
                    .button("Quit", |s| s.quit()),
            );
        }

        fn dialog_initial(
            s: &mut Cursive,
            header: &str,
            tx_worker: async_std::channel::Sender<WorkerMessage>,
        ) {
            s.pop_layer();
            s.add_layer(
                Dialog::around(TextView::new(header))
                    .title("cg-local-app.rs")
                    .button("Upload", {
                        let tx_worker = tx_worker.clone();
                        move |_| {
                            task::block_on(tx_worker.send(WorkerMessage::Start { download: false }))
                                .expect("failed to send start message to worker")
                        }
                    })
                    .button("Download", move |_| {
                        task::block_on(tx_worker.send(WorkerMessage::Start { download: true }))
                            .expect("failed to send start message to worker")
                    })
                    .button("Quit", |s| s.quit()),
            );
        }

        fn dialog_running(
            s: &mut Cursive,
            header: &str,
            tx_worker: async_std::channel::Sender<WorkerMessage>,
            opts: Arc<Mutex<Opts>>,
        ) {
            s.pop_layer();
            s.add_layer(
                Dialog::around(
                    LinearLayout::vertical().child(TextView::new(header)).child(
                        LinearLayout::horizontal()
                            .child({
                                let mut chk = Checkbox::new().on_change({
                                    let opts = opts.clone();
                                    move |_s, checked| {
                                        task::block_on(async { opts.lock().await.play = checked });
                                    }
                                });

                                if task::block_on(async { opts.lock().await.play }) {
                                    chk.check();
                                }

                                chk
                            })
                            .child(TextView::new("Play on upload")),
                    ),
                )
                .title("cg-local-app.rs")
                .button("Stop sync", move |_| {
                    task::block_on(tx_worker.send(WorkerMessage::Stop))
                        .expect("failed to send stop message to worker")
                })
                .button("Quit", |s| s.quit()),
            );
        }

        let mut s = cursive::default().into_runner();
        s.add_global_callback('q', |s| s.quit());

        dialog_waiting(&mut s);

        s.refresh();

        let mut header = String::new();

        loop {
            s.step();
            if !s.is_running() {
                break;
            }

            let mut needs_refresh = false;
            for m in rx_notification.try_iter() {
                match m {
                    WorkerNotification::Details { title, question_id } => {
                        header = format!("Working on question '{}' (id: {})", title, question_id);

                        dialog_initial(&mut s, &header, tx_worker.clone());
                    }
                    WorkerNotification::Initialized => {
                        // Show running screen
                        dialog_running(&mut s, &header, tx_worker.clone(), opts.clone());
                    }
                    WorkerNotification::Stopped => {
                        // Go back to question screen
                        dialog_initial(&mut s, &header, tx_worker.clone());
                    }
                    WorkerNotification::Terminate => {
                        s.quit();
                    }
                }

                needs_refresh = true;
            }

            if needs_refresh {
                s.refresh();
            }
        }
    }

    // Terminate worker
    task::block_on(tx_worker.send(WorkerMessage::Terminate))?;
    join_handle.join().unwrap()?;

    Ok(())
}