Compare commits
2 Commits
9b6a90fbce
...
trunk
Author | SHA1 | Date | |
---|---|---|---|
8bd09a4983 | |||
6c38363694 |
@ -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
61
server/addTask.go
Normal 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
23
server/agentManager.go
Normal 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)
|
||||
}
|
||||
}
|
80
server/agents.html
Normal file
80
server/agents.html
Normal file
@ -0,0 +1,80 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>JSON Data Display</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 20px;
|
||||
}
|
||||
#data-container {
|
||||
margin-top: 20px;
|
||||
}
|
||||
.data-item {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>JSON Data Display</h1>
|
||||
<div id="data-container">
|
||||
<!-- Data will be inserted here -->
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const url = 'http://localhost:8080/json'; // Replace with your actual URL
|
||||
const dataContainer = document.getElementById('data-container');
|
||||
|
||||
async function fetchData() {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
const data = await response.json();
|
||||
updateData(data);
|
||||
} catch (error) {
|
||||
console.error('There was a problem with the fetch operation:', error);
|
||||
}
|
||||
}
|
||||
|
||||
function updateData(data) {
|
||||
const agent = data.Agents.blackbox;
|
||||
dataContainer.innerHTML = `
|
||||
<div class="data-item"><strong>Name:</strong> ${agent.Name}</div>
|
||||
<div class="data-item"><strong>Registration Time:</strong> ${agent.Reg}</div>
|
||||
<div class="data-item"><strong>Cores:</strong> ${agent.Cores}</div>
|
||||
<div class="data-item"><strong>CPU Usage:</strong> ${agent.Cpu.join(', ')}</div>
|
||||
<div class="data-item"><strong>Max Memory:</strong> ${agent.Mem_max}</div>
|
||||
<div class="data-item"><strong>Memory Usage:</strong> ${agent.Mem_usg.join(', ')}</div>
|
||||
<div class="data-item"><strong>Task ID:</strong> ${agent.TaskId}</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// Fetch data initially
|
||||
fetchData();
|
||||
|
||||
// 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>
|
@ -13,6 +13,7 @@ func (server *Server) jsonDumper(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("This is my Website"))
|
||||
}
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(json)
|
||||
|
@ -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
13
server/validChar.go
Normal 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
40
server/validateRow.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user