2020-11-10 21:53:46 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"log"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-11-24 20:36:47 -05:00
|
|
|
func startctl(reportPostChan chan ReportPost) {
|
|
|
|
log.Print("Starting ctl listener on 127.0.0.1:5555")
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:5555")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to start listener:", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
commandClient := make(chan net.Conn)
|
|
|
|
|
|
|
|
go func(l net.Listener) {
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error on accept", err)
|
|
|
|
}
|
|
|
|
commandClient <- c
|
|
|
|
}
|
|
|
|
}(l)
|
|
|
|
|
|
|
|
for {
|
|
|
|
c := <-commandClient // New client connection
|
|
|
|
go handleClient(c, reportPostChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-17 18:28:59 -05:00
|
|
|
func handleClient(commandClient net.Conn, reportPostChan chan ReportPost) {
|
2020-11-10 21:53:46 -05:00
|
|
|
|
|
|
|
sizebyte := make([]byte, 4)
|
|
|
|
var commandmap CommandMap
|
|
|
|
var responseback ResponseBack
|
|
|
|
n, err := io.ReadFull(commandClient, sizebyte)
|
2020-11-13 20:13:13 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Read error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
if n != 4 {
|
|
|
|
fmt.Println("Did not read 4 bytes, failure.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
jsonsize := int(binary.LittleEndian.Uint32(sizebyte))
|
|
|
|
jsonbyte := make([]byte, jsonsize)
|
|
|
|
n, err = io.ReadFull(commandClient, jsonbyte)
|
2020-11-13 20:13:13 -05:00
|
|
|
if err != nil {
|
2020-11-24 20:36:47 -05:00
|
|
|
log.Fatal("Unable to unmarshal")
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
if n != jsonsize {
|
2020-11-24 20:36:47 -05:00
|
|
|
log.Fatal("Failed to read json size of ", n)
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(jsonbyte, &commandmap)
|
|
|
|
if err != nil {
|
2020-11-24 20:36:47 -05:00
|
|
|
log.Fatal("Unable to unmarshal")
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch commandmap.Type {
|
|
|
|
case "status":
|
|
|
|
responseback.Message = "Ok"
|
|
|
|
case "add":
|
|
|
|
log.Print("Manually added instance: " + commandmap.Endpoint)
|
2020-11-17 18:28:59 -05:00
|
|
|
ri_mutex.Lock()
|
|
|
|
_, exists := runninginstances[commandmap.Endpoint]
|
|
|
|
if exists == true {
|
|
|
|
log.Println("Already exists: " + commandmap.Endpoint)
|
2020-11-18 19:38:08 -05:00
|
|
|
responseback.Message = "Exists: " + commandmap.Endpoint
|
2020-11-17 18:28:59 -05:00
|
|
|
} else {
|
2020-11-18 19:38:08 -05:00
|
|
|
responseback.Message = "Added: " + commandmap.Endpoint
|
2020-11-17 18:28:59 -05:00
|
|
|
runninginstances[commandmap.Endpoint] = RunningInstance{}
|
2020-11-18 19:38:08 -05:00
|
|
|
go StartInstance(commandmap.Endpoint, reportPostChan)
|
2020-11-17 18:28:59 -05:00
|
|
|
}
|
|
|
|
ri_mutex.Unlock()
|
2020-11-10 21:53:46 -05:00
|
|
|
case "suspend":
|
|
|
|
fmt.Println("Suspend")
|
|
|
|
case "resume":
|
|
|
|
fmt.Println("Resume")
|
|
|
|
default:
|
|
|
|
fmt.Println("Something else")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
responseback.Type = "status"
|
2020-11-17 18:28:59 -05:00
|
|
|
responseback.RunningInstances = runninginstances
|
2020-11-10 21:53:46 -05:00
|
|
|
|
|
|
|
responsebytes, err := json.Marshal(responseback)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
n = len(responsebytes)
|
|
|
|
binary.LittleEndian.PutUint32(sizebyte, uint32(n))
|
|
|
|
|
2020-11-13 20:13:13 -05:00
|
|
|
_, err = commandClient.Write(sizebyte)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error on write:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
|
|
|
|
responsebyte, err := json.Marshal(responseback)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error response back: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-11-13 20:13:13 -05:00
|
|
|
_, err = commandClient.Write(responsebyte)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error on write:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
commandClient.Close()
|
|
|
|
}
|