diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-08-07 18:08:27 +0200 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-08-07 19:30:25 +0200 |
commit | 7496dbabaf710a9e0f3b599c83163adddfcb8870 (patch) | |
tree | d2ab9f9f89edc0f381a71a967d920cef3e150be5 /golib/eows/eows-in.go | |
parent | 62e2996fcbcd704653d3043046c451fbc044918b (diff) |
Added eows (Exec Over WebSocket) package.
Diffstat (limited to 'golib/eows/eows-in.go')
-rw-r--r-- | golib/eows/eows-in.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/golib/eows/eows-in.go b/golib/eows/eows-in.go new file mode 100644 index 0000000..1ecd2a1 --- /dev/null +++ b/golib/eows/eows-in.go @@ -0,0 +1,56 @@ +package eows + +import ( + "fmt" + "os" + "syscall" + "time" +) + +type DoneChan struct { + status int + err error +} + +// cmdPumpStdin is in charge of receive characters and send them to stdin +func (e *ExecOverWS) cmdPumpStdin(inw *os.File) { + + done := make(chan DoneChan, 1) + + if e.InputEvent != "" && e.InputCB != nil { + + err := (*e.SocketIO).On(e.InputEvent, func(stdin string) { + in, err := e.InputCB(e, string(stdin)) + if err != nil { + e.logDebug("Error stdin: %s", err.Error()) + inw.Close() + return + } + if _, err := inw.Write([]byte(in)); err != nil { + e.logError("Error while writing to stdin: %s", err.Error()) + } + }) + if err != nil { + e.logError("Error stdin on event: %s", err.Error()) + } + } + + // Monitor process exit + go func() { + status := 0 + sts, err := e.proc.Wait() + if !sts.Success() { + s := sts.Sys().(syscall.WaitStatus) + status = s.ExitStatus() + } + done <- DoneChan{status, err} + }() + + // Wait cmd complete + select { + case dC := <-done: + e.ExitCB(e, dC.status, dC.err) + case <-time.After(time.Duration(e.CmdExecTimeout) * time.Second): + e.ExitCB(e, -999, fmt.Errorf("Exit Timeout for command ID %v", e.CmdID)) + } +} |