2 * Copyright (C) 2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
24 socketio "github.com/googollee/go-socket.io"
25 "github.com/syncthing/syncthing/lib/sync"
28 // Terminals Represent a XDS terminals
29 type Terminals struct {
31 terms map[string]*ITERMINAL
34 // Mutex to make add/delete atomic
35 var tmMutex = sync.NewMutex()
37 // TerminalsConstructor Create a new instance of Model Terminal
38 func TerminalsConstructor(ctx *Context) *Terminals {
41 terms: make(map[string]*ITERMINAL),
45 // New Create a new terminal
46 func (t *Terminals) New(cfg xsapiv1.TerminalConfig, targetID string) (*xsapiv1.TerminalConfig, error) {
49 defer tmMutex.Unlock()
53 // For now, only SSH term is supported
55 case xsapiv1.TypeTermSSH:
56 newT = NewTermSSH(t.Context, cfg, targetID)
58 return nil, fmt.Errorf("terminal type not set")
61 termCfg := newT.GetConfig()
63 t.terms[termCfg.ID] = &newT
68 // Free a specific terminal
69 func (t *Terminals) Free(id string) (*xsapiv1.TerminalConfig, error) {
72 defer tmMutex.Unlock()
76 return nil, fmt.Errorf("Unknown id")
79 if _, err := (*tc).Close(); err != nil {
83 resTerm := (*tc).GetConfig()
90 // Get returns the terminal config or nil if not existing
91 func (t *Terminals) Get(id string) *ITERMINAL {
95 tc, exist := t.terms[id]
102 // GetConfigArr returns the config of all terminals as an array
103 func (t *Terminals) GetConfigArr() []xsapiv1.TerminalConfig {
105 defer tmMutex.Unlock()
107 return t.getConfigArrUnsafe()
110 // getConfigArrUnsafe Same as GetConfigArr without mutex protection
111 func (t *Terminals) getConfigArrUnsafe() []xsapiv1.TerminalConfig {
112 conf := []xsapiv1.TerminalConfig{}
113 for _, v := range t.terms {
114 conf = append(conf, (*v).GetConfig())
119 // Open adds a new terminal
120 func (t *Terminals) Open(id string, sock *socketio.Socket, sessID string) (*xsapiv1.TerminalConfig, error) {
123 return nil, fmt.Errorf("Unknown id")
125 return (*tc).Open(sock, sessID)
128 // Close a specific terminal
129 func (t *Terminals) Close(id string) (*xsapiv1.TerminalConfig, error) {
132 return nil, fmt.Errorf("Unknown id")
137 // Resize a specific terminal
138 func (t *Terminals) Resize(id string, cols, rows uint16) (*xsapiv1.TerminalConfig, error) {
140 defer tmMutex.Unlock()
144 return nil, fmt.Errorf("Unknown id")
146 return (*tc).Resize(cols, rows)
149 // Signal Send a Signal a specific terminal
150 func (t *Terminals) Signal(id, sigName string) error {
152 defer tmMutex.Unlock()
156 return fmt.Errorf("Unknown id")
158 return (*tc).Signal(sigName)