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); +