-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
162 lines (139 loc) · 2.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"os"
"os/signal"
"runtime/debug"
"strings"
"syscall"
"time"
)
const waitBetweenRetries = time.Second
const retryCount = 5
const waitOnRetryFailure = 65 * time.Second
var gotErrChan = make(chan bool)
var quitChan = make(chan bool)
func getAboutStr() string {
var v string
bi, ok := debug.ReadBuildInfo()
if ok {
v = bi.Main.Version
} else {
v = "(devel)"
}
return "kappanhang " + v + " by Norbert Varga HA2NON and Akos Marton ES1AKOS https://github.com/nonoo/kappanhang"
}
func wait(d time.Duration, osSignal chan os.Signal) (shouldExit bool) {
for sec := d.Seconds(); sec > 0; sec-- {
log.Print("waiting ", sec, " seconds...")
select {
case <-time.After(time.Second):
case <-osSignal:
log.Print("sigterm received")
return true
case <-quitChan:
return true
}
}
return false
}
func runControlStream(osSignal chan os.Signal) (requireWait, shouldExit bool, exitCode int) {
// Depleting gotErrChan.
var finished bool
for !finished {
select {
case <-gotErrChan:
default:
finished = true
}
}
ctrl := &controlStream{}
if err := ctrl.init(); err != nil {
log.Error(err)
ctrl.deinit()
if strings.Contains(err.Error(), "invalid username/password") {
return false, true, 1
}
return
}
select {
// Need to wait before reinit because the IC-705 will disconnect our audio stream eventually if we relogin
// in a too short interval without a deauth...
case requireWait = <-gotErrChan:
ctrl.deinit()
return
case <-osSignal:
log.Print("sigterm received")
ctrl.deinit()
return false, true, 0
case <-quitChan:
ctrl.deinit()
return false, true, 0
}
}
func reportError(err error) {
if !strings.Contains(err.Error(), "use of closed network connection") {
log.ErrorC(log.GetCallerFileName(true), ": ", err)
}
requireWait := true
if strings.Contains(err.Error(), "got radio disconnected") {
requireWait = false
}
// Non-blocking notify.
select {
case gotErrChan <- requireWait:
default:
}
}
func main() {
parseArgs()
log.Init()
log.Print(getAboutStr())
osSignal := make(chan os.Signal, 1)
signal.Notify(osSignal, os.Interrupt, syscall.SIGTERM)
var retries int
var requireWait bool
var shouldExit bool
var exitCode int
exit:
for {
requireWait, shouldExit, exitCode = runControlStream(osSignal)
if shouldExit {
break
}
select {
case <-osSignal:
log.Print("sigterm received")
break exit
case <-quitChan:
break exit
default:
}
if requireWait {
if retries < retryCount {
retries++
shouldExit = wait(waitBetweenRetries, osSignal)
} else {
retries = 0
shouldExit = wait(waitOnRetryFailure, osSignal)
}
} else {
retries = 0
shouldExit = wait(time.Second, osSignal)
}
if shouldExit {
break
}
log.Print("restarting control stream...")
}
rigctld.deinit()
serialTCPSrv.deinit()
runCmdRunner.stop()
serialCmdRunner.stop()
audio.deinit()
serialPort.deinit()
if statusLog.isRealtimeInternal() {
keyboard.deinit()
}
log.Print("exiting")
os.Exit(exitCode)
}