2022-12-05 22:34:40 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"C"
|
|
|
|
"fmt"
|
2022-12-16 02:11:43 +01:00
|
|
|
"log"
|
|
|
|
"musique/server/router"
|
|
|
|
"os"
|
|
|
|
"sort"
|
2022-12-05 22:34:40 +01:00
|
|
|
)
|
|
|
|
|
2023-01-02 05:47:27 +01:00
|
|
|
const (
|
|
|
|
initialWaitingTime = 3
|
|
|
|
userRequestedWatingingTime = 5
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
r := router.Router{}
|
|
|
|
registerRoutes(&r)
|
2022-12-16 16:34:52 +01:00
|
|
|
_, err = r.Run(baseIP, uint16(port))
|
2022-12-16 02:11:43 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2022-12-09 16:05:25 +01:00
|
|
|
|
2023-01-02 05:47:27 +01:00
|
|
|
if err := registerRemotes(initialWaitingTime); err != nil {
|
2022-12-16 02:11:43 +01:00
|
|
|
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 {
|
2023-01-02 05:47:27 +01:00
|
|
|
if err := registerRemotes(userRequestedWatingingTime); err != nil {
|
2022-12-16 16:34:52 +01:00
|
|
|
log.Println("discover:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
synchronizeHostsWithRemotes()
|
2022-12-16 02:11:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//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 02:11:43 +01:00
|
|
|
}
|
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
sort.Slice(list, func(i, j int) bool {
|
2022-12-16 02:11:43 +01:00
|
|
|
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
|
|
|
}
|
2022-12-16 02:11:43 +01:00
|
|
|
os.Stdout.Sync()
|
2022-12-05 22:34:40 +01:00
|
|
|
}
|