Refactor storage to support shallow types.
Add Client to storage. Fix client resizing issues.
This commit is contained in:
@@ -2,7 +2,6 @@ package pwd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/play-with-docker/play-with-docker/event"
|
||||
@@ -11,9 +10,10 @@ import (
|
||||
|
||||
func (p *pwd) ClientNew(id string, session *types.Session) *types.Client {
|
||||
defer observeAction("ClientNew", time.Now())
|
||||
c := &types.Client{Id: id, Session: session}
|
||||
session.Clients = append(session.Clients, c)
|
||||
p.clientCount = atomic.AddInt32(&p.clientCount, 1)
|
||||
c := &types.Client{Id: id, SessionId: session.Id}
|
||||
if err := p.storage.ClientPut(c); err != nil {
|
||||
log.Println("Error saving client", err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -22,38 +22,46 @@ func (p *pwd) ClientResizeViewPort(c *types.Client, cols, rows uint) {
|
||||
c.ViewPort.Rows = rows
|
||||
c.ViewPort.Cols = cols
|
||||
|
||||
p.notifyClientSmallestViewPort(c.Session)
|
||||
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.
|
||||
session := client.Session
|
||||
for i, cl := range session.Clients {
|
||||
if cl.Id == client.Id {
|
||||
session.Clients = append(session.Clients[:i], session.Clients[i+1:]...)
|
||||
p.clientCount = atomic.AddInt32(&p.clientCount, -1)
|
||||
break
|
||||
}
|
||||
if err := p.storage.ClientDelete(client.Id); err != nil {
|
||||
log.Println("Error deleting client", err)
|
||||
return
|
||||
}
|
||||
if len(session.Clients) > 0 {
|
||||
p.notifyClientSmallestViewPort(session)
|
||||
}
|
||||
p.setGauges()
|
||||
p.notifyClientSmallestViewPort(client.SessionId)
|
||||
}
|
||||
|
||||
func (p *pwd) ClientCount() int {
|
||||
return int(atomic.LoadInt32(&p.clientCount))
|
||||
count, err := p.storage.ClientCount()
|
||||
if err != nil {
|
||||
log.Println("Error counting clients", err)
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func (p *pwd) notifyClientSmallestViewPort(session *types.Session) {
|
||||
vp := p.SessionGetSmallestViewPort(session)
|
||||
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
|
||||
p.event.Emit(event.INSTANCE_VIEWPORT_RESIZE, session.Id, vp.Cols, vp.Rows)
|
||||
for _, instance := range session.Instances {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user