Added ability to add task; Started on an agentManager.

This commit is contained in:
Sacha Ligthert 2025-02-08 19:23:16 +01:00
parent 6c38363694
commit 8bd09a4983
7 changed files with 160 additions and 2 deletions

View File

@ -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)

61
server/addTask.go Normal file
View File

@ -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"))
}

23
server/agentManager.go Normal file
View File

@ -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)
}
}

View File

@ -59,5 +59,22 @@
// Fetch data every second
setInterval(fetchData, 1000);
</script>
<hr/>
<form action="http://localhost:8080/addTask" method="post">
<table border="0">
<tr><td>Row1</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row2</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row3</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row4</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row5</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row6</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row7</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row8</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td>Row9</td><td>:</td><td><input type="text" value="000000000" name="rows"></td></tr>
<tr><td colspan="3" align="right"><input type="submit"></td></tr>
</table>
</form>
<hr/>
</body>
</html>

View File

@ -26,7 +26,7 @@ type Server struct {
ListenAddress string
ListenPort int
Agents map[string]*Agent
Tasks []Task
Tasks []*Task
}
var upgrader = websocket.Upgrader{

13
server/validChar.go Normal file
View File

@ -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
}

40
server/validateRow.go Normal file
View File

@ -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
}