123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package main
- import (
- "flag"
- "fmt"
- "git.lattuga.net/boyska/direttoforo.git/liquidsoap"
- "os"
- "os/signal"
- "time"
- )
- func outUI(output chan liquidsoap.Output) {
- for msg := range output {
- if msg.Level <= 2 {
- fmt.Println(msg)
- }
- }
- }
- func main() {
- liqfile := flag.String("liq", "foo.liq", "Path to liquidsoap script to run")
- flag.Parse()
- killLs := make(chan struct{}) // when it is closed, liquidsoap will die
- killed := make(chan os.Signal, 1)
- signal.Notify(killed, os.Interrupt) // ctrl-c
- output, exit, err := liquidsoap.RunLiquidsoap(*liqfile, killLs)
- if err != nil {
- fmt.Fprintln(os.Stderr, "Error spawning liquidsoap", err)
- os.Exit(1)
- }
- go outUI(output)
- go func() {
- tick := time.Tick(3 * time.Second)
- for {
- <-tick
- t, err := liquidsoap.NewTelnet("localhost", 1234)
- if err != nil {
- fmt.Println("telnet connection errored", err)
- continue
- }
- t.Conn.SetDeadline(time.Now().Add(3 * time.Second))
- out, err := t.Outputs()
- if err != nil {
- fmt.Println("telnet cmd errored", err)
- continue
- }
- t.Close()
- fmt.Println("list=", out)
- }
- }()
- for {
- select {
- case how := <-exit: // liquidsoap exits
- if !how.Success() {
- fmt.Fprintln(os.Stderr, "liquidsoap terminated,", how.Err)
- os.Exit(1)
- }
- os.Exit(0)
- case <-killed: // we receive a SIGINT: ask liquidsoap to die is enough
- close(killLs)
- fmt.Println("Closed by user interaction, waiting for liquidsoap to exit")
- // TODO: schedule a more aggressive SIGKILL if liquidsoap doesn't exit soon
- }
- }
- }
|