integrate broker into server
This commit is contained in:
parent
5d42f8693a
commit
f0db7da230
|
@ -37,11 +37,11 @@ type session struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func serve() {
|
func serve() {
|
||||||
|
|
||||||
|
broker := NewBroker(3)
|
||||||
// start the can listener
|
// start the can listener
|
||||||
go vcanTest()
|
go vcanTest()
|
||||||
fromCanBus := make(chan can.Frame, 1)
|
go canHandler(broker)
|
||||||
toCanBus := make(chan can.Frame)
|
|
||||||
go canHandler(toCanBus, fromCanBus)
|
|
||||||
ln, err := net.Listen("tcp", ":8082")
|
ln, err := net.Listen("tcp", ":8082")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error listening: %v\n", err)
|
fmt.Printf("Error listening: %v\n", err)
|
||||||
|
@ -53,21 +53,53 @@ func serve() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error accepting: %v\n", err)
|
fmt.Printf("error accepting: %v\n", err)
|
||||||
}
|
}
|
||||||
go handleCon(conn)
|
go handleCon(conn, broker)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCon(conn net.Conn) {
|
func handleCon(conn net.Conn, broker *Broker) {
|
||||||
// reader := msgp.NewReader(conn)
|
// reader := msgp.NewReader(conn)
|
||||||
|
rxPkts := make(chan gotelem.Data)
|
||||||
|
go func() {
|
||||||
|
// setpu our msgp reader.
|
||||||
|
scann := msgp.NewReader(conn)
|
||||||
|
data := gotelem.Data{}
|
||||||
|
for {
|
||||||
|
err := data.DecodeMsg(scann)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
rxPkts <- data
|
||||||
|
}
|
||||||
|
|
||||||
|
}()
|
||||||
|
|
||||||
|
// subscribe to can packets
|
||||||
|
// TODO: make this unique since remote addr could be non-unique
|
||||||
|
canCh := broker.Subscribe(conn.RemoteAddr().String())
|
||||||
writer := msgp.NewWriter(conn)
|
writer := msgp.NewWriter(conn)
|
||||||
for {
|
for {
|
||||||
|
|
||||||
|
select {
|
||||||
|
case canFrame := <-canCh:
|
||||||
|
cf := gotelem.CanBody{
|
||||||
|
Id: canFrame.Id,
|
||||||
|
Payload: canFrame.Data,
|
||||||
|
Source: "me",
|
||||||
|
}
|
||||||
|
cf.EncodeMsg(writer)
|
||||||
|
case rxBody := <-rxPkts:
|
||||||
|
// do nothing for now.
|
||||||
|
fmt.Printf("got a body %v\n", rxBody)
|
||||||
|
case <-time.NewTimer(1 * time.Second).C: // time out.
|
||||||
|
fmt.Printf("timeout\n")
|
||||||
data := gotelem.StatusBody{
|
data := gotelem.StatusBody{
|
||||||
BatteryPct: 1.2,
|
BatteryPct: 1.2,
|
||||||
ErrCode: 0,
|
ErrCode: 0,
|
||||||
}
|
}
|
||||||
data.EncodeMsg(writer)
|
data.EncodeMsg(writer)
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
time.Sleep(1 * time.Second)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +134,7 @@ func xbeeSvc(packets <-chan can.Frame, device string, quit <-chan struct{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// this spins up a new can socket on vcan0 and broadcasts a packet every second.
|
// this spins up a new can socket on vcan0 and broadcasts a packet every second. for testing.
|
||||||
func vcanTest() {
|
func vcanTest() {
|
||||||
sock, _ := socketcan.NewCanSocket("vcan0")
|
sock, _ := socketcan.NewCanSocket("vcan0")
|
||||||
testFrame := &can.Frame{
|
testFrame := &can.Frame{
|
||||||
|
@ -118,24 +150,25 @@ func vcanTest() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func canHandler(pktToSend <-chan can.Frame, pktRecv chan<- can.Frame) {
|
func canHandler(broker *Broker) {
|
||||||
|
rxCh := broker.Subscribe("socketcan")
|
||||||
sock, _ := socketcan.NewCanSocket("vcan0")
|
sock, _ := socketcan.NewCanSocket("vcan0")
|
||||||
|
|
||||||
// start a simple dispatcher that just relays can frames.
|
// start a simple dispatcher that just relays can frames.
|
||||||
r := make(chan can.Frame)
|
rxCan := make(chan can.Frame)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
pkt, _ := sock.Recv()
|
pkt, _ := sock.Recv()
|
||||||
r <- *pkt
|
rxCan <- *pkt
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg := <-pktToSend:
|
case msg := <-rxCh:
|
||||||
sock.Send(&msg)
|
sock.Send(&msg)
|
||||||
case msg := <-r:
|
case msg := <-rxCan:
|
||||||
fmt.Printf("got a packet from the can %v\n", msg)
|
fmt.Printf("got a packet from the can %v\n", msg)
|
||||||
pktRecv <- msg
|
broker.Publish("socketcan", msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue