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
use actix_web::{*, web::Json};
use crate::*;
use parking_lot::RwLock;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use std::thread::{sleep, spawn};
use std::time::Duration;
use std::collections::{BTreeMap, VecDeque};

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
struct StatusView {
    done: bool,
    model: String,
    generated: usize,
    discoveries: BTreeMap<String, String>, // name+classification -> encoded path
    recent_path: Option<String>,
}

#[derive(Debug, Eq, PartialEq)]
struct StateView<State, Action> {
    action: Option<Action>,
    outcome: Option<String>,
    state: State,
    svg: Option<String>,
}

type StateViewsJson<State, Action> = Json<Vec<StateView<State, Action>>>;

impl<Action, State> serde::Serialize for StateView<State, Action>
where
    Action: Debug,
    State: Debug + Hash,
{
    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        let mut out = ser.serialize_struct("StateView", 3)?;
        if let Some(ref action) = self.action {
            out.serialize_field("action", &format!("{:?}", action))?;
        }
        if let Some(ref outcome) = self.outcome {
            out.serialize_field("outcome", outcome)?;
        }
        out.serialize_field("state", &format!("{:#?}", self.state))?;
        if let Some(ref svg) = self.svg {
            out.serialize_field("svg", svg)?;
        }
        out.serialize_field("fingerprint", &format!("{:?}", fingerprint(&self.state)))?;
        out.end()
    }
}

struct Snapshot<Action>(bool, Option<Vec<Action>>);
impl<M: Model> CheckerVisitor<M> for Arc<RwLock<Snapshot<M::Action>>> {
    fn visit(&self, _: &M, path: Path<M::State, M::Action>) {
        let guard = self.read();
        if !guard.0 { return }
        drop(guard);

        let mut guard = self.write();
        if !guard.0 { return } // May be racing other threads.
        guard.0 = false;
        guard.1 = Some(path.into_actions());
    }
}

pub(crate) fn serve<M>(checker_builder: CheckerBuilder<M>, addresses: impl ToSocketAddrs) -> Arc<impl Checker<M>>
where M: 'static + Model + Send + Sync,
      M::Action: Debug + Send + Sync,
      M::State: Debug + Hash + Send + Sync,
{
    let snapshot = Arc::new(RwLock::new(Snapshot(true, None)));
    let snapshot_for_visitor = Arc::clone(&snapshot);
    let snapshot_for_server = Arc::clone(&snapshot);
    spawn(move || {
        loop {
            sleep(Duration::from_secs(4));
            snapshot.write().0 = true;
        }
    });
    let checker = checker_builder
        .visitor(snapshot_for_visitor)
        .spawn_bfs();
    serve_checker(checker, snapshot_for_server, addresses)
}

fn serve_checker<M, C>(
    checker: C,
    snapshot: Arc<RwLock<Snapshot<M::Action>>>,
    addresses: impl ToSocketAddrs)
    -> Arc<impl Checker<M>>
where M: 'static + Model + Send + Sync,
      M::Action: Debug + Send + Sync,
      M::State: Debug + Hash + Send + Sync,
      C: 'static + Checker<M> + Send + Sync,
{
    let checker = Arc::new(checker);

    let data = Arc::new((snapshot, Arc::clone(&checker)));
    HttpServer::new(move || {
        macro_rules! get_ui_file {
            ($filename:literal) => {
                web::get().to(|| HttpResponse::Ok().body({
                    if let Ok(content) = std::fs::read(concat!("./ui/", $filename)) {
                        log::info!("Explorer dev mode. Loading {} from disk.", $filename);
                        content
                    } else {
                        include_bytes!(concat!("../../ui/", $filename)).to_vec()
                    }
                }))
            }
        };

        App::new()
            .data(Arc::clone(&data))
            .route("/.status", web::get().to(status::<M, C>))
            .route("/.states{fingerprints:.*}", web::get().to(states::<M, C>))
            .route("/", get_ui_file!("index.htm"))
            .route("/app.css", get_ui_file!("app.css"))
            .route("/app.js", get_ui_file!("app.js"))
            .route("/knockout-3.5.0.js", get_ui_file!("knockout-3.5.0.js"))
    }).bind(addresses).unwrap().run().unwrap();

    checker
}

type Data<Action, Checker> = web::Data<Arc<(Arc<RwLock<Snapshot<Action>>>, Arc<Checker>)>>;

fn status<M, C>(_: HttpRequest, data: Data<M::Action, C>) -> Result<Json<StatusView>>
where M: Model,
      M::Action: Debug,
      M::State: Hash,
      C: Checker<M>,
{
    let snapshot = &data.0;
    let checker = &data.1;

    let status = StatusView {
        model: std::any::type_name::<M>().to_string(),
        done: checker.is_done(),
        generated: checker.generated_count(),
        discoveries: checker.discoveries().into_iter()
            .map(|(name, path)| {
                let key = format!("\"{}\" {}", name, checker.discovery_classification(name));
                let value = path.encode();
                (key, value)
            })
            .collect(),
        recent_path: snapshot.read().1.as_ref().map(|p| format!("{:?}", p)),
    };
    Ok(Json(status))
}

fn states<M, C>(req: HttpRequest, data: Data<M::Action, C>)
    -> Result<StateViewsJson<M::State, M::Action>>
where M: Model,
      M::State: Debug + Hash,
      C: Checker<M>,
{
    let model = &data.1.model();

    // extract fingerprints
    let mut fingerprints_str = req.match_info().get("fingerprints").expect("missing 'fingerprints' param").to_string();
    if fingerprints_str.ends_with('/') {
        let relevant_len = fingerprints_str.len() - 1;
        fingerprints_str.truncate(relevant_len);
    }
    let fingerprints: VecDeque<_> = fingerprints_str.split('/').filter_map(|fp| fp.parse::<Fingerprint>().ok()).collect();

    // ensure all but the first string (which is empty) were parsed
    if fingerprints.len() + 1 != fingerprints_str.split('/').count() {
        return Err(
            actix_web::error::ErrorNotFound(
                format!("Unable to parse fingerprints {}", fingerprints_str)));
    }

    // now build up all the subsequent `StateView`s
    let mut results = Vec::new();
    if fingerprints.is_empty() {
        for state in model.init_states() {
            let svg = {
                let mut fingerprints: VecDeque<_> = fingerprints.clone().into_iter().collect();
                fingerprints.push_back(fingerprint(&state));
                model.as_svg(Path::from_fingerprints::<M>(model, fingerprints))
            };
            results.push(StateView {
                action: None,
                outcome: None,
                state,
                svg,
            });
        }
    } else if let Some(last_state) = Path::final_state::<M>(model, fingerprints.clone()) {
        // Must generate the actions three times because they are consumed by `next_state`
        // and `display_outcome`.
        let mut actions1 = Vec::new();
        let mut actions2 = Vec::new();
        let mut actions3 = Vec::new();
        model.actions(&last_state, &mut actions1);
        model.actions(&last_state, &mut actions2);
        model.actions(&last_state, &mut actions3);
        for ((action, action2), action3) in actions1.into_iter().zip(actions2).zip(actions3) {
            let outcome = model.display_outcome(&last_state, action2);
            let state = model.next_state(&last_state, action3);
            if let Some(state) = state {
                let svg = {
                    let mut fingerprints: VecDeque<_> = fingerprints.clone().into_iter().collect();
                    fingerprints.push_back(fingerprint(&state));
                    model.as_svg(Path::from_fingerprints::<M>(model, fingerprints))
                };
                results.push(StateView { action: Some(action), outcome, state, svg });
            }
        }
    } else {
        return Err(
            actix_web::error::ErrorNotFound(
                format!("Unable to find state following fingerprints {}", fingerprints_str)));
    }

    Ok(Json(results))
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_util::binary_clock::*;

    #[test]
    fn can_init() {
        let checker = Arc::new(BinaryClock.checker().spawn_bfs());
        assert_eq!(get_states(Arc::clone(&checker), "/").unwrap(), vec![
            StateView { action: None, outcome: None, state: 0, svg: None },
            StateView { action: None, outcome: None, state: 1, svg: None },
        ]);
    }

    #[test]
    fn can_next() {
        let checker = Arc::new(BinaryClock.checker().spawn_bfs());
        // We need a static string for TestRequest, so this is precomputed, but you can recompute
        // the values if needed as follows:
        // ```
        // let first = fingerprint(&1_i8);
        // let second = fingerprint(&0_i8);
        // let path_name = format!("/{}/{}", first, second);
        // println!("New path name is: {}", path_name);
        // ```
        assert_eq!(get_states(Arc::clone(&checker), "/2716592049047647680/9080728272894440685").unwrap(), vec![
            StateView {
                action: Some(BinaryClockAction::GoHigh),
                outcome: Some("1".to_string()),
                state: 1,
                svg: None,
            },
        ]);
    }

    #[test]
    fn err_for_invalid_fingerprint() {
        let checker = Arc::new(BinaryClock.checker().spawn_bfs());
        assert_eq!(format!("{}", get_states(Arc::clone(&checker), "/one/two/three").unwrap_err()),
            "Unable to parse fingerprints /one/two/three");
        assert_eq!(format!("{}", get_states(Arc::clone(&checker), "/1/2/3").unwrap_err()),
            "Unable to find state following fingerprints /1/2/3");
    }

    #[test]
    fn smoke_test_states() {
        use crate::actor::{DuplicatingNetwork, Envelope, Id, LossyNetwork, System, SystemState};
        use crate::actor::actor_test_util::ping_pong::{PingPongCount, PingPongMsg::*, PingPongSystem};
        use crate::actor::SystemAction::*;
        use crate::util::HashableHashSet;
        use std::iter::FromIterator;

        let checker = Arc::new(PingPongSystem {
            max_nat: 2,
            lossy: LossyNetwork::Yes,
            duplicating: DuplicatingNetwork::No,
            maintains_history: true,
        }.into_model().checker().spawn_bfs());
        assert_eq!(
            get_states(Arc::clone(&checker), "/").unwrap(),
            vec![
                StateView {
                    action: None,
                    outcome: None,
                    state: SystemState {
                        actor_states: vec![Arc::new(PingPongCount(0)), Arc::new(PingPongCount(0))],
                        history: (0, 1),
                        is_timer_set: vec![],
                        network: HashableHashSet::from_iter(vec![
                            Envelope { src: Id::from(0), dst: Id::from(1), msg: Ping(0) },
                        ]),
                    },
                    svg: Some("<svg version=\'1.1\' baseProfile=\'full\' width=\'500\' height=\'30\' viewbox=\'-20 -20 520 50\' xmlns=\'http://www.w3.org/2000/svg\'><defs><marker class=\'svg-event-shape\' id=\'arrow\' markerWidth=\'12\' markerHeight=\'10\' refX=\'12\' refY=\'5\' orient=\'auto\'><polygon points=\'0 0, 12 5, 0 10\' /></marker></defs><line x1=\'0\' y1=\'0\' x2=\'0\' y2=\'30\' class=\'svg-actor-timeline\' />\n<text x=\'0\' y=\'0\' class=\'svg-actor-label\'>0</text>\n<line x1=\'100\' y1=\'0\' x2=\'100\' y2=\'30\' class=\'svg-actor-timeline\' />\n<text x=\'100\' y=\'0\' class=\'svg-actor-label\'>1</text>\n</svg>\n".to_string()),
                },
            ]);
        // To regenerate the path if the fingerprint changes:
        // ```
        // let fp = fingerprint(&SystemState::<PingPongSystem> {
        //     actor_states: vec![Arc::new(PingPongCount(0)), Arc::new(PingPongCount(0))],
        //     history: (0, 1),
        //     is_timer_set: vec![],
        //     network: HashableHashSet::from_iter(vec![
        //         Envelope { src: Id::from(0), dst: Id::from(1), msg: Ping(0) },
        //     ]),
        // });
        // println!("New path name is: /{}", fp);
        // ```
        let states = get_states(Arc::clone(&checker), "/2298670378538534683").unwrap();
        assert_eq!(states.len(), 2);
        assert_eq!(
            states[0],
            StateView {
                action: Some(Drop(Envelope { src: Id::from(0), dst: Id::from(1), msg: Ping(0) })),
                outcome: Some("DROP: Envelope { src: Id(0), dst: Id(1), msg: Ping(0) }".to_string()),
                state: SystemState {
                    actor_states: vec![Arc::new(PingPongCount(0)), Arc::new(PingPongCount(0))],
                    history: (0, 1),
                    is_timer_set: vec![],
                    network: HashableHashSet::new(),
                },
                svg: Some("<svg version='1.1' baseProfile='full' width='500' height='60' viewbox='-20 -20 520 80' xmlns='http://www.w3.org/2000/svg'><defs><marker class='svg-event-shape' id='arrow' markerWidth='12' markerHeight='10' refX='12' refY='5' orient='auto'><polygon points='0 0, 12 5, 0 10' /></marker></defs><line x1='0' y1='0' x2='0' y2='60' class='svg-actor-timeline' />\n<text x='0' y='0' class='svg-actor-label'>0</text>\n<line x1='100' y1='0' x2='100' y2='60' class='svg-actor-timeline' />\n<text x='100' y='0' class='svg-actor-label'>1</text>\n</svg>\n".to_string()),
            });
        assert_eq!(
            states[1],
            StateView {
                action: Some(Deliver { src: Id::from(0), dst: Id::from(1), msg: Ping(0) }),
                outcome: Some("OUT: [Send(Id(0), Pong(0))]\n\nNEXT_STATE: PingPongCount(\n    1,\n)\n\nPREV_STATE: PingPongCount(\n    0,\n)\n".to_string()),
                state: SystemState {
                    actor_states: vec![
                        Arc::new(PingPongCount(0)),
                        Arc::new(PingPongCount(1)),
                    ],
                    history: (1, 2),
                    is_timer_set: vec![],
                    network: HashableHashSet::from_iter(vec![
                        Envelope { src: Id::from(1), dst: Id::from(0), msg: Pong(0) },
                    ]),
                },
                svg: Some("<svg version='1.1' baseProfile='full' width='500' height='60' viewbox='-20 -20 520 80' xmlns='http://www.w3.org/2000/svg'><defs><marker class='svg-event-shape' id='arrow' markerWidth='12' markerHeight='10' refX='12' refY='5' orient='auto'><polygon points='0 0, 12 5, 0 10' /></marker></defs><line x1='0' y1='0' x2='0' y2='60' class='svg-actor-timeline' />\n<text x='0' y='0' class='svg-actor-label'>0</text>\n<line x1='100' y1='0' x2='100' y2='60' class='svg-actor-timeline' />\n<text x='100' y='0' class='svg-actor-label'>1</text>\n<line x1='0' x2='100' y1='0' y2='30' marker-end='url(#arrow)' class='svg-event-line' />\n<text x='100' y='30' class='svg-event-label'>Ping(0)</text>\n</svg>\n".to_string()),
            });
    }

    #[test]
    fn smoke_test_status() {
        use crate::actor::{DuplicatingNetwork, LossyNetwork, System};
        use crate::actor::actor_test_util::ping_pong::PingPongSystem;

        let snapshot = Arc::new(RwLock::new(Snapshot(true, None)));
        let checker = PingPongSystem {
            max_nat: 2,
            lossy: LossyNetwork::No,
            duplicating: DuplicatingNetwork::No,
            maintains_history: true,
        }.into_model().checker()
            .visitor(Arc::clone(&snapshot)).spawn_bfs().join();
        let status = get_status(Arc::new(checker), snapshot).unwrap();
        assert_eq!(status.done, true);
        assert_eq!(
            status.model,
           "stateright::actor::system::SystemModel<\
                stateright::actor::actor_test_util::ping_pong::PingPongSystem\
            >");
        assert_eq!(status.generated, 5);
        assert_eq!(status.discoveries.len(), 2);
        assert!(status.discoveries.get("\"can reach max\" example").is_some());
        assert!(status.discoveries.get("\"must exceed max\" counterexample").is_some());
        assert!(status.recent_path.unwrap().starts_with("["));
    }

    fn get_states<M, C>(checker: Arc<C>, path_name: &'static str)
                -> Result<Vec<StateView<M::State, M::Action>>>
    where M: Model,
          M::State: Debug + Hash,
          C: Checker<M>,
    {
        let req = actix_web::test::TestRequest::get()
            .param("fingerprints", &path_name)
            .to_http_request();
        let snapshot = Arc::new(RwLock::new(Snapshot(true, None)));
        let data = web::Data::new(Arc::new((snapshot, checker)));
        match states(req, data) {
            Ok(Json(view)) => Ok(view),
            Err(err) => Err(err),
        }
    }

    fn get_status<M, C>(checker: Arc<C>, snapshot: Arc<RwLock<Snapshot<M::Action>>>)
    -> Result<StatusView>
    where M: Model,
          M::Action: Debug,
          M::State: Debug + Hash,
          C: Checker<M>,
    {
        let req = actix_web::test::TestRequest::get().to_http_request();
        let data = web::Data::new(Arc::new((snapshot, checker)));
        match status(req, data) {
            Ok(Json(view)) => Ok(view),
            Err(err) => Err(err),
        }
    }
}