Add command line program and improve logging
This commit is contained in:
parent
a82b56b1bf
commit
06e98d9998
85
cmd/openttd_multitool.go
Normal file
85
cmd/openttd_multitool.go
Normal file
@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/tardisx/openttd-admin/pkg"
|
||||
"flag"
|
||||
"strings"
|
||||
// "fmt"
|
||||
)
|
||||
|
||||
|
||||
// ./openttd_multitool --monthly "say \"hi it is a new month\"" --daily "say \"wow a new day %D"
|
||||
|
||||
const currentVersion = "0.01"
|
||||
|
||||
type dailyFlags []string
|
||||
type monthlyFlags []string
|
||||
type yearlyFlags []string
|
||||
|
||||
|
||||
|
||||
func (i *dailyFlags) String() string {
|
||||
// change this, this is just can example to satisfy the interface
|
||||
return "my string representation"
|
||||
}
|
||||
|
||||
func (i *dailyFlags) Set(value string) error {
|
||||
*i = append(*i, strings.TrimSpace(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *monthlyFlags) String() string {
|
||||
// change this, this is just can example to satisfy the interface
|
||||
return "my string representation"
|
||||
}
|
||||
|
||||
func (i *monthlyFlags) Set(value string) error {
|
||||
*i = append(*i, strings.TrimSpace(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *yearlyFlags) String() string {
|
||||
// change this, this is just can example to satisfy the interface
|
||||
return "my string representation"
|
||||
}
|
||||
|
||||
func (i *yearlyFlags) Set(value string) error {
|
||||
*i = append(*i, strings.TrimSpace(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var daily dailyFlags
|
||||
var monthly monthlyFlags
|
||||
var yearly yearlyFlags
|
||||
|
||||
flag.Var(&daily, "daily", "An RCON command to run daily - may be repeated")
|
||||
flag.Var(&monthly, "monthly", "An RCON command to run monthly - may be repeated")
|
||||
flag.Var(&yearly, "yearly", "An RCON command to run yearly - may be repeated")
|
||||
|
||||
var hostname string
|
||||
var password string
|
||||
var port int
|
||||
flag.StringVar(&hostname, "hostname", "localhost", "The hostname (or IP address) of the OpenTTD server to connect to")
|
||||
flag.StringVar(&password, "password", "", "The password for the admin interface ('admin_password' in openttd.cfg)")
|
||||
flag.IntVar(&port, "port", 3977, "The port number of the admin interface (default is 3977)")
|
||||
flag.Parse()
|
||||
|
||||
server := admin.OpenTTDServer{}
|
||||
|
||||
for _, value := range daily {
|
||||
server.RegisterDateChange("daily", value)
|
||||
}
|
||||
|
||||
for _, value := range monthly {
|
||||
server.RegisterDateChange("monthly", value)
|
||||
}
|
||||
|
||||
for _, value := range yearly {
|
||||
server.RegisterDateChange("yearly", value)
|
||||
}
|
||||
|
||||
// this blocks forever
|
||||
server.Connect(hostname, port, password, "openttd-multitool", currentVersion)
|
||||
}
|
26
pkg/admin.go
26
pkg/admin.go
@ -12,6 +12,7 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
"log"
|
||||
)
|
||||
|
||||
// OpenTTDServer - an object representing the server connection
|
||||
@ -93,21 +94,23 @@ func (server *OpenTTDServer) Connect(host string, port int, password string, bot
|
||||
for {
|
||||
|
||||
// fmt.Printf("array: %v (%T) %d\n", toSend, toSend, size)
|
||||
log.Println("connecting...")
|
||||
connectString := fmt.Sprintf("%s:%d", host, port)
|
||||
conn, err := net.Dial("tcp", connectString)
|
||||
if err != nil {
|
||||
fmt.Printf("%v\n", err)
|
||||
log.Printf("error connecting: %v\n", err)
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
//panic(err)
|
||||
}
|
||||
|
||||
log.Println("connected")
|
||||
|
||||
go server.listenSocket()
|
||||
|
||||
server.connected = make(chan bool)
|
||||
server.disconnected = make(chan bool)
|
||||
|
||||
fmt.Printf("saying we are connected\n")
|
||||
server.connection = conn
|
||||
server.connected <- true
|
||||
|
||||
@ -223,10 +226,9 @@ func (server *OpenTTDServer) sendSocket(protocol int, data []byte) {
|
||||
|
||||
func (server *OpenTTDServer) listenSocket() {
|
||||
|
||||
fmt.Println("waiting for connection...")
|
||||
// fmt.Println("waiting for connection...")
|
||||
// fmt.Printf("Listening to socket...\n")
|
||||
<-server.connected
|
||||
fmt.Println("connected indication")
|
||||
|
||||
var chunk []byte
|
||||
|
||||
@ -239,14 +241,14 @@ SocketLoop:
|
||||
if err != nil {
|
||||
if cErr, ok := err.(*net.OpError); ok {
|
||||
if cErr.Err.Error() == "read: connection reset by peer" {
|
||||
fmt.Println("Connection reset by peer - check the openttd log for details")
|
||||
log.Println("Connection reset by peer - check the openttd log for details")
|
||||
server.connection = nil
|
||||
server.disconnected <- true
|
||||
return
|
||||
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Error occurred on socket: ", err)
|
||||
log.Println("Error occurred on socket: ", err)
|
||||
server.connection = nil
|
||||
server.disconnected <- true
|
||||
return
|
||||
@ -285,11 +287,11 @@ SocketLoop:
|
||||
if packetType == adminPacketServerPROTOCOL {
|
||||
// fmt.Print(" - Got a adminPacketServerPROTOCOL packet\n")
|
||||
} else if packetType == adminPacketServerWELCOME {
|
||||
// fmt.Print(" - Got a adminPacketServerWELCOME packet\n")
|
||||
log.Println("received welcome packet")
|
||||
server.serverName = extractString(packetData[0:])
|
||||
// fmt.Printf(" * server name: %s\n", serverName)
|
||||
} else if packetType == adminPacketServerSHUTDOWN {
|
||||
fmt.Print("* Got a adminPacketServerSHUTDOWN packet - will try to reconnect\n")
|
||||
log.Println("server shutting down - will try to reconnect")
|
||||
server.connection = nil
|
||||
server.disconnected <- true
|
||||
return
|
||||
@ -310,16 +312,16 @@ SocketLoop:
|
||||
chatClientID := binary.LittleEndian.Uint32(packetData[2:6])
|
||||
chatMsg := extractString(packetData[6:])
|
||||
chatData := binary.LittleEndian.Uint64(packetData[len(packetData)-8:])
|
||||
fmt.Printf("action %v desttype %v, client id %v msg %v data %v\n", chatAction, chatDestType, chatClientID, string(chatMsg), chatData)
|
||||
log.Printf("chat message: action %v desttype %v, client id %v msg %v data %v\n", chatAction, chatDestType, chatClientID, string(chatMsg), chatData)
|
||||
} else if packetType == adminPacketServerRCON {
|
||||
colour := binary.LittleEndian.Uint16(packetData[0:2])
|
||||
string := extractString(packetData[2:])
|
||||
fmt.Printf("rcon: colour %v : %s\n", colour, string)
|
||||
log.Printf("rcon: colour %v : %s\n", colour, string)
|
||||
} else if packetType == adminPacketServerRCON_END {
|
||||
string := extractString(packetData[0:])
|
||||
fmt.Printf("rcon end : %s\n", string)
|
||||
log.Printf("rcon end : %s\n", string)
|
||||
} else {
|
||||
fmt.Printf("* Unknown packet received from server: %v [%v]\n", string(packetData), packetData)
|
||||
log.Printf("unknown packet received from server: %v [%v]\n", string(packetData), packetData)
|
||||
}
|
||||
|
||||
// fmt.Printf("removing the chunk we have processed\n")
|
||||
|
Loading…
x
Reference in New Issue
Block a user