node update sync wip
This commit is contained in:
parent
30aa3d0238
commit
8f913eca0d
5 changed files with 189 additions and 45 deletions
116
node.go
116
node.go
|
@ -1,21 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
connCode = "secretcode123"
|
||||
testMsg = "This is a test message"
|
||||
var (
|
||||
connCode string
|
||||
peers []string
|
||||
connCodeMutex sync.Mutex
|
||||
)
|
||||
|
||||
var connCodeMutex sync.Mutex
|
||||
var peers = []string{"localhost:50002", "localhost:5000"} // Example peer addresses
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func loadNodeConfig() {
|
||||
config := loadConfig()
|
||||
connCode = config.ConnectionCode
|
||||
peers = config.Peers
|
||||
}
|
||||
|
||||
func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
|
@ -33,55 +44,78 @@ func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
receivedCode := strings.TrimSpace(string(body))
|
||||
var msg Message
|
||||
if err := json.Unmarshal(body, &msg); err != nil {
|
||||
http.Error(w, "Error parsing JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if receivedCode != connCode {
|
||||
if msg.ID != connCode {
|
||||
http.Error(w, "Authentication failed", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Authentication successful")
|
||||
fmt.Fprintln(w, testMsg)
|
||||
interpretMessage(msg)
|
||||
fmt.Fprintln(w, "Message received")
|
||||
}
|
||||
|
||||
func interpretMessage(msg Message) {
|
||||
switch msg.Type {
|
||||
case "test":
|
||||
fmt.Println("Received test message:", msg.Content)
|
||||
case "get-version":
|
||||
fmt.Println("Received get-version message")
|
||||
// Handle get-version logic here
|
||||
case "update":
|
||||
fmt.Println("Received update message:", msg.Content)
|
||||
// Handle update logic here
|
||||
go update()
|
||||
default:
|
||||
fmt.Println("Received unknown message type:", msg.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func sendMessage(address, id, msgType, content string) error {
|
||||
msg := Message{
|
||||
ID: id,
|
||||
Type: msgType,
|
||||
Content: content,
|
||||
}
|
||||
msgBytes, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/node", address), bytes.NewBuffer(msgBytes))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
return fmt.Errorf("failed to send message: %s", body)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func startNodeClient(addresses []string) {
|
||||
for _, address := range addresses {
|
||||
go func(addr string) {
|
||||
for {
|
||||
url := fmt.Sprintf("http://%s/node", addr)
|
||||
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(connCode))
|
||||
err := sendMessage(addr, connCode, "test", "This is a test message")
|
||||
if err != nil {
|
||||
fmt.Println("Error creating request:", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
fmt.Println("Error sending test message to", addr, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Error connecting to", addr, ":", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Println("Authentication failed:", resp.Status)
|
||||
resp.Body.Close()
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading response:", err)
|
||||
resp.Body.Close()
|
||||
time.Sleep(5 * time.Second)
|
||||
continue
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
testMsg := strings.TrimSpace(string(body))
|
||||
fmt.Println("Received test message from", addr, ":", testMsg)
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}(address)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue