package server import ( "errors" "log" "git.a-wan.net/alan/pipelines/internal/protocol" ) // The active-client policy (terminal_viewers.go) applied to machine // terminals and to chats' terminal commands: requests name their // connection, and a connection that closes hands the terminal to the next. // ClientInput is Input from clientID: typing makes it the active client // (its size, cols x rows when it sent one, else its last, is applied // first); an automatic reply (reply) from any other client is acknowledged // and dropped. func (m *terminalManager) ClientInput(clientID, id, data string, reply bool, cols, rows int) error { if p := m.process(id); p != nil && p.running() { if reply && !p.viewers.answers(clientID) { return nil } if !reply { if size := p.viewers.typedAt(clientID, cols, rows); size.ok && !p.sized(size) { if err := p.applySize(size.cols, size.rows); err != nil { log.Printf("terminal %s resize: %v", id, err) } } } } return m.Input(id, data) } // ClientResize records the size clientID draws terminal id at and applies // it when clientID is (or, with activate, becomes) the active client. The // record carries the size the terminal has. func (m *terminalManager) ClientResize(clientID, id string, cols, rows int, activate bool) (protocol.TerminalRecord, error) { if cols <= 0 || rows <= 0 { return protocol.TerminalRecord{}, errors.New("positive cols and rows are required") } p, err := m.live(id) if err != nil { return protocol.TerminalRecord{}, err } if size := p.viewers.resize(clientID, cols, rows, activate); size.ok { return m.Resize(id, size.cols, size.rows) } return p.snapshot(false, 0), nil } // attachViewer records clientID as drawing terminal id (it read it). func (m *terminalManager) attachViewer(clientID, id string) { if p := m.process(id); p != nil { p.viewers.attach(clientID) } } // detach drops clientID's connection: its output frames, and its part in // every terminal it drew; a terminal it was active in takes the next // client's size. func (m *terminalManager) detach(clientID string) { m.clients.detach(clientID) m.mu.Lock() terms := make(map[string]*terminalProcess, len(m.terms)) for id, p := range m.terms { terms[id] = p } m.mu.Unlock() for id, p := range terms { if size := p.viewers.leave(clientID); size.ok && p.running() { if err := p.applySize(size.cols, size.rows); err != nil { log.Printf("terminal %s resize: %v", id, err) } } } } // ClientInput is Input from clientID, under the same policy as the machine // terminals' ClientInput. func (r *chatShellRunner) ClientInput(clientID, sessionID, runID, data string, reply bool, cols, rows int) error { if p := r.activeProcess(sessionID, runID); p != nil && p.terminal != nil { if reply && !p.viewers.answers(clientID) { return nil } if !reply { if size := p.viewers.typedAt(clientID, cols, rows); size.ok && !p.sized(size) { if _, err := r.Resize(sessionID, runID, size.cols, size.rows); err != nil { log.Printf("chat shell %s resize: %v", runID, err) } } } } return r.Input(sessionID, runID, data) } // ClientResize is Resize from clientID, applied only when it is (or, with // activate, becomes) the active client. func (r *chatShellRunner) ClientResize(clientID, sessionID, runID string, cols, rows int, activate bool) (protocol.ChatShellRunRecord, error) { if cols <= 0 || rows <= 0 { return protocol.ChatShellRunRecord{}, errors.New("positive cols and rows are required") } p, err := r.terminalProcess(sessionID, runID) if err != nil { return protocol.ChatShellRunRecord{}, err } if size := p.viewers.resize(clientID, cols, rows, activate); size.ok { return r.Resize(sessionID, runID, size.cols, size.rows) } p.mu.Lock() defer p.mu.Unlock() return p.record, nil } // attachViewer makes clientID, which started runID at cols x rows, its // active client. func (r *chatShellRunner) attachViewer(clientID, sessionID, runID string, cols, rows int) { if p := r.activeProcess(sessionID, runID); p != nil && p.terminal != nil && cols > 0 && rows > 0 { p.viewers.resize(clientID, cols, rows, true) } } // leave drops clientID's part in every running terminal command it drew. func (r *chatShellRunner) leave(clientID string) { if r == nil { return } type run struct{ sessionID, runID string } r.mu.Lock() procs := make(map[run]*chatShellProcess, len(r.active)) for id, p := range r.active { procs[run{p.record.SessionID, id}] = p } r.mu.Unlock() for key, p := range procs { if p.terminal == nil { continue } if size := p.viewers.leave(clientID); size.ok { if _, err := r.Resize(key.sessionID, key.runID, size.cols, size.rows); err != nil { log.Printf("chat shell %s resize: %v", key.runID, err) } } } } // sized reports whether the terminal already has size (clamped). func (p *terminalProcess) sized(size terminalSize) bool { cols, rows := chatShellTerminalSize(size.cols, size.rows) p.mu.Lock() defer p.mu.Unlock() return p.record.Cols == cols && p.record.Rows == rows } // sized reports whether the terminal command already has size (clamped). func (p *chatShellProcess) sized(size terminalSize) bool { cols, rows := chatShellTerminalSize(size.cols, size.rows) p.mu.Lock() defer p.mu.Unlock() return p.record.Cols == cols && p.record.Rows == rows }