diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-21 21:19:27 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-21 21:19:27 (GMT) |
commit | 0912ca15a61bc9eba102fc967367f8c1665842dc (patch) | |
tree | 4a479f3d242020a2340d94effb5a54aa64bd9978 /src/helsinki.at/rhimportd | |
parent | c06f4c3db13c69ec5ff8f3685aa5055c2645b144 (diff) |
canceling using works now
Diffstat (limited to 'src/helsinki.at/rhimportd')
-rw-r--r-- | src/helsinki.at/rhimportd/ctrlTelnet.go | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/helsinki.at/rhimportd/ctrlTelnet.go b/src/helsinki.at/rhimportd/ctrlTelnet.go index ff94d49..41ac7f2 100644 --- a/src/helsinki.at/rhimportd/ctrlTelnet.go +++ b/src/helsinki.at/rhimportd/ctrlTelnet.go @@ -296,7 +296,7 @@ func telnet_cmd_run(ctx rhimport.ImportContext, out chan<- string) { } } -func (c *TelnetClient) handle_cmd_run(args []string) { +func (c *TelnetClient) handle_cmd_run(args []string, cancel <-chan bool) { if c.ctx == nil { c.say("context is empty please set at least one option") return @@ -306,17 +306,22 @@ func (c *TelnetClient) handle_cmd_run(args []string) { c.say("sanity check for import context returned: %s", err) return } + select { + case <-cancel: // consume potentially pending cancel request + default: + } stdout := make(chan string) c.ctx.ProgressCallBack = telnet_progress_callback c.ctx.ProgressCallBackData = (chan<- string)(stdout) + c.ctx.Cancel = cancel go telnet_cmd_run(*c.ctx, stdout) for str := range stdout { c.write_string(str) } } -func (c *TelnetClient) handle_cmd(cmdstr string, done chan<- bool) { +func (c *TelnetClient) handle_cmd(cmdstr string, done chan<- bool, cancel <-chan bool) { cmdslice := strings.Fields(cmdstr) if len(cmdslice) == 0 || cmdslice[0] == "" { done <- false @@ -337,14 +342,14 @@ func (c *TelnetClient) handle_cmd(cmdstr string, done chan<- bool) { } else if cmd == "show" { c.handle_cmd_show(args) } else if cmd == "run" { - c.handle_cmd_run(args) + c.handle_cmd_run(args, cancel) } else { c.say("unknown command '%s'", cmd) } done <- false } -func (c *TelnetClient) handle_iac(iac []byte) bool { +func (c *TelnetClient) handle_iac(iac []byte, cancel chan<- bool) bool { if len(iac) < 2 { return false // this shouldn't happen } @@ -355,8 +360,10 @@ func (c *TelnetClient) handle_iac(iac []byte) bool { case DO, DONT: iac[1] = WONT case IP: - // TODO: cancel running command (if any) - rhdl.Printf("canceling running process - is not yet implemented!") + select { + case cancel <- true: + default: // process got canceled already + } return false default: rhdl.Printf("ignoring unimplemented telnet command: %X", iac[1]) @@ -446,6 +453,7 @@ func (c *TelnetClient) handle() { go c.recv(in) done := make(chan bool) + cancel := make(chan bool, 1) c.write_string(prompt) for { select { @@ -456,9 +464,9 @@ func (c *TelnetClient) handle() { if len(cmd) > 0 { switch cmd[0] { case IAC: - c.handle_iac([]byte(cmd)) + c.handle_iac([]byte(cmd), cancel) default: - go c.handle_cmd(cmd, done) + go c.handle_cmd(cmd, done, cancel) } } else { c.write_string(prompt) |