main: the resident daemon mode, the send errand and one instance
The executable now runs the app without its window: it owns the session name or forwards to the instance that does (an sms: link, --quit; --quit beside --send is refused, the errand being no instance, and so is a link that is not UTF-8, which D-Bus does not carry; a name request that fails or is refused ends the run with the error's words, exit 1, nobody being shown to run), opens the database (the executable's own Crafter.DB dependency enters here, with its first use), connects the GDBus client to Sync and the notifier, and runs the GLib main loop. --daemon is the resident mode, --headless adds a log line per event for the ssh smoke test on the phone, --session talks to imsd --session, --data-dir and --country override the database directory and the default country code (a country code as the ITU's length rule reads one: a key is made of the code as it is and read by that rule, so anything else is refused). A --daemon with no sms: link that finds the name owned ends at once instead of forwarding an Activate: the running instance already imports and notifies, and a forwarded Activate asks it for its window; --headless then exits 1, having run nothing (a link and --quit are forwarded from a --daemon as from any launch). A Subscribe, a GetStatus or a List that failed against a present imsd is tried again every 30 s, and so is a text Sync holds for a store that refused it; that text is notified all the same (OnUnstored), the notification being where it shows, and is announced as any import once its row is in. --send sends one text to the number as dialled (stored under its normalised key; a NUMBER or TEXT that is not UTF-8 is refused before anything is stored, exit 2: imsd takes D-Bus strings, and GLib would put its own text in the place of other bytes and have that sent; an empty NUMBER is refused there too) and exits on imsd's word for the submission — sent (0) or failed (1), which reaches it as the message's change (a status report's word follows sent, imsd's order) — or on its own failure (1), a store that refuses the text among them; imsd has 60 s to be ready and to take the text, then 2 (no final state) — once it has taken it the clock decides nothing, since imsd bounds its submission itself and ends it with a word, and a clock of the app's would report no final state for a text imsd goes on to send — as a SIGINT or SIGTERM after the start-up, before a final state, is 2 (a resident instance ending on either says so in its log: a session bus closing under it sends SIGTERM too). It is a client of its own: it takes no instance name, since a launch that finds the name taken forwards and exits, and it leaves imsd's stored and signalled texts to the instance that owns the user's store; beside that instance it needs a --data-dir of its own, the store taking one writer. It sends once GetStatus has answered, with Sync's Send gaining a force flag that skips the local readiness check: imsd answers a Send to its caller, subscribed or not, so a refused or unregistered errand ends with imsd's own reason. Its rule is Sync's SendErrand, a class over the listener's events with no GLib in it: one Send once the status is in and none after an end, latched before the call (Sync announces a status from inside Send — the store's word on its write — and the loop can dispatch one after the clock's or a signal's end); one end — the message's final state, the store's refusal, or none on the clock (which decides until imsd has taken the text), a signal or the loop's end; the first of them stands. The shell drives it with its loop, its 60 s source and its log. Tested: tests/Sync, the errand among its cases (a forced send against an unregistered status; imsd's words reaching the listener as the message's changes, as they come), and its rule: one Send across a status announced from inside Send (the store's recovery, end to end on a store refused under the file-size limit), 0 on sent, 1 on failed and on the store's refusal, 2 on the clock before imsd's id and on a signal, the clock deciding nothing once imsd took the text, a final state given first standing over the clock and the loop's end after it.
This commit is contained in:
parent
dcf1aecafd
commit
d9d6f6ef53
4 changed files with 515 additions and 17 deletions
|
|
@ -7,7 +7,8 @@ logic over an abstract transport.
|
|||
|
||||
imsd keeps a received text it could store until the app deletes it: the
|
||||
app lists at start and after every Received, imports what it lacks, then
|
||||
deletes imsd's copy. The signals are unicast to subscribers, so Subscribe
|
||||
deletes imsd's copy; the --send errand leaves that to the instance that
|
||||
owns the user's store. The signals are unicast to subscribers, so Subscribe
|
||||
goes first, every time net.catcrafts.IMS1 appears on the bus (imsd's
|
||||
restart empties its list). A Send answers with an id at once; the outcome
|
||||
is a StateChanged for that id. All of it runs on one thread (the GLib main
|
||||
|
|
@ -169,6 +170,11 @@ export namespace ImsdMessages {
|
|||
// notifier leaves it alone.
|
||||
void SetOpenConversation(std::optional<std::string> key) { openConversation = std::move(key); }
|
||||
|
||||
// The --send errand: it sends and follows its own message and
|
||||
// leaves the texts imsd keeps to the instance that owns the user's
|
||||
// store, so it lists, imports and deletes nothing.
|
||||
void LeaveTexts() { leaveTexts = true; }
|
||||
|
||||
std::string CountryCode() const {
|
||||
if (!countryOverride.empty()) return countryOverride;
|
||||
// The own number says which country a national number is in;
|
||||
|
|
@ -288,8 +294,9 @@ export namespace ImsdMessages {
|
|||
// Stores the message as Pending under its conversation key and
|
||||
// submits it to `to`, the address as dialled (Number's
|
||||
// DialledNumber), which the key never stands in for; the row id
|
||||
// comes back at once, the outcome through OnMessageChanged.
|
||||
std::uint64_t Send(std::string_view conversation, std::string_view to, std::string_view text) {
|
||||
// comes back at once, the outcome through OnMessageChanged. `force`
|
||||
// skips the local readiness check and lets imsd answer.
|
||||
std::uint64_t Send(std::string_view conversation, std::string_view to, std::string_view text, bool force = false) {
|
||||
Message m;
|
||||
m.conversation = std::string(conversation);
|
||||
m.peer = std::string(to);
|
||||
|
|
@ -304,7 +311,7 @@ export namespace ImsdMessages {
|
|||
if (!id) return kNotStored; // the store refused it: the caller keeps the text
|
||||
m.id = *id;
|
||||
listener.OnMessageChanged(m);
|
||||
Submit(m.id);
|
||||
Submit(m.id, force);
|
||||
return m.id;
|
||||
}
|
||||
|
||||
|
|
@ -346,8 +353,8 @@ export namespace ImsdMessages {
|
|||
Changed(id);
|
||||
}
|
||||
|
||||
void Submit(std::uint64_t id) {
|
||||
if (!CanSend(status)) {
|
||||
void Submit(std::uint64_t id, bool force = false) {
|
||||
if (!force && !CanSend(status)) {
|
||||
Fail(id, BannerText(status));
|
||||
return;
|
||||
}
|
||||
|
|
@ -409,6 +416,7 @@ export namespace ImsdMessages {
|
|||
}
|
||||
|
||||
void FetchList() {
|
||||
if (leaveTexts) return;
|
||||
if (listInFlight) {
|
||||
listAgain = true;
|
||||
return;
|
||||
|
|
@ -462,6 +470,7 @@ export namespace ImsdMessages {
|
|||
// sends for a text it could not store itself too — the signal is
|
||||
// then its only delivery, and no List carries the text after.
|
||||
void Import(const ReceivedMessage& r, bool signalled = false) {
|
||||
if (leaveTexts) return;
|
||||
if (!IsType0(r) && !known.contains(r.imsdId)) {
|
||||
Message m = MessageFromReceived(r);
|
||||
m.read = openConversation && *openConversation == m.conversation;
|
||||
|
|
@ -527,5 +536,77 @@ export namespace ImsdMessages {
|
|||
std::optional<std::string> openConversation;
|
||||
bool listInFlight = false;
|
||||
bool listAgain = false;
|
||||
bool leaveTexts = false;
|
||||
};
|
||||
|
||||
// The --send errand's rule over Sync's events: one Send once imsd's
|
||||
// status is in — latched before the call, since Sync announces a
|
||||
// status from inside Send (the store's word on its write) — and one
|
||||
// end: the message's final state (Sent 0, Failed 1), a store that
|
||||
// refused the text (1), or none — the clock, a signal, the loop's end —
|
||||
// (2). The clock is for getting the text to imsd: once imsd has taken
|
||||
// it, its word ends the errand. The loop, the clock and the log are the
|
||||
// shell's (main).
|
||||
class SendErrand {
|
||||
public:
|
||||
struct End {
|
||||
std::int32_t code;
|
||||
std::string caption; // the log's: the state's, or what ended the errand without one
|
||||
std::string reason;
|
||||
};
|
||||
|
||||
// The Send is due on this status: once, only with imsd's status in,
|
||||
// and never after an end (the loop can dispatch a status after the
|
||||
// clock's or a signal's end).
|
||||
bool SendDue(const AppStatus& s) {
|
||||
if (started || done || !s.imsdPresent || !s.statusKnown) return false;
|
||||
started = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Send's answer: the row to follow, or kNotStored, which ends the errand.
|
||||
std::optional<End> Sent(std::uint64_t id) {
|
||||
if (id == Sync::kNotStored) return Ended(1, "Failed", "the store did not take the text");
|
||||
sentId = id;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// A message's change: the errand's own, in a final state, ends it;
|
||||
// with imsd's id on it, imsd has taken the text.
|
||||
std::optional<End> Changed(const Message& m) {
|
||||
if (!sentId || m.id != *sentId) return std::nullopt;
|
||||
if (!m.imsdId.empty()) taken = true;
|
||||
if (m.state == MessageState::Sent) return Ended(0, std::string(StateCaption(m.state)), m.stateReason);
|
||||
if (m.state == MessageState::Failed) return Ended(1, std::string(StateCaption(m.state)), m.stateReason);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// The clock ran out (`after`: the shell's span) before imsd was ready
|
||||
// or had taken the text. Once it has, the clock decides nothing:
|
||||
// imsd bounds a submission itself, segment by segment, and ends it
|
||||
// with a word — a clock of the app's own would report no final state
|
||||
// for a text imsd goes on to send, and leave its row to fail at the
|
||||
// next start.
|
||||
std::optional<End> TimedOut(std::string_view after) {
|
||||
if (taken) return std::nullopt;
|
||||
return Ended(2, started ? std::format("no final state within {}", after) : std::format("imsd did not become ready within {}", after), "");
|
||||
}
|
||||
|
||||
// A signal, or the loop ended by something else: no final state.
|
||||
std::optional<End> NoFinalState(std::string_view how) { return Ended(2, std::string(how), ""); }
|
||||
|
||||
bool Done() const noexcept { return done; }
|
||||
|
||||
private:
|
||||
std::optional<End> Ended(std::int32_t code, std::string caption, std::string reason) {
|
||||
if (done) return std::nullopt;
|
||||
done = true;
|
||||
return End{ code, std::move(caption), std::move(reason) };
|
||||
}
|
||||
|
||||
bool started = false; // the Send was made
|
||||
bool taken = false; // imsd answered it with an id
|
||||
bool done = false; // the end was given
|
||||
std::optional<std::uint64_t> sentId;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue