func NewSsn(execOutFile string, onErr chan error) *Ssn { var tail *cmn.CmdWrapper = nil if len(execOutFile) > 0 { tail = cmn.NewCmdWrapper(exec.Command("tail", "-f", execOutFile)) } return &Ssn{ 499, false, false, &sync.Mutex{}, execOutFile, cmn.NewCmdWrapper(exec.Command("bash")), tail, // make([]string, 0, 11), // make([][]*Record, 0), onErr, make(chan *Msg), make(chan string), make(chan []string), } }
func (ssn *nvlvSsn) Run() error { var err error ssn.shMsgBody = &clientBody{ "sh", make(map[string]interface{}), } ssn.exeMsgBody = &clientBody{ "exe", make(map[string]interface{}), } ssn.gdbMsgBody = &clientBody{ "gdb", make(map[string]interface{}), } ssn.cmdMsgBody = &clientBody{ "cmd", make(map[string]interface{}), } gdbErrChan := make(chan error) ssn.gdbErr = gdbErrChan ssn.gdbSsn = gdb.NewSsn(ssn.gdbExecOut, gdbErrChan) gdbInferiorOut := ssn.gdbSsn.InferiorOutput() gdbOutput := ssn.gdbSsn.GdbOutput() ssn.shCmd = cmn.NewCmdWrapper(exec.Command("bash")) if err = ssn.shCmd.Start(); err != nil { log.Println("Err: Unable to start shell command for nvlv session: ", err) return err } ssn.msgFromClient = make(chan *clientMsg) go recvJsonLoop(ssn.ws, ssn.msgFromClient) for { select { case m := <-ssn.shCmd.OutChan(): if isReadErr(m.Err, "sh out stream") { err = m.Err goto endConn } ssn.shMsgBody.sendMsg(ssn.ws, m.Msg) case m := <-ssn.shCmd.ErrChan(): if isReadErr(m.Err, "sh err stream") { err = m.Err goto endConn } ssn.shMsgBody.sendErr(ssn.ws, m.Msg) case err = <-gdbErrChan: log.Println("gbd err: ", err) ssn.gdbMsgBody.sendErr(ssn.ws, err.Error()) goto endConn case s := <-gdbInferiorOut: ssn.exeMsgBody.sendMsg(ssn.ws, s) case gdbMsg := <-gdbOutput: ssn.lastGdbRecs = gdbMsg.Records ssn.gdbMsgBody.sendData(ssn.ws, gdbMsg.Records, "raw", gdbMsg.Raw) case m := <-ssn.msgFromClient: if isReadErr(m.err, "client") { err = m.err goto endConn } log.Println("msg from client:", m.data) err = handleMsg(ssn, m) } } endConn: log.Println("Killing cmds") if ssn.shCmd.IsStarted() && !ssn.shCmd.IsKilled() { ssn.shCmd.InChan() <- "exit" ssn.shCmd.KillRelease() } ssn.gdbSsn.Kill() return err }