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:
Jorijn van der Graaf 2026-10-17 23:45:29 +02:00
commit d9d6f6ef53
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 515 additions and 17 deletions

View file

@ -589,6 +589,43 @@ int main() {
fs::remove_all(throwDir);
}
// --- the --send errand: it sends, and leaves imsd's texts to the instance --------------
{
const fs::path errandDir = fs::temp_directory_path() / "imsd-messages-sync-errand";
fs::remove_all(errandDir);
MessageStore errandStore(errandDir);
Fake bus3;
Recorder events3;
Sync errand(errandStore, bus3, events3, "31");
errand.LeaveTexts();
errand.OnImsdAppeared();
bus3.AnswerSubscribe();
Check(bus3.statuses.size() == 1 && bus3.lists.empty(), "the errand asks for the status and lists nothing");
bus3.AnswerStatus(Online());
errand.OnReceived(Text("ims-sms-in-9-1", "+31621308050", "for the instance", 1100));
Check(errandStore.Size() == 0 && bus3.deletes.empty() && bus3.lists.empty(), "a signalled text is neither imported nor deleted by the errand");
const std::uint64_t sent = errand.Send("+31621308050", "+31621308050", "from the errand", true);
Check(bus3.sends.size() == 1 && bus3.sends[0].text == "from the errand", "the errand sends");
bus3.AnswerSend(std::string("ims-sms-9"));
// imsd's words reach the listener as the message's changes, as they
// come, the submission's first (imsd's order); the errand ends on it.
errand.OnStateChanged("ims-sms-9", "sent", "");
errand.OnStateChanged("ims-sms-9", "delivery-pending", "TP-ST 0x30");
std::vector<MessageState> seen; // the message's states as announced (the pending's announcements — the Send's, the id's — collapsed)
for (const Message& m : events3.changed) if (m.id == sent) seen.push_back(m.state);
seen.erase(std::unique(seen.begin(), seen.end()), seen.end());
Check(seen == std::vector<MessageState>{ MessageState::Pending, MessageState::Sent, MessageState::DeliveryPending } && errandStore.Get(sent)->state == MessageState::DeliveryPending, "imsd's words reach the listener as the message's changes, as they come, and the store follows them");
// Unregistered: a send fails at once, a forced one goes to imsd.
errand.OnRegistrationChanged(false);
const std::uint64_t local = errand.Send("+31621308050", "+31621308050", "not forced");
Check(errandStore.Get(local)->state == MessageState::Failed && bus3.sends.empty(), "unregistered, a send fails without asking imsd");
const std::uint64_t forced = errand.Send("+31621308050", "+31621308050", "forced", true);
Check(bus3.sends.size() == 1 && bus3.sends[0].text == "forced", "a forced send goes to imsd all the same");
bus3.AnswerSend(std::unexpected(Error(BusError::Kind::Other, "imsd is not registered")));
Check(errandStore.Get(forced)->state == MessageState::Failed && errandStore.Get(forced)->stateReason == "imsd is not registered", "and fails with imsd's reason");
fs::remove_all(errandDir);
}
// --- the store refusing a write: the file-size limit stands in for a full disk ------------
{
const fs::path fullDir = fs::temp_directory_path() / "imsd-messages-sync-full";
@ -648,6 +685,108 @@ int main() {
fs::remove_all(fullDir);
}
// --- the --send errand's rule (Sync's SendErrand): one Send, one end -----------------
{
SendErrand e;
AppStatus s;
Check(!e.SendDue(s), "no Send before imsd is there");
s.imsdPresent = true;
Check(!e.SendDue(s), "nor before its status is in");
s.statusKnown = true;
Check(e.SendDue(s) && !e.SendDue(s), "once the status is in: one Send; a second status — one announced from inside Send — makes none");
Check(!e.Sent(7).has_value(), "a stored row is followed, nothing ends");
Message other;
other.id = 8;
other.state = MessageState::Sent;
Check(!e.Changed(other).has_value(), "another message's final state is not the errand's");
Message own;
own.id = 7;
own.state = MessageState::Pending;
Check(!e.Changed(own).has_value(), "a pending change ends nothing");
own.state = MessageState::Sent;
own.stateReason = "2026-11-01T22:00:00+01:00";
const std::optional<SendErrand::End> sent = e.Changed(own);
Check(sent && sent->code == 0 && sent->caption == "Sent" && sent->reason == "2026-11-01T22:00:00+01:00" && e.Done(), "sent: 0, imsd's caption and reason");
Check(!e.TimedOut("60 s").has_value() && !e.NoFinalState("ended before a final state").has_value(), "a final state given first stands: the clock and the loop's end after it change nothing");
own.state = MessageState::Failed;
Check(!e.Changed(own).has_value(), "and over a later word");
SendErrand failing;
failing.SendDue(s);
failing.Sent(9);
Message f;
f.id = 9;
f.state = MessageState::Failed;
f.stateReason = "RP-ERROR cause 38";
const std::optional<SendErrand::End> fe = failing.Changed(f);
Check(fe && fe->code == 1 && fe->caption == "Failed" && fe->reason == "RP-ERROR cause 38", "failed: 1, with imsd's reason");
SendErrand refused;
refused.SendDue(s);
const std::optional<SendErrand::End> re = refused.Sent(Sync::kNotStored);
Check(re && re->code == 1 && re->caption == "Failed" && re->reason == "the store did not take the text" && refused.Done(), "a store that refused the text: 1, ended");
SendErrand early;
const std::optional<SendErrand::End> te = early.TimedOut("60 s");
Check(te && te->code == 2 && te->caption == "imsd did not become ready within 60 s", "the clock before the status: 2, imsd not ready");
Check(!early.SendDue(s), "and no Send after it: a status dispatched after the clock's end makes none");
SendErrand waiting;
waiting.SendDue(s);
waiting.Sent(3);
const std::optional<SendErrand::End> tw = waiting.TimedOut("60 s");
Check(tw && tw->code == 2 && tw->caption == "no final state within 60 s" && !waiting.TimedOut("60 s").has_value(), "the clock after the Send, before imsd's id: 2, no final state — once");
SendErrand taken;
taken.SendDue(s);
taken.Sent(5);
Message t;
t.id = 5;
t.state = MessageState::Pending;
t.imsdId = "ims-sms-5";
Check(!taken.Changed(t).has_value() && !taken.TimedOut("60 s").has_value() && !taken.Done(), "the clock once imsd has taken the text decides nothing: imsd bounds its submission itself");
t.state = MessageState::Sent;
const std::optional<SendErrand::End> ts = taken.Changed(t);
Check(ts && ts->code == 0 && taken.Done(), "and imsd's word ends the errand, however long after");
SendErrand signalled;
signalled.SendDue(s);
signalled.Sent(4);
const std::optional<SendErrand::End> si = signalled.NoFinalState("interrupted before a final state");
Check(si && si->code == 2 && si->caption == "interrupted before a final state", "a signal: 2");
SendErrand ended;
ended.NoFinalState("interrupted before a final state");
Check(!ended.SendDue(s), "nor a Send after a signal's end before any");
}
// The rule end to end, in the shape that sent two texts once: a listener
// running it from OnStatus, over a store refused before and recovering
// inside the Send's own write — which announces a status from inside
// Send. One text to imsd.
{
struct ErrandListener : Recorder {
Sync* sync = nullptr;
SendErrand errand;
std::size_t sends = 0;
void OnStatus(const AppStatus& s) override {
Recorder::OnStatus(s);
if (sync && errand.SendDue(s)) {
++sends;
errand.Sent(sync->Send("+31621308050", "+31621308050", std::string(4096, 'z'), true));
}
}
};
const fs::path reDir = fs::temp_directory_path() / "imsd-messages-sync-reentry";
fs::remove_all(reDir);
MessageStore reStore(reDir);
Fake bus10;
ErrandListener events10;
Sync reSync(reStore, bus10, events10, "31");
events10.sync = &reSync;
reSync.LeaveTexts();
const rlimit before = FillUntilRefused(reStore, reDir);
Check(reStore.Refused(), "the store refused a write");
setrlimit(RLIMIT_FSIZE, &before); // room again: the errand's own write will grow the file and tell of the recovery, from inside Send
reSync.OnImsdAppeared();
bus10.AnswerSubscribe();
bus10.AnswerStatus(Online());
Check(events10.sends == 1 && bus10.sends.size() == 1 && !reStore.Refused() && !events10.errand.Done(), "the status announced from inside the errand's Send makes no second Send: one text to imsd");
fs::remove_all(reDir);
}
// --- a signalled text the store refuses: imsd signals a text it could not
// store itself too, so no List may ever carry it — held, told, stored
// when there is room ---------------------------------------------------