listen.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
  2. // Use of this source code is governed by the MIT-license that can be
  3. // found in the LICENSE file.
  4. package main
  5. import (
  6. "log"
  7. "net"
  8. "strings"
  9. "time"
  10. "github.com/la5nta/wl2k-go/transport/ax25"
  11. "github.com/la5nta/wl2k-go/transport/telnet"
  12. )
  13. func Unlisten(param string) {
  14. methods := strings.FieldsFunc(param, SplitFunc)
  15. for _, method := range methods {
  16. ok, err := listenHub.Disable(method)
  17. if err != nil {
  18. log.Printf("Unable to close %s listener: %s", method, err)
  19. } else if !ok {
  20. log.Printf("No active %s listener, ignoring.\n", method)
  21. }
  22. }
  23. }
  24. func Listen(listenStr string) {
  25. methods := strings.FieldsFunc(listenStr, SplitFunc)
  26. for _, method := range methods {
  27. switch strings.ToLower(method) {
  28. case MethodWinmor:
  29. listenHub.Enable(WINMORListener{})
  30. case MethodArdop:
  31. listenHub.Enable(ARDOPListener{})
  32. case MethodTelnet:
  33. listenHub.Enable(TelnetListener{})
  34. case MethodAX25:
  35. listenHub.Enable(&AX25Listener{})
  36. case MethodSerialTNC:
  37. log.Printf("%s listen not implemented, ignoring.", method)
  38. default:
  39. log.Printf("'%s' is not a valid listen method", method)
  40. return
  41. }
  42. }
  43. log.Printf("Listening for incoming traffic on %s...", listenStr)
  44. }
  45. type AX25Listener struct{ stopBeacon chan<- struct{} }
  46. func (l *AX25Listener) Init() (net.Listener, error) {
  47. return ax25.ListenAX25(config.AX25.Port, fOptions.MyCall)
  48. }
  49. func (l *AX25Listener) BeaconStart() error {
  50. if config.AX25.Beacon.Every > 0 {
  51. l.stopBeacon = l.beaconLoop(time.Duration(config.AX25.Beacon.Every) * time.Second)
  52. }
  53. return nil
  54. }
  55. func (l *AX25Listener) BeaconStop() {
  56. select {
  57. case l.stopBeacon <- struct{}{}:
  58. default:
  59. }
  60. }
  61. func (l *AX25Listener) beaconLoop(dur time.Duration) chan<- struct{} {
  62. stop := make(chan struct{}, 1)
  63. go func() {
  64. b, err := ax25.NewAX25Beacon(config.AX25.Port, fOptions.MyCall, config.AX25.Beacon.Destination, config.AX25.Beacon.Message)
  65. if err != nil {
  66. log.Printf("Unable to activate beacon: %s", err)
  67. return
  68. }
  69. t := time.Tick(dur)
  70. for {
  71. select {
  72. case <-t:
  73. if err := b.Now(); err != nil {
  74. log.Printf("%s beacon failed: %s", l.Name(), err)
  75. return
  76. }
  77. case <-stop:
  78. return
  79. }
  80. }
  81. }()
  82. return stop
  83. }
  84. func (l *AX25Listener) CurrentFreq() (Frequency, bool) { return 0, false }
  85. func (l *AX25Listener) Name() string { return MethodAX25 }
  86. type ARDOPListener struct{}
  87. func (l ARDOPListener) Name() string { return MethodArdop }
  88. func (l ARDOPListener) Init() (net.Listener, error) {
  89. if err := initArdopTNC(); err != nil {
  90. return nil, err
  91. }
  92. ln, err := adTNC.Listen()
  93. if err != nil {
  94. return nil, err
  95. }
  96. return ln, err
  97. }
  98. func (l ARDOPListener) CurrentFreq() (Frequency, bool) {
  99. if rig, ok := rigs[config.Ardop.Rig]; ok {
  100. f, _ := rig.GetFreq()
  101. return Frequency(f), ok
  102. }
  103. return 0, false
  104. }
  105. func (l ARDOPListener) BeaconStart() error {
  106. return adTNC.BeaconEvery(time.Duration(config.Ardop.BeaconInterval) * time.Second)
  107. }
  108. func (l ARDOPListener) BeaconStop() { adTNC.BeaconEvery(0) }
  109. type WINMORListener struct{}
  110. func (l WINMORListener) Name() string { return MethodWinmor }
  111. func (l WINMORListener) Init() (net.Listener, error) {
  112. if err := initWinmorTNC(); err != nil {
  113. return nil, err
  114. }
  115. return wmTNC.Listen(config.Winmor.InboundBandwidth)
  116. }
  117. func (l WINMORListener) CurrentFreq() (Frequency, bool) {
  118. if rig, ok := rigs[config.Winmor.Rig]; ok {
  119. f, _ := rig.GetFreq()
  120. return Frequency(f), ok
  121. }
  122. return 0, false
  123. }
  124. type TelnetListener struct{}
  125. func (l TelnetListener) Name() string { return MethodTelnet }
  126. func (l TelnetListener) Init() (net.Listener, error) { return telnet.Listen(config.Telnet.ListenAddr) }
  127. func (l TelnetListener) CurrentFreq() (Frequency, bool) { return 0, false }