main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "git.lattuga.net/boyska/direttoforo.git/liquidsoap"
  6. "os"
  7. "os/signal"
  8. "time"
  9. )
  10. func outUI(output chan liquidsoap.Output) {
  11. for msg := range output {
  12. if msg.Level <= 2 {
  13. fmt.Println(msg)
  14. }
  15. }
  16. }
  17. func main() {
  18. liqfile := flag.String("liq", "foo.liq", "Path to liquidsoap script to run")
  19. flag.Parse()
  20. killLs := make(chan struct{}) // when it is closed, liquidsoap will die
  21. killed := make(chan os.Signal, 1)
  22. signal.Notify(killed, os.Interrupt) // ctrl-c
  23. output, exit, err := liquidsoap.RunLiquidsoap(*liqfile, killLs)
  24. if err != nil {
  25. fmt.Fprintln(os.Stderr, "Error spawning liquidsoap", err)
  26. os.Exit(1)
  27. }
  28. go outUI(output)
  29. go func() {
  30. tick := time.Tick(3 * time.Second)
  31. for {
  32. <-tick
  33. t, err := liquidsoap.NewTelnet("localhost", 1234)
  34. if err != nil {
  35. fmt.Println("telnet connection errored", err)
  36. continue
  37. }
  38. t.Conn.SetDeadline(time.Now().Add(3 * time.Second))
  39. out, err := t.Outputs()
  40. if err != nil {
  41. fmt.Println("telnet cmd errored", err)
  42. continue
  43. }
  44. t.Close()
  45. fmt.Println("list=", out)
  46. }
  47. }()
  48. for {
  49. select {
  50. case how := <-exit: // liquidsoap exits
  51. if !how.Success() {
  52. fmt.Fprintln(os.Stderr, "liquidsoap terminated,", how.Err)
  53. os.Exit(1)
  54. }
  55. os.Exit(0)
  56. case <-killed: // we receive a SIGINT: ask liquidsoap to die is enough
  57. close(killLs)
  58. fmt.Println("Closed by user interaction, waiting for liquidsoap to exit")
  59. // TODO: schedule a more aggressive SIGKILL if liquidsoap doesn't exit soon
  60. }
  61. }
  62. }