musique/server/musique-bridge.go

84 lines
1.4 KiB
Go
Raw Permalink Normal View History

2022-12-05 22:34:40 +01:00
package main
import (
"C"
"fmt"
"log"
"musique/server/router"
"os"
"sort"
2022-12-05 22:34:40 +01:00
)
//export ServerInit
2022-12-16 11:38:05 +01:00
func ServerInit(inputNick string, inputPort int) {
2022-12-16 16:34:52 +01:00
logFile, err := os.OpenFile("musique.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o640)
if err != nil {
log.Fatalln(err)
}
log.SetOutput(logFile)
2022-12-16 11:38:05 +01:00
nick = inputNick
port = inputPort
r := router.Router{}
registerRoutes(&r)
2022-12-16 16:34:52 +01:00
_, err = r.Run(baseIP, uint16(port))
if err != nil {
log.Fatalln(err)
}
2022-12-09 16:05:25 +01:00
if err := registerRemotes(); err != nil {
log.Fatalln(err)
}
2022-12-16 16:34:52 +01:00
timesync()
}
//export SetTimeout
func SetTimeout(timeout int64) {
maxReactionTime = timeout
2022-12-05 22:34:40 +01:00
}
//export ServerBeginProtocol
func ServerBeginProtocol() {
2022-12-16 16:34:52 +01:00
self := notifyAll()
select {
case <-self:
case <-pinger:
}
}
//export Discover
func Discover() {
if len(remotes) == 0 {
if err := registerRemotes(); err != nil {
log.Println("discover:", err)
}
}
synchronizeHostsWithRemotes()
}
//export ListKnownRemotes
func ListKnownRemotes() {
type nickAddr struct {
nick, addr string
}
list := []nickAddr{}
for _, remote := range remotes {
2022-12-16 16:34:52 +01:00
list = append(list, nickAddr{remote.Nick, remote.Address})
}
2022-12-16 16:34:52 +01:00
sort.Slice(list, func(i, j int) bool {
if list[i].nick == list[j].nick {
return list[i].addr < list[j].addr
}
return list[i].nick < list[j].nick
})
for _, nickAddr := range list {
fmt.Printf("%s@%s\n", nickAddr.nick, nickAddr.addr)
2022-12-05 22:34:40 +01:00
}
os.Stdout.Sync()
2022-12-05 22:34:40 +01:00
}