M4 networkmanager: open the page and tell the person
At a join, at the watcher's start and at a session's start while PORTAL, once per ask, the watcher opens the check address with systemd-run --user and xdg-open in the person's session and then shows the notification its outcome calls for, critical when the open failed; it says when the network is open, and handles what it hears during an open after it. Builds: design 08 sign-in-page §15, §18, §19, §22
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
// Telling the person (design 08 sign-in-page §19): org.freedesktop.Notifications.Notify on the session's bus, the
|
||||
// desktop notification service, app_name mesh.
|
||||
|
||||
import "github.com/godbus/dbus/v5"
|
||||
|
||||
func notifyOn(bus string, n note) error {
|
||||
conn, err := dbus.Connect(bus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
hints := map[string]dbus.Variant{}
|
||||
if n.Critical {
|
||||
hints["urgency"] = dbus.MakeVariant(byte(2))
|
||||
}
|
||||
return conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications").
|
||||
Call("org.freedesktop.Notifications.Notify", 0, "mesh", uint32(0), "", n.Summary, n.Body, []string{}, hints,
|
||||
int32(-1)).Err
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package main
|
||||
|
||||
// Opening the page and telling the person (design 08 sign-in-page §15, §18, §19): at the three moments, once per ask,
|
||||
// in the person's session. The open runs beside the watch; rating changes heard meanwhile are handled after it, in the
|
||||
// order they came.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// openBound is how long the watcher waits for the opener: ended within it, its status decides; still running at it,
|
||||
// the page is opening and the run is left to go on (§18).
|
||||
const openBound = 10 * time.Second
|
||||
|
||||
// desk is the person's desktop as the watcher reaches it: an address opened in their session, a notification shown.
|
||||
type desk interface {
|
||||
open(s Session, address string) openOutcome
|
||||
notify(s Session, n note) error
|
||||
}
|
||||
|
||||
// openOutcome is how an open ended: opened, opening (still running at its bound) or failed, with the program and the
|
||||
// reason of a failure.
|
||||
type openOutcome struct {
|
||||
Address string `json:"address"`
|
||||
Program string `json:"program,omitempty"`
|
||||
Outcome string `json:"outcome"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// The outcomes of an open.
|
||||
const (
|
||||
outcomeOpened = "opened"
|
||||
outcomeOpening = "opening"
|
||||
outcomeFailed = "failed"
|
||||
)
|
||||
|
||||
// note is one notification: its summary and body, critical or not, and, once shown, when and whether it failed.
|
||||
type note struct {
|
||||
Summary string `json:"summary"`
|
||||
Body string `json:"body"`
|
||||
Critical bool `json:"critical,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
const askedSummary = "This network asks you to sign in"
|
||||
|
||||
// signInNote is what the person is told after an open, by its outcome (§19).
|
||||
func signInNote(network, address string, o openOutcome) note {
|
||||
switch o.Outcome {
|
||||
case outcomeOpened:
|
||||
return note{Summary: askedSummary, Body: fmt.Sprintf("%s: its sign-in page is open in %s.", network, o.Program)}
|
||||
case outcomeOpening:
|
||||
return note{Summary: askedSummary, Body: fmt.Sprintf("%s: its sign-in page is opening in %s.", network, o.Program)}
|
||||
}
|
||||
return note{Summary: askedSummary, Critical: true,
|
||||
Body: fmt.Sprintf("%s: the page did not open (%s); open %s in a browser.", network, o.Reason, address)}
|
||||
}
|
||||
|
||||
// signedInNote is what the person is told when the rating leaves PORTAL for FULL (§19).
|
||||
func signedInNote(network string) note {
|
||||
return note{Summary: "The network is open", Body: network + ": signed in."}
|
||||
}
|
||||
|
||||
// present opens the address in the person's session, s or the one found now, and then tells them (§15, §18, §19),
|
||||
// beside the watch; with no session found it opens and shows nothing, and the events are kept all the same (§15,
|
||||
// §16).
|
||||
func (w *Watcher) present(s *Session, network, address string) {
|
||||
if w.opening {
|
||||
w.later = append(w.later, func() { w.present(s, network, address) })
|
||||
return
|
||||
}
|
||||
if s = w.sessionNow(s, "the sign-in page of "+network+" is not opened"); s == nil {
|
||||
return
|
||||
}
|
||||
session := *s
|
||||
w.opening = true
|
||||
w.Beside(func() func() {
|
||||
o := w.Desk.open(session, address)
|
||||
n := signInNote(network, address, o)
|
||||
err := w.Desk.notify(session, n)
|
||||
return func() {
|
||||
o.At = w.Now()
|
||||
w.state.LastOpen = &o
|
||||
if o.Outcome == outcomeFailed {
|
||||
journal("opening %s in session %s failed: %s", address, session.ID, o.Reason)
|
||||
} else {
|
||||
journal("the sign-in page %s is %s in %s", address, o.Outcome, o.Program)
|
||||
}
|
||||
w.shown(n, err)
|
||||
w.opening = false
|
||||
w.drain()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// tellSignedIn tells the person the network is open, in their session; with none, nothing (§19).
|
||||
func (w *Watcher) tellSignedIn(network string) {
|
||||
s := w.sessionNow(nil, "the network being open is not shown")
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
session, n := *s, signedInNote(network)
|
||||
w.Beside(func() func() {
|
||||
err := w.Desk.notify(session, n)
|
||||
return func() { w.shown(n, err) }
|
||||
})
|
||||
}
|
||||
|
||||
// sessionNow is s, or the person's session found now; nil with a journal line saying what is not done.
|
||||
func (w *Watcher) sessionNow(s *Session, notDone string) *Session {
|
||||
if s != nil {
|
||||
return s
|
||||
}
|
||||
found, err := w.findSession()
|
||||
if err != nil {
|
||||
journal("finding the person's session failed: %v; %s", err, notDone)
|
||||
return nil
|
||||
}
|
||||
if found == nil {
|
||||
journal("no graphical session: %s", notDone)
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
func (w *Watcher) shown(n note, err error) {
|
||||
n.At = w.Now()
|
||||
if err != nil {
|
||||
n.Error = err.Error()
|
||||
journal("showing the notification %q failed: %v", n.Summary, err)
|
||||
} else {
|
||||
journal("showed the notification %q: %s", n.Summary, n.Body)
|
||||
}
|
||||
w.state.LastNotification = &n
|
||||
}
|
||||
|
||||
// drain handles, in the order they came, what was heard during an open, until one starts another open.
|
||||
func (w *Watcher) drain() {
|
||||
for len(w.later) > 0 && !w.opening {
|
||||
next := w.later[0]
|
||||
w.later = w.later[1:]
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
// besideLoop runs work beside the loop and hands its end back to the loop, which runs it.
|
||||
func (w *Watcher) besideLoop(work func() func()) {
|
||||
go func() { w.ends <- work() }()
|
||||
}
|
||||
|
||||
// sessionDesk opens and tells in the person's session: the programs run with the session's bus and runtime directory
|
||||
// in their environment.
|
||||
type sessionDesk struct {
|
||||
// query runs a program to its end, within openBound, and answers its output.
|
||||
query func(env []string, name string, args ...string) (string, error)
|
||||
// start starts a program; its end, with its status, comes on the channel.
|
||||
start func(env []string, name string, args ...string) (<-chan error, error)
|
||||
after func(time.Duration) <-chan time.Time
|
||||
show func(bus string, n note) error
|
||||
}
|
||||
|
||||
func newSessionDesk() sessionDesk {
|
||||
return sessionDesk{query: queryProgram, start: startProgram, after: time.After, show: notifyOn}
|
||||
}
|
||||
|
||||
// open asks xdg-mime for the scheme's program, then runs `systemd-run --user --collect --quiet --wait -- xdg-open
|
||||
// <address>`, the address one argument and no shell, so the browser runs under the person's user manager and outlives
|
||||
// the watcher (§18).
|
||||
func (d sessionDesk) open(s Session, address string) openOutcome {
|
||||
o := openOutcome{Address: address, Outcome: outcomeFailed}
|
||||
u, err := url.Parse(address)
|
||||
if err != nil {
|
||||
o.Reason = err.Error()
|
||||
return o
|
||||
}
|
||||
program, err := d.query(s.Env(), "xdg-mime", "query", "default", "x-scheme-handler/"+u.Scheme)
|
||||
if err != nil {
|
||||
o.Reason = "xdg-mime: " + err.Error()
|
||||
return o
|
||||
}
|
||||
if program = strings.TrimSpace(program); program == "" {
|
||||
o.Reason = "no default program for " + u.Scheme
|
||||
return o
|
||||
}
|
||||
done, err := d.start(s.Env(), "systemd-run", "--user", "--collect", "--quiet", "--wait", "--", "xdg-open", address)
|
||||
if err != nil {
|
||||
o.Reason = "systemd-run: " + err.Error()
|
||||
return o
|
||||
}
|
||||
o.Program = program
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
o.Program, o.Reason = "", "xdg-open "+status(err)
|
||||
return o
|
||||
}
|
||||
o.Outcome = outcomeOpened
|
||||
case <-d.after(openBound):
|
||||
o.Outcome = outcomeOpening
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func (d sessionDesk) notify(s Session, n note) error { return d.show(s.Bus(), n) }
|
||||
|
||||
// status names how a program ended: its exit status, or why it did not end with one.
|
||||
func status(err error) string {
|
||||
var exit *exec.ExitError
|
||||
if errors.As(err, &exit) {
|
||||
return fmt.Sprintf("ended with status %d", exit.ExitCode())
|
||||
}
|
||||
return "failed: " + err.Error()
|
||||
}
|
||||
|
||||
func queryProgram(env []string, name string, args ...string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), openBound)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, name, args...)
|
||||
cmd.Env = env
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", errors.New(status(err))
|
||||
}
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
func startProgram(env []string, name string, args ...string) (<-chan error, error) {
|
||||
cmd := exec.Command(name, args...)
|
||||
cmd.Env = env
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- cmd.Wait() }()
|
||||
return done, nil
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// aDeskWatcher is a watcher with the person's session found and the desktop stood in; it starts subscribed, the rating
|
||||
// at its start being start.
|
||||
func aDeskWatcher(t *testing.T, start Rating) (*Watcher, *fakeSystem, *fakeDesk) {
|
||||
t.Helper()
|
||||
sys := &fakeSystem{sessions: []loginSession{personsSession()}}
|
||||
sys.set(start)
|
||||
w := aWatcher(t, sys)
|
||||
w.Environment = func(string) ([]string, error) { return []string{"DISPLAY=:1", "XAUTHORITY=/x"}, nil }
|
||||
w.subscribe()
|
||||
return w, sys, w.Desk.(*fakeDesk)
|
||||
}
|
||||
|
||||
const openedNote = "This network asks you to sign in: guest-example: its sign-in page is open in browser.desktop."
|
||||
|
||||
// PORTAL at a join and PORTAL at the watcher's start each open the page and show one notification; a second PORTAL
|
||||
// without a leave does nothing; PORTAL to FULL shows that the network is open, PORTAL to any other rating shows nothing
|
||||
// (design 08 sign-in-page §15, §19, §25).
|
||||
func TestTheRatingOpensThePageAndTellsThePersonOncePerAsk(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
start Rating
|
||||
then []Rating
|
||||
opens int
|
||||
notes []string
|
||||
journal string
|
||||
}{
|
||||
{"PORTAL at a join", guest(ratingFull), []Rating{guest(ratingPortal)}, 1, []string{openedNote},
|
||||
"showed the notification"},
|
||||
{"PORTAL at the start", guest(ratingPortal), nil, 1, []string{openedNote}, "the sign-in page http://check.example.net/ is opened in browser.desktop"},
|
||||
{"a second PORTAL", guest(ratingPortal), []Rating{guest(ratingPortal), guest(ratingPortal)}, 1,
|
||||
[]string{openedNote}, ""},
|
||||
{"PORTAL to FULL", guest(ratingPortal), []Rating{guest(ratingFull)}, 1,
|
||||
[]string{openedNote, "The network is open: guest-example: signed in."}, ""},
|
||||
{"PORTAL to LIMITED", guest(ratingPortal), []Rating{guest(ratingLimited)}, 1, []string{openedNote}, ""},
|
||||
{"PORTAL, left, PORTAL on another", guest(ratingPortal), []Rating{other(ratingPortal)}, 2,
|
||||
[]string{openedNote, "This network asks you to sign in: home-example: its sign-in page is open in browser.desktop."}, ""},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
lines := journalLines(t)
|
||||
w, sys, desk := aDeskWatcher(t, c.start)
|
||||
for _, r := range c.then {
|
||||
sys.set(r)
|
||||
w.heardSignal(signal{Rating: true})
|
||||
}
|
||||
if got := desk.opened(); len(got) != c.opens {
|
||||
t.Fatalf("opened %q", got)
|
||||
}
|
||||
if got := desk.shown(); strings.Join(got, "|") != strings.Join(c.notes, "|") {
|
||||
t.Fatalf("showed %q, want %q", got, c.notes)
|
||||
}
|
||||
if !strings.Contains(strings.Join(lines(), "\n"), c.journal) {
|
||||
t.Fatalf("the journal does not say %q: %q", c.journal, lines())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// An opener that fails gives the critical notification naming the reason and the address (§19, §25).
|
||||
func TestAFailingOpenerGivesTheCriticalNotification(t *testing.T) {
|
||||
journalLines(t)
|
||||
sys := &fakeSystem{sessions: []loginSession{personsSession()}}
|
||||
sys.set(guest(ratingFull))
|
||||
w := aWatcher(t, sys)
|
||||
w.Environment = func(string) ([]string, error) { return []string{"DISPLAY=:1"}, nil }
|
||||
desk := w.Desk.(*fakeDesk)
|
||||
desk.outcome = openOutcome{Address: "http://check.example.net/", Outcome: outcomeFailed, Reason: "xdg-open ended with status 4"}
|
||||
w.subscribe()
|
||||
sys.set(guest(ratingPortal))
|
||||
w.heardSignal(signal{Rating: true})
|
||||
want := "critical This network asks you to sign in: guest-example: the page did not open (xdg-open ended with status 4); " +
|
||||
"open http://check.example.net/ in a browser."
|
||||
if got := desk.shown(); len(got) != 1 || got[0] != want {
|
||||
t.Fatalf("showed %q", got)
|
||||
}
|
||||
if w.state.LastOpen == nil || w.state.LastOpen.Outcome != outcomeFailed || w.state.LastNotification == nil {
|
||||
t.Fatalf("the state says %+v", w.state)
|
||||
}
|
||||
}
|
||||
|
||||
// A rating change heard while the page opens is handled after the open, in the order heard (§18, §25).
|
||||
func TestARatingChangeHeardDuringAnOpenIsHandledAfterIt(t *testing.T) {
|
||||
journalLines(t)
|
||||
sys := &fakeSystem{sessions: []loginSession{personsSession()}}
|
||||
sys.set(guest(ratingFull))
|
||||
w := aWatcher(t, sys)
|
||||
w.Environment = func(string) ([]string, error) { return []string{"DISPLAY=:1"}, nil }
|
||||
var beside []func() func()
|
||||
w.Beside = func(work func() func()) { beside = append(beside, work) }
|
||||
w.subscribe()
|
||||
sys.set(guest(ratingPortal))
|
||||
w.heardSignal(signal{Rating: true})
|
||||
sys.set(guest(ratingLimited))
|
||||
w.heardSignal(signal{Rating: true})
|
||||
sys.set(guest(ratingFull))
|
||||
w.heardSignal(signal{Rating: true})
|
||||
if got := keptSaid(t, w.Folder); len(got) != 2 || len(beside) != 1 {
|
||||
t.Fatalf("during the open: kept %q, %d run beside", got, len(beside))
|
||||
}
|
||||
beside[0]()()
|
||||
got := keptSaid(t, w.Folder)
|
||||
if len(got) != 3 || got[2] != "sign-in-ended no-longer-asked guest-example wlan0" {
|
||||
t.Fatalf("after the open: kept %q", got)
|
||||
}
|
||||
if w.opening || len(w.later) != 0 {
|
||||
t.Fatal("what was heard is still waiting")
|
||||
}
|
||||
}
|
||||
|
||||
// The opener in the person's session: xdg-mime asked for the scheme's program, then systemd-run --user with the address
|
||||
// as one argument, both with the session's bus and runtime directory; its status, or its bound, decides (§18).
|
||||
func TestTheOpenerRunsInThePersonsSession(t *testing.T) {
|
||||
s := Session{ID: "2", Type: "x11", RuntimePath: "/run/user/1000"}
|
||||
address := "http://check.example.net/a'b;c"
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
program string
|
||||
ends error
|
||||
running bool
|
||||
want openOutcome
|
||||
}{
|
||||
{"opened", "browser.desktop\n", nil, false, openOutcome{Address: address, Program: "browser.desktop", Outcome: outcomeOpened}},
|
||||
{"opening", "browser.desktop\n", nil, true, openOutcome{Address: address, Program: "browser.desktop", Outcome: outcomeOpening}},
|
||||
{"failed", "browser.desktop\n", exitStatus(t, 3), false,
|
||||
openOutcome{Address: address, Outcome: outcomeFailed, Reason: "xdg-open ended with status 3"}},
|
||||
{"no program", "\n", nil, false, openOutcome{Address: address, Outcome: outcomeFailed, Reason: "no default program for http"}},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
var ran []string
|
||||
envOf := func(env []string) string {
|
||||
var out []string
|
||||
for _, kv := range env {
|
||||
if strings.HasPrefix(kv, "DBUS_SESSION_BUS_ADDRESS=") || strings.HasPrefix(kv, "XDG_RUNTIME_DIR=") {
|
||||
out = append(out, kv)
|
||||
}
|
||||
}
|
||||
return strings.Join(out, " ")
|
||||
}
|
||||
bound := make(chan time.Time, 1)
|
||||
d := sessionDesk{
|
||||
query: func(env []string, name string, args ...string) (string, error) {
|
||||
ran = append(ran, envOf(env)+" | "+name+" "+strings.Join(args, " "))
|
||||
return c.program, nil
|
||||
},
|
||||
start: func(env []string, name string, args ...string) (<-chan error, error) {
|
||||
ran = append(ran, envOf(env)+" | "+name+" "+strings.Join(args, " "))
|
||||
if args[len(args)-1] != address {
|
||||
t.Errorf("the address is not one argument: %q", args)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
if c.running {
|
||||
bound <- time.Now()
|
||||
} else {
|
||||
done <- c.ends
|
||||
}
|
||||
return done, nil
|
||||
},
|
||||
after: func(d time.Duration) <-chan time.Time {
|
||||
if d != 10*time.Second {
|
||||
t.Errorf("the open's bound is %s", d)
|
||||
}
|
||||
return bound
|
||||
},
|
||||
}
|
||||
got := d.open(s, address)
|
||||
if got != c.want {
|
||||
t.Fatalf("answered %+v, want %+v", got, c.want)
|
||||
}
|
||||
env := "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus XDG_RUNTIME_DIR=/run/user/1000 | "
|
||||
want := []string{env + "xdg-mime query default x-scheme-handler/http"}
|
||||
if c.program != "\n" {
|
||||
want = append(want, env+"systemd-run --user --collect --quiet --wait -- xdg-open "+address)
|
||||
}
|
||||
if strings.Join(ran, "\n") != strings.Join(want, "\n") {
|
||||
t.Fatalf("ran\n%s\nwant\n%s", strings.Join(ran, "\n"), strings.Join(want, "\n"))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// exitStatus is the error of a program that ended with status n.
|
||||
func exitStatus(t *testing.T, n int) error {
|
||||
t.Helper()
|
||||
err := exec.Command("sh", "-c", "exit "+string(rune('0'+n))).Run()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) {
|
||||
t.Fatalf("no exit status: %v", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -207,50 +207,3 @@ type sessionNote struct {
|
||||
At time.Time `json:"at"`
|
||||
Did string `json:"did"`
|
||||
}
|
||||
|
||||
// desk is the person's desktop as the watcher reaches it: opening an address in their session (§18).
|
||||
type desk interface {
|
||||
open(s Session, address string) openOutcome
|
||||
}
|
||||
|
||||
// openOutcome is how an open ended: opened, opening (still running at its bound) or failed, with the program and the
|
||||
// reason of a failure.
|
||||
type openOutcome struct {
|
||||
Address string `json:"address"`
|
||||
Program string `json:"program,omitempty"`
|
||||
Outcome string `json:"outcome"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// The outcomes of an open.
|
||||
const (
|
||||
outcomeOpened = "opened"
|
||||
outcomeOpening = "opening"
|
||||
outcomeFailed = "failed"
|
||||
)
|
||||
|
||||
// present opens the address in the person's session, s or the one found now (§15, §18); with no session found it opens
|
||||
// nothing, and the events are kept all the same (§15, §16).
|
||||
func (w *Watcher) present(s *Session, network, address string) {
|
||||
if s == nil {
|
||||
found, err := w.findSession()
|
||||
if err != nil {
|
||||
journal("finding the person's session failed: %v; the sign-in page of %q is not opened", err, network)
|
||||
return
|
||||
}
|
||||
if found == nil {
|
||||
journal("no graphical session: the sign-in page of %q is not opened", network)
|
||||
return
|
||||
}
|
||||
s = found
|
||||
}
|
||||
o := w.Desk.open(*s, address)
|
||||
o.At = w.Now()
|
||||
w.state.LastOpen = &o
|
||||
if o.Outcome == outcomeFailed {
|
||||
journal("opening %s in session %s failed: %s", address, s.ID, o.Reason)
|
||||
} else {
|
||||
journal("the sign-in page %s is %s in %s", address, o.Outcome, o.Program)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +123,7 @@ type watcherState struct {
|
||||
AsksEnded int `json:"asks_ended"`
|
||||
DroppedPastBound int `json:"dropped_past_bound"`
|
||||
LastOpen *openOutcome `json:"last_open,omitempty"`
|
||||
LastNotification *note `json:"last_notification,omitempty"`
|
||||
LastSession *sessionNote `json:"last_session,omitempty"`
|
||||
}
|
||||
|
||||
@@ -155,8 +156,13 @@ type Watcher struct {
|
||||
// After is a stated bound's end.
|
||||
After func(time.Duration) <-chan time.Time
|
||||
Desk desk
|
||||
// Beside runs an open beside the loop; the func it returns is run by the loop at its end.
|
||||
Beside func(work func() func())
|
||||
|
||||
starting *sessionStart
|
||||
opening bool
|
||||
later []func()
|
||||
ends chan func()
|
||||
asks chan asked
|
||||
bus systemBus
|
||||
signals <-chan signal
|
||||
@@ -169,8 +175,11 @@ type Watcher struct {
|
||||
}
|
||||
|
||||
func newWatcher(f Folder) *Watcher {
|
||||
return &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked),
|
||||
UID: uint32(os.Getuid()), Environment: userEnvironment, FileEvents: fileEvents, After: time.After}
|
||||
w := &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked),
|
||||
UID: uint32(os.Getuid()), Environment: userEnvironment, FileEvents: fileEvents, After: time.After,
|
||||
Desk: newSessionDesk(), ends: make(chan func())}
|
||||
w.Beside = w.besideLoop
|
||||
return w
|
||||
}
|
||||
|
||||
// watch is `uplink-tools watch`: the watcher, until its process is stopped.
|
||||
@@ -229,6 +238,8 @@ func (w *Watcher) run(stop <-chan struct{}) {
|
||||
case <-w.socketAt:
|
||||
w.socketAt = nil
|
||||
w.subscribe()
|
||||
case end := <-w.ends:
|
||||
end()
|
||||
case a := <-w.asks:
|
||||
answer, err := w.answer(a.ask)
|
||||
a.reply <- askReply{answer, err}
|
||||
@@ -307,9 +318,16 @@ func (w *Watcher) readRating() {
|
||||
journal("reading the network manager's rating failed: %v", err)
|
||||
return
|
||||
}
|
||||
atStart := !w.started
|
||||
w.started = true
|
||||
w.heard(r, atStart)
|
||||
if !w.started {
|
||||
w.started = true
|
||||
w.heard(r, true)
|
||||
return
|
||||
}
|
||||
if w.opening {
|
||||
w.later = append(w.later, func() { w.heard(r, false) })
|
||||
return
|
||||
}
|
||||
w.heard(r, false)
|
||||
}
|
||||
|
||||
// heard acts on a rating read: the check address, a journal line for a change, and the events of §21.
|
||||
@@ -352,7 +370,9 @@ func (w *Watcher) heard(r Rating, atStart bool) {
|
||||
case !r.checkOn() || (r.Connectivity != ratingPortal && r.Connectivity != ratingFull):
|
||||
w.ended(w.ask.Link, w.ask.Network, whyNoLongerAsked)
|
||||
case r.Connectivity == ratingFull:
|
||||
w.ended(w.ask.Link, w.ask.Network, whySignedIn)
|
||||
network := w.ask.Network
|
||||
w.ended(w.ask.Link, network, whySignedIn)
|
||||
w.tellSignedIn(network)
|
||||
default:
|
||||
return
|
||||
}
|
||||
@@ -375,6 +395,7 @@ func (w *Watcher) asked(r Rating) {
|
||||
w.state.AsksOpened++
|
||||
w.keep(eventAsked, signInAsked{Node: w.Folder.Node, Link: r.Link, Network: r.Network, Address: r.CheckURI,
|
||||
At: stamp(now)})
|
||||
w.present(nil, r.Network, r.CheckURI)
|
||||
}
|
||||
|
||||
// ended keeps sign-in-ended and drops the ask.
|
||||
|
||||
@@ -120,16 +120,40 @@ func aWatcher(t *testing.T, sys *fakeSystem) *Watcher {
|
||||
}, SocketThere: func(stop <-chan struct{}) <-chan struct{} { return sys.socket }, asks: make(chan asked),
|
||||
UID: 1000, Desk: &fakeDesk{}, Environment: func(string) ([]string, error) { return nil, nil },
|
||||
FileEvents: func([]string, <-chan struct{}) <-chan struct{} { return nil },
|
||||
After: func(time.Duration) <-chan time.Time { return nil }}
|
||||
After: func(time.Duration) <-chan time.Time { return nil },
|
||||
Beside: func(work func() func()) { work()() }}
|
||||
}
|
||||
|
||||
// The desktop stood in: each open it was asked, answered with outcome.
|
||||
type fakeDesk struct {
|
||||
mu sync.Mutex
|
||||
opens []string
|
||||
notes []note
|
||||
outcome openOutcome
|
||||
}
|
||||
|
||||
func (d *fakeDesk) notify(s Session, n note) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
d.notes = append(d.notes, n)
|
||||
return nil
|
||||
}
|
||||
|
||||
// shown is every notification shown, as `[critical] summary: body`.
|
||||
func (d *fakeDesk) shown() []string {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
var out []string
|
||||
for _, n := range d.notes {
|
||||
critical := ""
|
||||
if n.Critical {
|
||||
critical = "critical "
|
||||
}
|
||||
out = append(out, critical+n.Summary+": "+n.Body)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (d *fakeDesk) open(s Session, address string) openOutcome {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user