Refactor gateway module to remove mqtt dependency

This commit is contained in:
2021-01-21 23:39:52 +01:00
parent 8107aca0a8
commit 0f79efacf2
8 changed files with 496 additions and 267 deletions

View File

@ -1,15 +1,8 @@
package gateway
import (
"bufio"
"encoding/json"
"fmt"
"github.com/cyrilix/robocar-protobuf/go/events"
"github.com/cyrilix/robocar-simulator/pkg/simulator"
log "github.com/sirupsen/logrus"
"io"
"net"
"sync"
"testing"
)
@ -71,7 +64,7 @@ func TestGateway_WriteSteering(t *testing.T) {
}
}()
gw := New(nil, simulatorMock.Addr())
gw := New(simulatorMock.Addr())
if err != nil {
t.Fatalf("unable to init simulator gateway: %v", err)
}
@ -190,7 +183,7 @@ func TestGateway_WriteThrottle(t *testing.T) {
}
}()
gw := New(nil, simulatorMock.Addr())
gw := New(simulatorMock.Addr())
if err != nil {
t.Fatalf("unable to init simulator gateway: %v", err)
}
@ -206,78 +199,3 @@ func TestGateway_WriteThrottle(t *testing.T) {
}
}
}
type ConnMock struct {
initMsgsOnce sync.Once
ln net.Listener
notifyChan chan *simulator.ControlMsg
initNotifyChan sync.Once
}
func (c *ConnMock) Notify() <-chan *simulator.ControlMsg {
c.initNotifyChan.Do(func() { c.notifyChan = make(chan *simulator.ControlMsg) })
return c.notifyChan
}
func (c *ConnMock) listen() error {
ln, err := net.Listen("tcp", "127.0.0.1:")
c.ln = ln
if err != nil {
return fmt.Errorf("unable to listen on port: %v", err)
}
go func() {
for {
conn, err := c.ln.Accept()
if err != nil {
log.Infof("connection close: %v", err)
break
}
go c.handleConnection(conn)
}
}()
return nil
}
func (c *ConnMock) Addr() string {
return c.ln.Addr().String()
}
func (c *ConnMock) handleConnection(conn net.Conn) {
c.initNotifyChan.Do(func() { c.notifyChan = make(chan *simulator.ControlMsg) })
reader := bufio.NewReader(conn)
for {
rawCmd, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
log.Info("connection closed")
break
}
log.Errorf("unable to read request: %v", err)
return
}
var msg simulator.ControlMsg
err = json.Unmarshal(rawCmd, &msg)
if err != nil {
log.Errorf("unable to unmarchal control msg \"%v\": %v", string(rawCmd), err)
continue
}
c.notifyChan <- &msg
}
}
func (c *ConnMock) Close() error {
log.Infof("close mock server")
err := c.ln.Close()
if err != nil {
return fmt.Errorf("unable to close mock server: %v", err)
}
if c.notifyChan != nil {
close(c.notifyChan)
}
return nil
}

View File

@ -7,8 +7,6 @@ import (
"github.com/avast/retry-go"
"github.com/cyrilix/robocar-protobuf/go/events"
"github.com/cyrilix/robocar-simulator/pkg/simulator"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
log "github.com/sirupsen/logrus"
"io"
@ -17,18 +15,36 @@ import (
"time"
)
func New(publisher Publisher, addressSimulator string) *Gateway {
type SimulatorSource interface {
FrameSource
SteeringSource
ThrottleSource
}
type FrameSource interface {
SubscribeFrame() <-chan *events.FrameMessage
}
type SteeringSource interface {
SubscribeSteering() <-chan *events.SteeringMessage
}
type ThrottleSource interface {
SubscribeThrottle() <-chan *events.ThrottleMessage
}
func New(addressSimulator string) *Gateway {
l := log.WithField("simulator", addressSimulator)
l.Info("run gateway from simulator")
return &Gateway{
address: addressSimulator,
publisher: publisher,
log: l,
address: addressSimulator,
log: l,
frameSubscribers: make(map[chan<- *events.FrameMessage]interface{}),
steeringSubscribers: make(map[chan<- *events.SteeringMessage]interface{}),
throttleSubscribers: make(map[chan<- *events.ThrottleMessage]interface{}),
}
}
/* Simulator interface to publish gateway frames into mqtt topicFrame */
/* Simulator interface to events gateway frames into events topicFrame */
type Gateway struct {
cancel chan interface{}
@ -39,8 +55,11 @@ type Gateway struct {
muControl sync.Mutex
lastControl *simulator.ControlMsg
publisher Publisher
log *log.Entry
log *log.Entry
frameSubscribers map[chan<- *events.FrameMessage]interface{}
steeringSubscribers map[chan<- *events.SteeringMessage]interface{}
throttleSubscribers map[chan<- *events.ThrottleMessage]interface{}
}
func (p *Gateway) Start() error {
@ -53,9 +72,9 @@ func (p *Gateway) Start() error {
for {
select {
case msg := <-msgChan:
go p.publishFrame(msg)
go p.publishInputSteering(msg)
go p.publishInputThrottle(msg)
fr := p.publishFrame(msg)
go p.publishInputSteering(msg, fr)
go p.publishInputThrottle(msg, fr)
case <-p.cancel:
return nil
}
@ -72,6 +91,15 @@ func (p *Gateway) Stop() {
}
func (p *Gateway) Close() error {
for c := range p.frameSubscribers {
close(c)
}
for c := range p.steeringSubscribers {
close(c)
}
for c := range p.throttleSubscribers {
close(c)
}
if p.conn == nil {
p.log.Warn("no connection to close")
return nil
@ -144,54 +172,68 @@ func (p *Gateway) listen(msgChan chan<- *simulator.TelemetryMsg, reader *bufio.R
}
}
func (p *Gateway) publishFrame(msgSim *simulator.TelemetryMsg) {
func (p *Gateway) publishFrame(msgSim *simulator.TelemetryMsg) *events.FrameRef {
now := time.Now()
msg := &events.FrameMessage{
Id: &events.FrameRef{
Name: "gateway",
Id: fmt.Sprintf("%d%03d", now.Unix(), now.Nanosecond()/1000/1000),
CreatedAt: &timestamp.Timestamp{
Seconds: now.Unix(),
Nanos: int32(now.Nanosecond()),
},
frameRef := &events.FrameRef{
Name: "gateway",
Id: fmt.Sprintf("%d%03d", now.Unix(), now.Nanosecond()/1000/1000),
CreatedAt: &timestamp.Timestamp{
Seconds: now.Unix(),
Nanos: int32(now.Nanosecond()),
},
}
msg := &events.FrameMessage{
Id: frameRef,
Frame: msgSim.Image,
}
log.Debugf("publish frame '%v/%v'", msg.Id.Name, msg.Id.Id)
payload, err := proto.Marshal(msg)
if err != nil {
p.log.Errorf("unable to marshal protobuf message: %v", err)
log.Debugf("events frame '%v/%v'", msg.Id.Name, msg.Id.Id)
for fs := range p.frameSubscribers {
fs <- msg
}
p.publisher.PublishFrame(payload)
return frameRef
}
func (p *Gateway) publishInputSteering(msgSim *simulator.TelemetryMsg) {
func (p *Gateway) publishInputSteering(msgSim *simulator.TelemetryMsg, frameRef *events.FrameRef) {
steering := &events.SteeringMessage{
FrameRef: frameRef,
Steering: float32(msgSim.SteeringAngle),
Confidence: 1.0,
}
log.Debugf("publish steering '%v'", steering.Steering)
payload, err := proto.Marshal(steering)
if err != nil {
p.log.Errorf("unable to marshal protobuf message: %v", err)
log.Debugf("events steering '%v'", steering.Steering)
for ss := range p.steeringSubscribers {
ss <- steering
}
p.publisher.PublishSteering(payload)
}
func (p *Gateway) publishInputThrottle(msgSim *simulator.TelemetryMsg) {
steering := &events.ThrottleMessage{
func (p *Gateway) publishInputThrottle(msgSim *simulator.TelemetryMsg, frameRef *events.FrameRef) {
msg := &events.ThrottleMessage{
FrameRef: frameRef,
Throttle: float32(msgSim.Throttle),
Confidence: 1.0,
}
log.Debugf("publish throttle '%v'", steering.Throttle)
payload, err := proto.Marshal(steering)
if err != nil {
p.log.Errorf("unable to marshal protobuf message: %v", err)
log.Debugf("events throttle '%v'", msg.Throttle)
for ts := range p.throttleSubscribers {
ts <- msg
}
p.publisher.PublishThrottle(payload)
}
func (p *Gateway) SubscribeFrame() <-chan *events.FrameMessage {
frameChan := make(chan *events.FrameMessage)
p.frameSubscribers[frameChan] = struct{}{}
return frameChan
}
func (p *Gateway) SubscribeSteering() <-chan *events.SteeringMessage {
steeringChan := make(chan *events.SteeringMessage)
p.steeringSubscribers[steeringChan] = struct{}{}
return steeringChan
}
func (p *Gateway) SubscribeThrottle() <-chan *events.ThrottleMessage {
throttleChan := make(chan *events.ThrottleMessage)
p.throttleSubscribers[throttleChan] = struct{}{}
return throttleChan
}
var connect = func(address string) (io.ReadWriteCloser, error) {
@ -262,64 +304,3 @@ func (p *Gateway) initLastControlMsg() {
Brake: 0.,
}
}
type Publisher interface {
PublishFrame(payload []byte)
PublishThrottle(payload []byte)
PublishSteering(payload []byte)
}
func NewMqttPublisher(client mqtt.Client, topicFrame, topicThrottle, topicSteering string) Publisher {
return &MqttPublisher{
client: client,
topicFrame: topicFrame,
topicSteering: topicSteering,
topicThrottle: topicThrottle,
}
}
type MqttPublisher struct {
client mqtt.Client
topicFrame string
topicSteering string
topicThrottle string
}
func (m *MqttPublisher) PublishThrottle(payload []byte) {
if m.topicThrottle == "" {
return
}
err := m.publish(m.topicThrottle, payload)
if err != nil {
log.Errorf("unable to publish throttle: %v", err)
}
}
func (m *MqttPublisher) PublishSteering(payload []byte) {
if m.topicSteering == "" {
return
}
err := m.publish(m.topicSteering, payload)
if err != nil {
log.Errorf("unable to publish steering: %v", err)
}
}
func (m *MqttPublisher) PublishFrame(payload []byte) {
if m.topicFrame == "" {
return
}
err := m.publish(m.topicFrame, payload)
if err != nil {
log.Errorf("unable to publish frame: %v", err)
}
}
func (m *MqttPublisher) publish(topic string, payload []byte) error {
token := m.client.Publish(topic, 0, false, payload)
token.WaitTimeout(10 * time.Millisecond)
if err := token.Error(); err != nil {
return fmt.Errorf("unable to publish to topic: %v", err)
}
return nil
}

View File

@ -3,7 +3,6 @@ package gateway
import (
"encoding/json"
"github.com/cyrilix/robocar-protobuf/go/events"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
"io/ioutil"
"strings"
@ -12,51 +11,6 @@ import (
"time"
)
type MockPublisher struct {
notifyFrameChan chan []byte
initNotifyFrameChan sync.Once
notifySteeringChan chan []byte
initNotifySteeringChan sync.Once
notifyThrottleChan chan []byte
initNotifyThrottleChan sync.Once
}
func (p *MockPublisher) Close() error {
if p.notifyFrameChan != nil {
close(p.notifyFrameChan)
}
if p.notifyThrottleChan != nil {
close(p.notifyThrottleChan)
}
if p.notifySteeringChan != nil {
close(p.notifySteeringChan)
}
return nil
}
func (p *MockPublisher) PublishFrame(payload []byte) {
p.notifyFrameChan <- payload
}
func (p *MockPublisher) PublishSteering(payload []byte) {
p.notifySteeringChan <- payload
}
func (p *MockPublisher) PublishThrottle(payload []byte) {
p.notifyThrottleChan <- payload
}
func (p *MockPublisher) NotifyFrame() <-chan []byte {
p.initNotifyFrameChan.Do(func() { p.notifyFrameChan = make(chan []byte) })
return p.notifyFrameChan
}
func (p *MockPublisher) NotifySteering() <-chan []byte {
p.initNotifySteeringChan.Do(func() { p.notifySteeringChan = make(chan []byte) })
return p.notifySteeringChan
}
func (p *MockPublisher) NotifyThrottle() <-chan []byte {
p.initNotifyThrottleChan.Do(func() { p.notifyThrottleChan = make(chan []byte) })
return p.notifyThrottleChan
}
func TestGateway_ListenEvents(t *testing.T) {
simulatorMock := Sim2GwMock{}
err := simulatorMock.Start()
@ -69,20 +23,21 @@ func TestGateway_ListenEvents(t *testing.T) {
}
}()
publisher := MockPublisher{}
part := New(&publisher, simulatorMock.Addr())
gw := New(simulatorMock.Addr())
go func() {
err := part.Start()
err := gw.Start()
if err != nil {
t.Fatalf("unable to start gateway simulator: %v", err)
}
}()
defer func() {
if err := part.Close(); err != nil {
if err := gw.Close(); err != nil {
t.Errorf("unable to close gateway simulator: %v", err)
}
}()
frameChannel := gw.SubscribeFrame()
steeringChannel := gw.SubscribeSteering()
throttleChannel := gw.SubscribeThrottle()
simulatorMock.WaitConnection()
log.Trace("read test data")
@ -98,7 +53,7 @@ func TestGateway_ListenEvents(t *testing.T) {
eventsType := map[string]bool{"frame": false, "steering": false, "throttle": false}
nbEventsExpected := len(eventsType)
wg := sync.WaitGroup{}
// Expect number mqtt event
// Expect number events event
wg.Add(nbEventsExpected)
finished := make(chan struct{})
@ -110,19 +65,24 @@ func TestGateway_ListenEvents(t *testing.T) {
timeout := time.Tick(100 * time.Millisecond)
endLoop := false
var frameRef, steeringIdRef, throttleIdRef *events.FrameRef
for {
select {
case byteMsg := <-publisher.NotifyFrame():
checkFrame(t, byteMsg)
case msg := <-frameChannel:
checkFrame(t, msg)
eventsType["frame"] = true
frameRef = msg.Id
wg.Done()
case byteMsg := <-publisher.NotifySteering():
checkSteering(t, byteMsg, line)
case msg := <-steeringChannel:
checkSteering(t, msg, line)
eventsType["steering"] = true
steeringIdRef = msg.FrameRef
wg.Done()
case byteMsg := <-publisher.NotifyThrottle():
checkThrottle(t, byteMsg, line)
case msg := <-throttleChannel:
checkThrottle(t, msg, line)
eventsType["throttle"] = true
throttleIdRef = msg.FrameRef
wg.Done()
case <-finished:
log.Trace("loop ended")
@ -132,6 +92,12 @@ func TestGateway_ListenEvents(t *testing.T) {
t.FailNow()
}
if endLoop {
if frameRef != steeringIdRef || steeringIdRef == nil {
t.Errorf("steering msg without frameRef '%#v', wants '%#v'", steeringIdRef, frameRef)
}
if frameRef != throttleIdRef || throttleIdRef == nil {
t.Errorf("throttle msg without frameRef '%#v', wants '%#v'", throttleIdRef, frameRef)
}
break
}
}
@ -143,12 +109,7 @@ func TestGateway_ListenEvents(t *testing.T) {
}
}
func checkFrame(t *testing.T, byteMsg []byte) {
var msg events.FrameMessage
err := proto.Unmarshal(byteMsg, &msg)
if err != nil {
t.Errorf("unable to unmarshal frame msg: %v", err)
}
func checkFrame(t *testing.T, msg *events.FrameMessage) {
if msg.GetId() == nil {
t.Error("frame msg has not Id")
}
@ -156,7 +117,7 @@ func checkFrame(t *testing.T, byteMsg []byte) {
t.Errorf("[%v] invalid frame image: %v", msg.Id, msg.GetFrame())
}
}
func checkSteering(t *testing.T, byteMsg []byte, rawLine string) {
func checkSteering(t *testing.T, msg *events.SteeringMessage, rawLine string) {
var input map[string]interface{}
err := json.Unmarshal([]byte(rawLine), &input)
if err != nil {
@ -165,12 +126,6 @@ func checkSteering(t *testing.T, byteMsg []byte, rawLine string) {
steering := input["steering_angle"].(float64)
expectedSteering := float32(steering)
var msg events.SteeringMessage
err = proto.Unmarshal(byteMsg, &msg)
if err != nil {
t.Errorf("unable to unmarshal steering msg: %v", err)
}
if msg.GetSteering() != expectedSteering {
t.Errorf("invalid steering value: %f, wants %f", msg.GetSteering(), expectedSteering)
}
@ -179,7 +134,7 @@ func checkSteering(t *testing.T, byteMsg []byte, rawLine string) {
}
}
func checkThrottle(t *testing.T, byteMsg []byte, rawLine string) {
func checkThrottle(t *testing.T, msg *events.ThrottleMessage, rawLine string) {
var input map[string]interface{}
err := json.Unmarshal([]byte(rawLine), &input)
if err != nil {
@ -188,14 +143,8 @@ func checkThrottle(t *testing.T, byteMsg []byte, rawLine string) {
throttle := input["throttle"].(float64)
expectedThrottle := float32(throttle)
var msg events.SteeringMessage
err = proto.Unmarshal(byteMsg, &msg)
if err != nil {
t.Errorf("unable to unmarshal throttle msg: %v", err)
}
if msg.GetSteering() != expectedThrottle {
t.Errorf("invalid throttle value: %f, wants %f", msg.GetSteering(), expectedThrottle)
if msg.Throttle != expectedThrottle {
t.Errorf("invalid throttle value: %f, wants %f", msg.Throttle, expectedThrottle)
}
if msg.Confidence != 1.0 {
t.Errorf("invalid throttle confidence: %f, wants %f", msg.Confidence, 1.0)