diff --git a/server/Start.go b/server/Start.go index 42536be..bfe1ecd 100644 --- a/server/Start.go +++ b/server/Start.go @@ -11,8 +11,12 @@ func (server *Server) Start() { // Declare ourselves up and running to the console. log.Println("Started sudoku-funpark server") + // Run the agent manager + server.agentManager() + // Start the http server - http.HandleFunc("/json", server.jsonDumper) + http.HandleFunc("GET /json", server.jsonDumper) + http.HandleFunc("POST /addTask", server.addTask) // Start the websocket server http.HandleFunc("/ws", server.handleConnections) diff --git a/server/addTask.go b/server/addTask.go new file mode 100644 index 0000000..df5b3c4 --- /dev/null +++ b/server/addTask.go @@ -0,0 +1,61 @@ +package server + +import ( + "fmt" + "net/http" +) + +func (server *Server) addTask(w http.ResponseWriter, r *http.Request) { + var valid bool = true + + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + if err := r.ParseForm(); err != nil { + fmt.Fprintf(w, "ERROR: Failed ParseForm() err: %v", err) + return + } + + //fmt.Println(r.PostForm) + // Validate r.PostForm["rows"] + // Look at flags.validateRow() + // 1. Make sure the rows is 9 in length + if len(r.PostForm["rows"]) != 9 { + fmt.Fprintf(w, "ERROR: There aren't 9 rows") + return + } + + // 2. Validate the row + for _, value := range r.PostForm["rows"] { + if !server.validateRow(value) { + valid = false + } + } + + // Go/No-Go moment + if !valid { + w.Write([]byte("ERROR found")) + return + } + + // Add the task + var puzzle [9]string + puzzle[0] = r.PostForm["rows"][0] + puzzle[1] = r.PostForm["rows"][1] + puzzle[2] = r.PostForm["rows"][2] + puzzle[3] = r.PostForm["rows"][3] + puzzle[4] = r.PostForm["rows"][4] + puzzle[5] = r.PostForm["rows"][5] + puzzle[6] = r.PostForm["rows"][6] + puzzle[7] = r.PostForm["rows"][7] + puzzle[8] = r.PostForm["rows"][8] + + // Create task and chuck it in the server struct + task := Task{Puzzle: puzzle} + server.Tasks = append(server.Tasks, &task) + + // Calling it + w.Write([]byte("Ok")) + +} diff --git a/server/agentManager.go b/server/agentManager.go new file mode 100644 index 0000000..046e057 --- /dev/null +++ b/server/agentManager.go @@ -0,0 +1,23 @@ +package server + +import "time" + +func (server *Server) agentManager() { + // Start the great for-loop + for { + // Count the number of agents without a runnig task + + // Check if there are any new Tasks up for grabs + + // See if the task can be divided by the number of agents + + // If so, split + // if not, split-1 + // Try again + + // Give agents task, maybe even look for the ones with the most CPUs + + // 😴 + time.Sleep(10 * time.Second) + } +} diff --git a/server/agents.html b/server/agents.html index e75adba..f3454f0 100644 --- a/server/agents.html +++ b/server/agents.html @@ -59,5 +59,22 @@ // Fetch data every second setInterval(fetchData, 1000); +
+
+ + + + + + + + + + + + +
Row1:
Row2:
Row3:
Row4:
Row5:
Row6:
Row7:
Row8:
Row9:
+
+
\ No newline at end of file diff --git a/server/types.go b/server/types.go index f487942..c44da7d 100644 --- a/server/types.go +++ b/server/types.go @@ -26,7 +26,7 @@ type Server struct { ListenAddress string ListenPort int Agents map[string]*Agent - Tasks []Task + Tasks []*Task } var upgrader = websocket.Upgrader{ diff --git a/server/validChar.go b/server/validChar.go new file mode 100644 index 0000000..170e88a --- /dev/null +++ b/server/validChar.go @@ -0,0 +1,13 @@ +package server + +func (server *Server) validChar(char rune) (valid bool) { + decvals := [10]int{48, 49, 50, 51, 52, 53, 54, 55, 56, 57} + + for _, value := range decvals { + if char == rune(value) { + valid = true + } + } + + return +} diff --git a/server/validateRow.go b/server/validateRow.go new file mode 100644 index 0000000..9787be5 --- /dev/null +++ b/server/validateRow.go @@ -0,0 +1,40 @@ +package server + +func (server *Server) validateRow(row string) (valid bool) { + // Declarations baby! + valid = true + var found bool + var double bool + count := make(map[rune]int) + + // 1. Make sure row is 9 in length + if len(row) != 9 { + valid = false + } + + // 2. Ensure all digits are numbers + for _, value := range row { + found = server.validChar(value) + } + + if !found { + valid = false + } + + // 3. Ensure all digits (except zero) are only present once + for _, digits := range row { + count[digits] = count[digits] + 1 + } + + for key, value := range count { + if value > 1 && key != 48 { + double = true + } + } + + if double { + valid = false + } + + return +}