Laying the groundwork for a better view of the output
This commit is contained in:
@@ -17,3 +17,7 @@ To start replaying:
|
||||
|
||||
To start recording:
|
||||
`BeamNGMockOg record -a 127.0.0.1 -p 4443 -o sunburstDCT.bin`
|
||||
|
||||
## Development
|
||||
Use `tcpdump` to listen to the socket and check if data is coming through:
|
||||
`tcpdump -i any udp port <port> -X`
|
||||
|
||||
+60
-8
@@ -10,13 +10,49 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Replay replays a given file <fp> in a UDP server <addr>:<port>
|
||||
func Replay(ctx context.Context, address string, port int, loop bool, fp string) error {
|
||||
type ViewData struct {
|
||||
Data []byte
|
||||
SizeRead int
|
||||
}
|
||||
|
||||
// renderToTerminal will render the data for the users viewing pleasure
|
||||
func renderToTerminal(ctx context.Context, stream chan ViewData, fp string) {
|
||||
fileInfo, err := os.Stat(fp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error stating file: %v", err)
|
||||
// NOTE: learn how to handle this error
|
||||
// return fmt.Errorf("error stating file: %v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case data := <-stream:
|
||||
percent := int(float64(data.SizeRead) / float64(fileInfo.Size()) * 100)
|
||||
fmt.Printf("\rReplayed: %d%%", percent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// writeToUDPSocket will write the telemetry data to the UDP socket
|
||||
func writeToUDPSocket(ctx context.Context, stream chan []byte, socket *UDPTransport) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case data := <-stream:
|
||||
_, err := socket.Send(data)
|
||||
if err != nil {
|
||||
continue
|
||||
// NOTE: log the error somewhere maybe
|
||||
// return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replay replays a given file <fp> in a UDP server <addr>:<port>
|
||||
func Replay(ctx context.Context, address string, port int, loop bool, fp string) error {
|
||||
bin, err := os.Open(fp)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening file: %v", err)
|
||||
@@ -31,6 +67,12 @@ func Replay(ctx context.Context, address string, port int, loop bool, fp string)
|
||||
ticker := time.NewTicker(time.Second / 60)
|
||||
defer ticker.Stop()
|
||||
|
||||
uiChannel := make(chan ViewData, 1)
|
||||
socketChannel := make(chan []byte, 1)
|
||||
|
||||
go renderToTerminal(ctx, uiChannel, fp)
|
||||
go writeToUDPSocket(ctx, socketChannel, udp)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -47,6 +89,7 @@ func Replay(ctx context.Context, address string, port int, loop bool, fp string)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -54,13 +97,22 @@ func Replay(ctx context.Context, address string, port int, loop bool, fp string)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = udp.Send(data)
|
||||
if err != nil {
|
||||
return err
|
||||
// Send the data to the view
|
||||
select {
|
||||
case uiChannel <- ViewData{Data: data, SizeRead: reader.TotalRead}:
|
||||
// Sent the data
|
||||
default:
|
||||
// Dropped the frame!
|
||||
}
|
||||
|
||||
// Send the data to the UDP socket
|
||||
select {
|
||||
case socketChannel <- data:
|
||||
// Sent the data
|
||||
default:
|
||||
// Dropped the frame!
|
||||
}
|
||||
|
||||
percent := int(float64(reader.TotalRead) / float64(fileInfo.Size()) * 100)
|
||||
fmt.Printf("\rReplayed: %d%%", percent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user