2020-11-10 21:53:46 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2020-12-17 04:23:25 +00:00
|
|
|
"net"
|
2020-11-10 21:53:46 -05:00
|
|
|
)
|
|
|
|
|
2020-12-22 20:36:37 +00:00
|
|
|
func startctl() {
|
2021-01-14 19:51:42 +00:00
|
|
|
logInfo.Print("Starting ctl listener on 127.0.0.1:5555")
|
2020-11-24 20:36:47 -05:00
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:5555")
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Unable to start listener:", err)
|
2020-11-24 20:36:47 -05:00
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
commandClient := make(chan net.Conn)
|
|
|
|
|
|
|
|
go func(l net.Listener) {
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Error on accept", err)
|
2020-11-24 20:36:47 -05:00
|
|
|
}
|
|
|
|
commandClient <- c
|
|
|
|
}
|
|
|
|
}(l)
|
|
|
|
|
|
|
|
for {
|
2020-12-17 04:23:25 +00:00
|
|
|
c := <-commandClient // New client connection
|
2020-12-22 20:36:37 +00:00
|
|
|
go handleClient(c)
|
2020-11-24 20:36:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:36:37 +00:00
|
|
|
func handleClient(commandClient net.Conn) {
|
2021-01-30 14:30:49 +00:00
|
|
|
defer commandClient.Close()
|
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 {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Read error: ", err)
|
|
|
|
return
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
if n != 4 {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Did not read 4 bytes, failure.")
|
|
|
|
return
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
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 {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Unable to unmarshal")
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
if n != jsonsize {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Failed to read json size of ", n)
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(jsonbyte, &commandmap)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Unable to unmarshal")
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch commandmap.Type {
|
|
|
|
case "status":
|
|
|
|
responseback.Message = "Ok"
|
|
|
|
case "add":
|
2021-01-14 19:51:42 +00:00
|
|
|
logInfo.Print("Manually added instance: " + commandmap.Endpoint)
|
2020-11-17 18:28:59 -05:00
|
|
|
ri_mutex.Lock()
|
|
|
|
_, exists := runninginstances[commandmap.Endpoint]
|
|
|
|
if exists == true {
|
2021-01-14 19:51:42 +00:00
|
|
|
logInfo.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 {
|
2021-02-01 02:05:56 +00:00
|
|
|
responseback.Message = "Adding: " + commandmap.Endpoint
|
|
|
|
o := RunningInstance{}
|
|
|
|
o.recenturis = newfifo(5)
|
|
|
|
o.client = BuildClient(commandmap.Endpoint)
|
|
|
|
runninginstances[commandmap.Endpoint] = o
|
2020-12-22 20:36:37 +00:00
|
|
|
go StartInstance(commandmap.Endpoint)
|
2020-11-17 18:28:59 -05:00
|
|
|
}
|
|
|
|
ri_mutex.Unlock()
|
2020-11-10 21:53:46 -05:00
|
|
|
case "suspend":
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Suspend")
|
2020-11-10 21:53:46 -05:00
|
|
|
case "resume":
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Resume")
|
2020-11-10 21:53:46 -05:00
|
|
|
default:
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Something else")
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-01-14 19:51:42 +00:00
|
|
|
logErr.Fatal(err)
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
n = len(responsebytes)
|
|
|
|
binary.LittleEndian.PutUint32(sizebyte, uint32(n))
|
|
|
|
|
2020-11-13 20:13:13 -05:00
|
|
|
_, err = commandClient.Write(sizebyte)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Error on write:", err)
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
|
|
|
|
responsebyte, err := json.Marshal(responseback)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Error response back: ", err)
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
2020-11-13 20:13:13 -05:00
|
|
|
_, err = commandClient.Write(responsebyte)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Error on write:", err)
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|