Skip to content

Commit da186de

Browse files
matteosuppofacchinm
authored andcommitted
Kill upload
1 parent 06f05ae commit da186de

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

conn.go

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func (l PLogger) Debug(args ...interface{}) {
171171
// Info always send messages
172172
func (l PLogger) Info(args ...interface{}) {
173173
output := fmt.Sprint(args...)
174+
log.Println(output)
174175
send(map[string]string{"ProgrammerStatus": "Busy", "Msg": output})
175176
}
176177

hub.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ func checkCmd(m []byte) {
171171

172172
} else if strings.HasPrefix(sl, "killprogrammer") {
173173
// kill the running process (assumes singleton for now)
174-
go programmer.Kill()
174+
go func() {
175+
programmer.Kill()
176+
h.broadcastSys <- []byte("{\"ProgrammerStatus\": \"Killed\"}")
177+
log.Println("{\"ProgrammerStatus\": \"Killed\"}")
178+
}()
175179

176180
} else if strings.HasPrefix(sl, "sendjsonraw") {
177181
// will catch sendjsonraw

programmer/programmer.go

+32-11
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func Resolve(port, board, file, commandline string, extra Extra, t locater) (str
7575

7676
// Network performs a network upload
7777
func Network(port, board, file, commandline string, auth Auth, l logger) error {
78+
Busy = true
79+
7880
// Defaults
7981
if auth.Username == "" {
8082
auth.Username = "root"
@@ -89,14 +91,16 @@ func Network(port, board, file, commandline string, auth Auth, l logger) error {
8991
// try with ssh
9092
err = ssh(port, file, commandline, auth, l)
9193
}
94+
95+
Busy = false
9296
return err
9397
}
9498

95-
// Kill stops any upload process as soon as possible
96-
func Kill() {}
97-
9899
// Serial performs a serial upload
99100
func Serial(port, commandline string, extra Extra, l logger) error {
101+
Busy = true
102+
defer func() { Busy = false }()
103+
100104
// some boards needs to be resetted
101105
if extra.Use1200bpsTouch {
102106
var err error
@@ -114,6 +118,14 @@ func Serial(port, commandline string, extra Extra, l logger) error {
114118
return program(z[0], z[1:], l)
115119
}
116120

121+
// Kill stops any upload process as soon as possible
122+
func Kill() {
123+
log.Println(cmd)
124+
if cmd != nil && cmd.Process.Pid > 0 {
125+
cmd.Process.Kill()
126+
}
127+
}
128+
117129
// reset opens the port at 1200bps. It returns the new port name (which could change
118130
// sometimes) and an error (usually because the port listing failed)
119131
func reset(port string, wait bool, l logger) (string, error) {
@@ -204,9 +216,14 @@ func waitReset(beforeReset []string, l logger) string {
204216
return port
205217
}
206218

219+
// cmd is the upload command
220+
var cmd *exec.Cmd
221+
207222
// program spawns the given binary with the given args, logging the sdtout and stderr
208223
// through the logger
209224
func program(binary string, args []string, l logger) error {
225+
defer func() { cmd = nil }()
226+
210227
// remove quotes form binary command and args
211228
binary = strings.Replace(binary, "\"", "", -1)
212229

@@ -220,23 +237,23 @@ func program(binary string, args []string, l logger) error {
220237
extension = ".exe"
221238
}
222239

223-
oscmd := exec.Command(binary, args...)
240+
cmd = exec.Command(binary, args...)
224241

225-
utilities.TellCommandNotToSpawnShell(oscmd)
242+
utilities.TellCommandNotToSpawnShell(cmd)
226243

227-
stdout, err := oscmd.StdoutPipe()
244+
stdout, err := cmd.StdoutPipe()
228245
if err != nil {
229246
return errors.Wrapf(err, "Retrieve output")
230247
}
231248

232-
stderr, err := oscmd.StderrPipe()
249+
stderr, err := cmd.StderrPipe()
233250
if err != nil {
234251
return errors.Wrapf(err, "Retrieve output")
235252
}
236253

237254
info(l, "Flashing with command:"+binary+extension+" "+strings.Join(args, " "))
238255

239-
err = oscmd.Start()
256+
err = cmd.Start()
240257

241258
stdoutCopy := bufio.NewScanner(stdout)
242259
stderrCopy := bufio.NewScanner(stderr)
@@ -256,9 +273,11 @@ func program(binary string, args []string, l logger) error {
256273
}
257274
}()
258275

259-
err = oscmd.Wait()
260-
261-
return errors.Wrapf(err, "Executing command")
276+
err = cmd.Wait()
277+
if err != nil {
278+
return errors.Wrapf(err, "Executing command")
279+
}
280+
return nil
262281
}
263282

264283
func form(port, board, file string, auth Auth, l logger) error {
@@ -280,6 +299,7 @@ func form(port, board, file string, auth Auth, l logger) error {
280299
if _, err = io.Copy(fw, f); err != nil {
281300
return errors.Wrapf(err, "Copy form file")
282301
}
302+
283303
// Add the other fields
284304
board = strings.Replace(board, ":", "_", -1)
285305
if fw, err = w.CreateFormField("board"); err != nil {
@@ -288,6 +308,7 @@ func form(port, board, file string, auth Auth, l logger) error {
288308
if _, err = fw.Write([]byte(board)); err != nil {
289309
return errors.Wrapf(err, "")
290310
}
311+
291312
// Don't forget to close the multipart writer.
292313
// If you don't close it, your request will be missing the terminating boundary.
293314
w.Close()

0 commit comments

Comments
 (0)