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
//! A session's reports to the session that started it, for a session
//! another session started as an agent that reports to its starter
//! (`starter_hears: reports`; see `agent::StarterHears`).
//!
//! Such a session works on its own between reports, so most of its turns
//! end telling its starter nothing. Its `report_to_starter` tool marks the
//! turn under way with a [`ReportKind`] in the session's [`StarterReport`],
//! and as that turn ends the daemon tells the starter, with the turn's
//! final message as the report (see `daemon_host::sessions::peer_wake`).
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use rho_sdk::SessionId;
use serde::{Deserialize, Serialize};
use super::self_questions::SelfQuestions;
/// What a report says of the reporting session's work.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ReportKind {
/// Its whole task is finished.
Done,
/// It can't go on without a decision its starter or a person makes.
NeedsDecision,
/// Something it can't fix stops it.
Blocked,
}
impl ReportKind {
/// Every kind, as the tool offers them.
pub(crate) const ALL: [Self; 3] = [Self::Done, Self::NeedsDecision, Self::Blocked];
/// The kind's name, as serde writes it.
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Done => "done",
Self::NeedsDecision => "needs_decision",
Self::Blocked => "blocked",
}
}
}
/// Where a reporting session's turn marks its report, whom it reports to,
/// and where it asks its own questions, which it does without waiting (see
/// `app::self_questions`). Cheap to clone; clones share the mark and the
/// questions.
#[derive(Clone, Debug)]
pub(crate) struct StarterReport {
starter: SessionId,
marked: Arc<Mutex<Option<ReportKind>>>,
questions: SelfQuestions,
}
impl StarterReport {
/// The reports of a session that session `starter` started.
pub(crate) fn to(starter: SessionId) -> Self {
Self {
starter,
marked: Arc::default(),
questions: SelfQuestions::new(),
}
}
/// The session the reports go to.
pub(crate) fn starter(&self) -> &SessionId {
&self.starter
}
/// Where the session asks its own questions, and their answers arrive.
pub(crate) fn questions(&self) -> &SelfQuestions {
&self.questions
}
/// Marks the turn under way as reporting `kind`; a later mark in the
/// same turn replaces it.
pub(crate) fn mark(&self, kind: ReportKind) {
*self.slot() = Some(kind);
}
/// The report the turn under way, or the one that just ended, marked so
/// far, if any; it stays marked until [`Self::take`].
pub(crate) fn marked(&self) -> Option<ReportKind> {
*self.slot()
}
/// The report the turn that just ended marked, if it marked one; the
/// next turn starts unmarked.
pub(crate) fn take(&self) -> Option<ReportKind> {
self.slot().take()
}
fn slot(&self) -> MutexGuard<'_, Option<ReportKind>> {
// Only ever replaced or taken whole.
self.marked.lock().unwrap_or_else(PoisonError::into_inner)
}
}