mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
use Id function instead of accessing config directly
This commit is contained in:
@@ -43,7 +43,7 @@ func (hc *HTTPClient) Run() error {
|
||||
}
|
||||
|
||||
<-hc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", hc.config.Id)
|
||||
slog.Debug("router context done in module", "id", hc.Id())
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ func (hc *HTTPClient) Output(payload any) error {
|
||||
}
|
||||
|
||||
if hc.router != nil {
|
||||
hc.router.HandleInput(hc.config.Id, response)
|
||||
hc.router.HandleInput(hc.Id(), response)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -61,7 +61,7 @@ func (hs *HTTPServer) HandleDefault(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if hs.router != nil {
|
||||
routingErrors := hs.router.HandleInput(hs.config.Id, r)
|
||||
routingErrors := hs.router.HandleInput(hs.Id(), r)
|
||||
if routingErrors != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
response.Status = "error"
|
||||
@@ -89,12 +89,12 @@ func (hs *HTTPServer) Run() error {
|
||||
|
||||
go func() {
|
||||
<-hs.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", hs.config.Id)
|
||||
slog.Debug("router context done in module", "id", hs.Id())
|
||||
httpServer.Close()
|
||||
}()
|
||||
|
||||
err := httpServer.ListenAndServe()
|
||||
slog.Debug("net.http.server closed", "id", hs.config.Id)
|
||||
slog.Debug("net.http.server closed", "id", hs.Id())
|
||||
// TODO(jwetzell): handle server closed error differently
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -56,11 +56,11 @@ func (i *Interval) Run() error {
|
||||
for {
|
||||
select {
|
||||
case <-i.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", i.config.Id)
|
||||
slog.Debug("router context done in module", "id", i.Id())
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if i.router != nil {
|
||||
i.router.HandleInput(i.config.Id, time.Now())
|
||||
i.router.HandleInput(i.Id(), time.Now())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ func (mc *MIDIClient) Run() error {
|
||||
|
||||
stop, err := midi.ListenTo(in, func(msg midi.Message, timestampms int32) {
|
||||
if mc.router != nil {
|
||||
// TODO(jwetzell): unpack MIDI messsage into something more useful?
|
||||
mc.router.HandleInput(mc.Id(), msg)
|
||||
}
|
||||
}, midi.UseSysEx())
|
||||
@@ -99,7 +100,7 @@ func (mc *MIDIClient) Run() error {
|
||||
mc.SendFunc = send
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ func (mc *MQTTClient) Run() error {
|
||||
|
||||
opts.OnConnect = func(c mqtt.Client) {
|
||||
token := mc.client.Subscribe(mc.Topic, 1, func(c mqtt.Client, m mqtt.Message) {
|
||||
mc.router.HandleInput(mc.config.Id, m)
|
||||
mc.router.HandleInput(mc.Id(), m)
|
||||
})
|
||||
token.Wait()
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func (mc *MQTTClient) Run() error {
|
||||
}
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ func (nc *NATSClient) Run() error {
|
||||
|
||||
sub, err := nc.client.Subscribe(nc.Subject, func(msg *nats.Msg) {
|
||||
if nc.router != nil {
|
||||
nc.router.HandleInput(nc.config.Id, msg)
|
||||
nc.router.HandleInput(nc.Id(), msg)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -87,7 +87,7 @@ func (nc *NATSClient) Run() error {
|
||||
defer sub.Unsubscribe()
|
||||
|
||||
<-nc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", nc.config.Id)
|
||||
slog.Debug("router context done in module", "id", nc.Id())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ func (pc *PSNClient) Run() error {
|
||||
select {
|
||||
case <-pc.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", pc.config.Id)
|
||||
slog.Debug("router context done in module", "id", pc.Id())
|
||||
return nil
|
||||
default:
|
||||
pc.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -76,15 +76,15 @@ func (pc *PSNClient) Run() error {
|
||||
message := buffer[:numBytes]
|
||||
err := pc.decoder.Decode(message)
|
||||
if err != nil {
|
||||
slog.Error("net.psn.client problem decoding psn traffic", "id", pc.config.Id, "error", err)
|
||||
slog.Error("net.psn.client problem decoding psn traffic", "id", pc.Id(), "error", err)
|
||||
}
|
||||
|
||||
if pc.router != nil {
|
||||
for _, tracker := range pc.decoder.Trackers {
|
||||
pc.router.HandleInput(pc.config.Id, tracker)
|
||||
pc.router.HandleInput(pc.Id(), tracker)
|
||||
}
|
||||
} else {
|
||||
slog.Error("net.psn.client has no router", "id", pc.config.Id)
|
||||
slog.Error("net.psn.client has no router", "id", pc.Id())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ func (mc *SerialClient) Run() error {
|
||||
// TODO(jwetzell): shutdown with router.Context properly
|
||||
go func() {
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
if mc.port != nil {
|
||||
mc.port.Close()
|
||||
}
|
||||
@@ -115,10 +115,10 @@ func (mc *SerialClient) Run() error {
|
||||
err := mc.SetupPort()
|
||||
if err != nil {
|
||||
if mc.ctx.Err() != nil {
|
||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
return nil
|
||||
}
|
||||
slog.Error("misc.serial.client", "id", mc.config.Id, "error", err.Error())
|
||||
slog.Error("misc.serial.client", "id", mc.Id(), "error", err.Error())
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
}
|
||||
@@ -126,14 +126,14 @@ func (mc *SerialClient) Run() error {
|
||||
buffer := make([]byte, 1024)
|
||||
select {
|
||||
case <-mc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
return nil
|
||||
default:
|
||||
READ:
|
||||
for {
|
||||
select {
|
||||
case <-mc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
return nil
|
||||
default:
|
||||
byteCount, err := mc.port.Read(buffer)
|
||||
@@ -148,9 +148,9 @@ func (mc *SerialClient) Run() error {
|
||||
messages := mc.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if mc.router != nil {
|
||||
mc.router.HandleInput(mc.config.Id, message)
|
||||
mc.router.HandleInput(mc.Id(), message)
|
||||
} else {
|
||||
slog.Error("misc.serial.client has no router", "id", mc.config.Id)
|
||||
slog.Error("misc.serial.client has no router", "id", mc.Id())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (tc *TCPClient) Run() error {
|
||||
// TODO(jwetzell): shutdown with router.Context properly
|
||||
go func() {
|
||||
<-tc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
if tc.conn != nil {
|
||||
tc.conn.Close()
|
||||
}
|
||||
@@ -101,10 +101,10 @@ func (tc *TCPClient) Run() error {
|
||||
err := tc.SetupConn()
|
||||
if err != nil {
|
||||
if tc.ctx.Err() != nil {
|
||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
return nil
|
||||
}
|
||||
slog.Error("net.tcp.client", "id", tc.config.Id, "error", err.Error())
|
||||
slog.Error("net.tcp.client", "id", tc.Id(), "error", err.Error())
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
}
|
||||
@@ -112,14 +112,14 @@ func (tc *TCPClient) Run() error {
|
||||
buffer := make([]byte, 1024)
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
return nil
|
||||
default:
|
||||
READ:
|
||||
for {
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
return nil
|
||||
default:
|
||||
byteCount, err := tc.conn.Read(buffer)
|
||||
@@ -134,9 +134,9 @@ func (tc *TCPClient) Run() error {
|
||||
messages := tc.framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if tc.router != nil {
|
||||
tc.router.HandleInput(tc.config.Id, message)
|
||||
tc.router.HandleInput(tc.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.tcp.client has no router", "id", tc.config.Id)
|
||||
slog.Error("net.tcp.client has no router", "id", tc.Id())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
||||
ts.connectionsMu.Lock()
|
||||
ts.connections = append(ts.connections, client)
|
||||
ts.connectionsMu.Unlock()
|
||||
slog.Debug("net.tcp.server connection accepted", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
|
||||
slog.Debug("net.tcp.server connection accepted", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
defer client.Close()
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
@@ -125,7 +125,7 @@ ClientRead:
|
||||
break
|
||||
}
|
||||
}
|
||||
slog.Debug("net.tcp.server connection reset", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
|
||||
slog.Debug("net.tcp.server connection reset", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,7 @@ ClientRead:
|
||||
break
|
||||
}
|
||||
}
|
||||
slog.Debug("net.tcp.server stream ended", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
|
||||
slog.Debug("net.tcp.server stream ended", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
return
|
||||
@@ -148,9 +148,9 @@ ClientRead:
|
||||
messages := ts.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if ts.router != nil {
|
||||
ts.router.HandleInput(ts.config.Id, message)
|
||||
ts.router.HandleInput(ts.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.tcp.server has no router", "id", ts.config.Id)
|
||||
slog.Error("net.tcp.server has no router", "id", ts.Id())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,7 +170,7 @@ func (ts *TCPServer) Run() error {
|
||||
<-ts.ctx.Done()
|
||||
close(ts.quit)
|
||||
listener.Close()
|
||||
slog.Debug("router context done in module", "id", ts.config.Id)
|
||||
slog.Debug("router context done in module", "id", ts.Id())
|
||||
}()
|
||||
|
||||
AcceptLoop:
|
||||
|
||||
@@ -55,11 +55,11 @@ func (t *Timer) Run() error {
|
||||
select {
|
||||
case <-t.ctx.Done():
|
||||
t.timer.Stop()
|
||||
slog.Debug("router context done in module", "id", t.config.Id)
|
||||
slog.Debug("router context done in module", "id", t.Id())
|
||||
return nil
|
||||
case time := <-t.timer.C:
|
||||
if t.router != nil {
|
||||
t.router.HandleInput(t.config.Id, time)
|
||||
t.router.HandleInput(t.Id(), time)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ func (uc *UDPClient) Run() error {
|
||||
}
|
||||
|
||||
<-uc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", uc.config.Id)
|
||||
slog.Debug("router context done in module", "id", uc.Id())
|
||||
if uc.conn != nil {
|
||||
uc.conn.Close()
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ func (um *UDPMulticast) Run() error {
|
||||
select {
|
||||
case <-um.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", um.config.Id)
|
||||
slog.Debug("router context done in module", "id", um.Id())
|
||||
return nil
|
||||
default:
|
||||
um.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -97,9 +97,9 @@ func (um *UDPMulticast) Run() error {
|
||||
message := buffer[:numBytes]
|
||||
|
||||
if um.router != nil {
|
||||
um.router.HandleInput(um.config.Id, message)
|
||||
um.router.HandleInput(um.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.udp.multicast has no router", "id", um.config.Id)
|
||||
slog.Error("net.udp.multicast has no router", "id", um.Id())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ func (us *UDPServer) Run() error {
|
||||
select {
|
||||
case <-us.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", us.config.Id)
|
||||
slog.Debug("router context done in module", "id", us.Id())
|
||||
return nil
|
||||
default:
|
||||
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -96,9 +96,9 @@ func (us *UDPServer) Run() error {
|
||||
}
|
||||
message := buffer[:numBytes]
|
||||
if us.router != nil {
|
||||
us.router.HandleInput(us.config.Id, message)
|
||||
us.router.HandleInput(us.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.udp.server has no router", "id", us.config.Id)
|
||||
slog.Error("net.udp.server has no router", "id", us.Id())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user