Fixed issue where the echoed request would be returned instead of the actual request result
This commit is contained in:
parent
563dc3cc67
commit
1a61b94a11
1 changed files with 21 additions and 6 deletions
|
@ -21,6 +21,10 @@ var f *os.File;
|
||||||
var fLock sync.RWMutex;
|
var fLock sync.RWMutex;
|
||||||
var reader *bufio.Scanner;
|
var reader *bufio.Scanner;
|
||||||
|
|
||||||
|
// This is here to ignore lines written to STDIN echoed back through STDOUT
|
||||||
|
var ignoreEcho map[string]bool = make(map[string]bool);
|
||||||
|
var ignoreEchoMutex sync.RWMutex;
|
||||||
|
|
||||||
func SetupCMD(binaryLocation string) error {
|
func SetupCMD(binaryLocation string) error {
|
||||||
// Avoid double set-up
|
// Avoid double set-up
|
||||||
if cmdStarted {return errors.New("cmd already started")};
|
if cmdStarted {return errors.New("cmd already started")};
|
||||||
|
@ -64,6 +68,13 @@ func readCMD() {
|
||||||
// Read the line
|
// Read the line
|
||||||
line := reader.Text();
|
line := reader.Text();
|
||||||
|
|
||||||
|
// Check for echo
|
||||||
|
ignoreEchoMutex.Lock();
|
||||||
|
_, exists := ignoreEcho[line];
|
||||||
|
if exists {delete(ignoreEcho, line)}
|
||||||
|
ignoreEchoMutex.Unlock();
|
||||||
|
if exists {continue}
|
||||||
|
|
||||||
// Unmarshal the JSON
|
// Unmarshal the JSON
|
||||||
var unmarshaledJSON any;
|
var unmarshaledJSON any;
|
||||||
if err := json.Unmarshal([]byte(line), &unmarshaledJSON); err != nil {continue}
|
if err := json.Unmarshal([]byte(line), &unmarshaledJSON); err != nil {continue}
|
||||||
|
@ -74,22 +85,26 @@ func readCMD() {
|
||||||
|
|
||||||
// Get method
|
// Get method
|
||||||
method, ok := unmarshaledJSONMap["method"];
|
method, ok := unmarshaledJSONMap["method"];
|
||||||
if !ok {continue}
|
if ok && method == "receive" {
|
||||||
|
|
||||||
// Redirect to handlers based off method
|
|
||||||
if method == "receive" {
|
|
||||||
handleIncoming(line, unmarshaledJSONMap);
|
handleIncoming(line, unmarshaledJSONMap);
|
||||||
} else {
|
continue;
|
||||||
handleResponse(line, unmarshaledJSONMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleResponse(line, unmarshaledJSONMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write a line into the subprocess */
|
/* Write a line into the subprocess */
|
||||||
func writeCMD(line string) (ok bool) {
|
func writeCMD(line string) (ok bool) {
|
||||||
|
// Write into ignoreEcho map so reader can skip the echoed line
|
||||||
|
ignoreEchoMutex.Lock();
|
||||||
|
ignoreEcho[line] = true;
|
||||||
|
ignoreEchoMutex.Unlock();
|
||||||
|
|
||||||
fLock.Lock();
|
fLock.Lock();
|
||||||
if line[len(line)-1] != '\n' {line += "\n"}
|
if line[len(line)-1] != '\n' {line += "\n"}
|
||||||
f.WriteString(line);
|
f.WriteString(line);
|
||||||
fLock.Unlock();
|
fLock.Unlock();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue