package ui import ( "context" "errors" "slices" "time" "100gex/internal/hosts" "100gex/internal/rex" "100gex/internal/workspace" ) // server is a Rex server the window is connected to: the connection, the // workspace on it, and what reaches it again. The window shows one; the // others are parked, keeping their connections and lists of sessions, so // that the sidebar lists every connected server's sessions. type server struct { conn *rex.Conn ws *workspace.Workspace label string // the host's label it was reached by, or "" // target reaches it again: "" for the local server, a host's label, or // an address. target string parked string // the session shown when it was switched away from // Fetched in the background; guarded by UI.mu. version, account string } // find returns the connected server that target reaches, or nil. func (u *UI) find(target string) *server { for _, s := range u.servers { if s.target == target || (s.label != "" && s.label == target) { return s } } return nil } // connecting is a switch to another server, or a reconnect, under way. type connecting struct { target string label string // what to call the server, if target doesn't say reconnect bool err error // why the last attempt failed } // SetTarget says what reached the first server, so that a reconnect can // reach it again, and the label to call it by. func (u *UI) SetTarget(target, label string) { u.srv.target, u.srv.label = target, label } // setServer makes s the server the window shows. func (u *UI) setServer(s *server) { u.srv, u.conn, u.ws = s, s.conn, s.ws } // watch passes a server's events to its workspace, and to the UI while it's // the one shown, until its connection ends, and then asks for a frame, so // that the UI notices. func (u *UI) watch(s *server) { go func() { for ev := range s.conn.Events() { u.mu.Lock() current := u.watched == s u.mu.Unlock() if current { u.handleEvent(s.conn, ev) } if ev.Name == "hosts_changed" { u.fetchSaved() } // A parked server's workspace keeps its list of sessions. s.ws.HandleEvent(ev) } u.notify() }() } // Connect switches the window to another server: "" for the local one, a // host's label, or an address. It connects in the background, unless the // window is connected to it already; the window keeps showing the current // server until the new one is ready. func (u *UI) Connect(target string) { if s := u.find(target); s != nil { u.switchTo(s, s.parked) return } u.mu.Lock() if u.connecting != nil && u.connecting.target == target && u.connecting.err == nil { u.mu.Unlock() return } c := &connecting{target: target} u.connecting = c u.mu.Unlock() u.notify() go u.dial(c, "") } // switchTo shows a connected server, and the session sessionID on it, or // the one it last showed if that's "". It attaches in the background. func (u *UI) switchTo(s *server, sessionID string) { if s == u.srv { if sessionID != "" { u.ws.Switch(sessionID) } return } if sessionID == "" { sessionID = s.parked } u.resume(s, func(ctx context.Context) error { return s.ws.Resume(ctx, sessionID) }) } // newSessionOn makes a session on a connected server, and shows it. func (u *UI) newSessionOn(s *server) { if s == u.srv { u.ws.NewSession() return } u.resume(s, s.ws.ResumeNew) } // resume shows a parked server once attach has attached its workspace to a // session, in the background. func (u *UI) resume(s *server, attach func(context.Context) error) { c := &connecting{target: s.target} u.mu.Lock() u.connecting = c u.mu.Unlock() u.notify() go func() { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err := attach(ctx) u.mu.Lock() defer u.mu.Unlock() if u.connecting != c { // Superseded. The session was never shown, so it holds no // panes, and parking it here is safe. if err == nil { go s.ws.Park() } return } if err != nil { c.err = err } else { u.next = s } u.notify() }() } // dial connects to c.target and hands the server to the drawing goroutine. // session, if set, is the session to show first. func (u *UI) dial(c *connecting, session string) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() conn, ep, label, err := hosts.Dial(ctx, c.target) var ws *workspace.Workspace if err == nil { ws, err = workspace.Start(ctx, conn, ep, session, u.TerminalTheme, u.notify) if err != nil && session != "" { // The session may be gone; show the one the server picks. ws, err = workspace.Start(ctx, conn, ep, "", u.TerminalTheme, u.notify) } if err != nil { conn.Close() } } u.mu.Lock() defer u.mu.Unlock() if u.connecting != c { // Superseded by another switch while connecting. if err == nil { go func() { ws.Close(); conn.Close() }() } return } if err != nil { c.err = err u.notify() return } if label == "" { label = c.label } u.next = &server{conn: conn, ws: ws, label: label, target: c.target} u.notify() } // reconnectDelays are the waits between attempts to reach a server whose // connection dropped. var reconnectDelays = []time.Duration{0, time.Second, 2 * time.Second, 5 * time.Second, 10 * time.Second} // reconnect tries to reach the current server again, until it answers or // the person switches away, showing the session shown before. func (u *UI) reconnect(target, session string) { c := &connecting{target: target, reconnect: true} u.mu.Lock() u.connecting = c u.mu.Unlock() u.notify() go func() { for i := 0; ; i++ { time.Sleep(reconnectDelays[min(i, len(reconnectDelays)-1)]) u.mu.Lock() current := u.connecting == c u.mu.Unlock() if !current { return } u.dial(c, session) u.mu.Lock() done := u.connecting != c || u.next != nil u.mu.Unlock() if done { return } } }() } // syncServer runs on the drawing goroutine: it puts in place a server that // has finished connecting, and starts reconnecting when the connection // drops. func (u *UI) syncServer() { u.mu.Lock() next := u.next u.next = nil if next != nil { u.connecting = nil } reconnecting := u.connecting != nil && u.connecting.reconnect u.mu.Unlock() if next != nil { old := u.srv resumed := slices.Contains(u.servers, next) u.setServer(next) u.mu.Lock() u.watched = next u.serverActions, u.bindings = nil, defaultBindings() u.mu.Unlock() u.palette.close() if !resumed { u.watch(next) } go u.fetchKeymap(next.conn) if !resumed { go u.fetchServerDetails(next) } switch { case resumed: old.parked = old.ws.Park() case old != nil && old.conn.Err() != nil: // A reconnect: the new connection takes the old one's place. // Its panes close here, on the drawing goroutine; the // connection, in the background. old.ws.Close() go old.conn.Close() u.servers[slices.Index(u.servers, old)] = next default: if old != nil { old.parked = old.ws.Park() } u.servers = append(u.servers, next) } return } // A parked server whose connection drops is forgotten. u.servers = slices.DeleteFunc(u.servers, func(s *server) bool { if s == u.srv || s.conn.Err() == nil { return false } s.ws.Close() go s.conn.Close() return true }) if err := u.conn.Err(); err != nil && !errors.Is(err, rex.ErrClosed) && !reconnecting { session := "" if u.session != nil { session = u.session.ID() } u.reconnect(u.srv.target, session) } } // connectState describes a switch or reconnect under way, for the server // chip, or returns "" when there is none. func (u *UI) connectState() (state string, err error) { u.mu.Lock() defer u.mu.Unlock() if u.connecting == nil { return "", nil } if u.connecting.reconnect { return "reconnecting", u.connecting.err } return "connecting to " + displayTarget(u.connecting.target), u.connecting.err } func displayTarget(target string) string { if target == "" { return "this machine" } return target }