Simplify structure of command messages

The first byte of a input message shows the type of that message.
0: normal keystrokes
1: resize window command
This commit is contained in:
Iwasaki Yudai 2015-08-21 13:40:39 +09:00
parent ce96943af2
commit 5eea5067db
3 changed files with 35 additions and 53 deletions

View File

@ -158,7 +158,7 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
} }
switch data[0] { switch data[0] {
case '0': case Input:
if !app.options.PermitWrite { if !app.options.PermitWrite {
break break
} }
@ -168,41 +168,22 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
return return
} }
case '1': case ResizeTerminal:
var remoteCmd command var args argResizeTerminal
err = json.Unmarshal(data[1:], &remoteCmd) err = json.Unmarshal(data[1:], &args)
if err != nil { if err != nil {
log.Print("Malformed remote command") log.Print("Malformed remote command")
return return
} }
switch remoteCmd.Name {
case "resize_terminal":
rows := remoteCmd.Arguments["rows"]
switch rows.(type) {
case float64:
default:
log.Print("Malformed remote command")
return
}
cols := remoteCmd.Arguments["columns"]
switch cols.(type) {
case float64:
default:
log.Print("Malformed remote command")
return
}
window := struct { window := struct {
row uint16 row uint16
col uint16 col uint16
x uint16 x uint16
y uint16 y uint16
}{ }{
uint16(rows.(float64)), uint16(args.Rows),
uint16(cols.(float64)), uint16(args.Columns),
0, 0,
0, 0,
} }
@ -212,7 +193,6 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
syscall.TIOCSWINSZ, syscall.TIOCSWINSZ,
uintptr(unsafe.Pointer(&window)), uintptr(unsafe.Pointer(&window)),
) )
}
default: default:
log.Print("Unknown message type") log.Print("Unknown message type")
@ -230,9 +210,14 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
} }
} }
type command struct { const (
Name string `json:"name"` Input = '0'
Arguments map[string]interface{} `json:"arguments"` ResizeTerminal = '1'
)
type argResizeTerminal struct {
Columns float64
Rows float64
} }
func generateRandomString(length int) string { func generateRandomString(length int) string {

File diff suppressed because one or more lines are too long

View File

@ -24,11 +24,8 @@
ws.send( ws.send(
"1" + JSON.stringify( "1" + JSON.stringify(
{ {
name: "resize_terminal",
arguments: {
columns: columns, columns: columns,
rows: rows, rows: rows,
},
} }
) )
) )