Browse Source

Mutex was added

Denis V. Dedkov 9 năm trước cách đây
mục cha
commit
bbd9eb5c77
1 tập tin đã thay đổi với 8 bổ sung3 xóa
  1. 8 3
      noolite-cli.go

+ 8 - 3
noolite-cli.go

@@ -4,13 +4,14 @@ import (
 	"errors"
 	"flag"
 	"fmt"
+	"sync"
 	"github.com/dedkovd/noolite"
 	"net/http"
 	"strings"
 	"strconv"
 )
 
-func sendCommand(command string, channel, value, r, g, b int) error {
+func sendCommand(mutex *sync.Mutex, command string, channel, value, r, g, b int) error {
 	if channel == -1 {
 		return errors.New("Channel was not set")
 	}
@@ -19,6 +20,8 @@ func sendCommand(command string, channel, value, r, g, b int) error {
 		return errors.New("Command was not set")
 	}
 
+	mutex.Lock()
+	defer mutex.Unlock()
 	n, err := noolite.DefaultNooliteAdapter()
 
 	if err != nil {
@@ -94,8 +97,10 @@ func main() {
 
 	flag.Parse()
 
+	mutex := &sync.Mutex{}
+
 	if *http_port < 0 {
-		err := sendCommand(*command, *channel, *value, *red, *green, *blue)
+		err := sendCommand(mutex, *command, *channel, *value, *red, *green, *blue)
 
 		if err != nil {
 			panic(err)
@@ -104,7 +109,7 @@ func main() {
 		http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
 			command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
 
-			err := sendCommand(command, channel, value, red, green, blue)
+			err := sendCommand(mutex, command, channel, value, red, green, blue)
 
 			if err != nil {
 				fmt.Fprintf(w, "{\"error\": %q}", err)