forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
67 lines (58 loc) · 1.8 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package pwd
import (
"log"
"time"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd/types"
)
func (p *pwd) ClientNew(id string, session *types.Session) *types.Client {
defer observeAction("ClientNew", time.Now())
c := &types.Client{Id: id, SessionId: session.Id}
if err := p.storage.ClientPut(c); err != nil {
log.Println("Error saving client", err)
}
return c
}
func (p *pwd) ClientResizeViewPort(c *types.Client, cols, rows uint) {
defer observeAction("ClientResizeViewPort", time.Now())
c.ViewPort.Rows = rows
c.ViewPort.Cols = cols
if err := p.storage.ClientPut(c); err != nil {
log.Println("Error saving client", err)
return
}
p.notifyClientSmallestViewPort(c.SessionId)
}
func (p *pwd) ClientClose(client *types.Client) {
defer observeAction("ClientClose", time.Now())
// Client has disconnected. Remove from session and recheck terminal sizes.
if err := p.storage.ClientDelete(client.Id); err != nil {
log.Println("Error deleting client", err)
return
}
p.notifyClientSmallestViewPort(client.SessionId)
}
func (p *pwd) ClientCount() int {
count, err := p.storage.ClientCount()
if err != nil {
log.Println("Error counting clients", err)
return 0
}
return count
}
func (p *pwd) notifyClientSmallestViewPort(sessionId string) {
instances, err := p.storage.InstanceFindBySessionId(sessionId)
if err != nil {
log.Printf("Error finding instances for session [%s]. Got: %v\n", sessionId, err)
return
}
vp := p.SessionGetSmallestViewPort(sessionId)
// Resize all terminals in the session
for _, instance := range instances {
err := p.InstanceResizeTerminal(instance, vp.Rows, vp.Cols)
if err != nil {
log.Println("Error resizing terminal", err)
}
}
p.event.Emit(event.INSTANCE_VIEWPORT_RESIZE, sessionId, vp.Cols, vp.Rows)
}