-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement broadcaster and recorder
- Loading branch information
1 parent
ca313dc
commit 5c40295
Showing
17 changed files
with
562 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,7 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Other | ||
.idea | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/racing-telemetry/f1-dump/internal/text/emoji" | ||
"github.com/racing-telemetry/f1-dump/internal/text/printer" | ||
"github.com/racing-telemetry/f1-dump/pkg/broadcaster" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
var broadcastCmd = &cobra.Command{ | ||
Use: "broadcast", | ||
Short: "Start broadcasting", | ||
Long: `Start broadcasting`, | ||
SilenceUsage: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
flags, err := buildFlags(cmd) | ||
if err != nil { | ||
return printer.Error(err) | ||
} | ||
|
||
b, err := broadcaster.NewBroadcaster(flags.UDPAddr()) | ||
if err != nil { | ||
return printer.Error(err) | ||
} | ||
|
||
// wait exit signal, ctrl+c to early exit | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
|
||
go func() { | ||
<-c | ||
|
||
b.Stop() | ||
printer.Print(emoji.Rocket, "%d packs have been published", b.Stats.RecvCount()) | ||
|
||
os.Exit(0) | ||
}() | ||
|
||
printer.Print(emoji.Sparkless, "Broadcast started at %s:%d, press Ctrl+C to stop", flags.host, flags.port) | ||
err = b.Start(flags.file) | ||
if err != nil { | ||
return printer.Error(err) | ||
} | ||
|
||
b.Stop() | ||
printer.Print(emoji.Rocket, "%d packs have been published", b.Stats.RecvCount()) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
addFlags(broadcastCmd) | ||
|
||
rootCmd.AddCommand(broadcastCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/racing-telemetry/f1-dump/internal" | ||
"github.com/spf13/cobra" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type Flags struct { | ||
port int | ||
host string | ||
file string | ||
} | ||
|
||
func (f *Flags) UDPAddr() *net.UDPAddr { | ||
return &net.UDPAddr{ | ||
IP: net.ParseIP(f.host), | ||
Port: f.port, | ||
} | ||
} | ||
|
||
func addFlags(cmd *cobra.Command) { | ||
cmd.Flags().IntP("port", "p", internal.DefaultPort, "Port address to listen on UDP.") | ||
cmd.Flags().String("ip", internal.DefaultHost, "Address to listen on UDP.") | ||
cmd.Flags().StringP("file", "f", "", "I/O file path or name to save and read packets. (sample: foo/bar/output.bin)") | ||
} | ||
|
||
func buildFlags(cmd *cobra.Command) (*Flags, error) { | ||
port, err := cmd.Flags().GetInt("port") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
host, _ := cmd.Flags().GetString("ip") | ||
if host == "" { | ||
host = internal.DefaultHost | ||
} | ||
|
||
path, _ := cmd.Flags().GetString("file") | ||
path = strings.TrimSpace(path) | ||
if path != "" { | ||
ext := filepath.Ext(path) | ||
if ext != ".bin" { | ||
return nil, errors.New("file extension must be ends with .bin") | ||
} | ||
|
||
_, err = os.Stat(path) | ||
switch cmd.Name() { | ||
case "record": | ||
if err == nil { | ||
return nil, fmt.Errorf("file already exists: %s", path) | ||
} | ||
|
||
case "broadcast": | ||
if err != nil { | ||
return nil, fmt.Errorf("file doesnt exist: %s", path) | ||
} | ||
} | ||
} | ||
|
||
return &Flags{port: port, host: host, file: path}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/dustin/go-humanize" | ||
"github.com/racing-telemetry/f1-dump/internal/text/emoji" | ||
"github.com/racing-telemetry/f1-dump/internal/text/printer" | ||
"github.com/racing-telemetry/f1-dump/pkg/recorder" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
var cmdRecord = &cobra.Command{ | ||
Use: "record", | ||
Short: "Start recording packets from UDP source.", | ||
Long: `Start recording packets from UDP source.`, | ||
SilenceUsage: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
flags, err := buildFlags(cmd) | ||
if err != nil { | ||
return printer.Error(err) | ||
} | ||
|
||
rc, err := recorder.NewRecorder(flags.UDPAddr()) | ||
if err != nil { | ||
return fmt.Errorf("%s\n%s", printer.Error(errors.New("recorder can't create")), printer.Error(err)) | ||
} | ||
|
||
// wait exit signal, ctrl+c to exit | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
|
||
go func() { | ||
<-c | ||
|
||
rc.Stop() | ||
|
||
f, err := rc.Save(flags.file) | ||
if err != nil { | ||
printer.PrintError(err.Error()) | ||
} else { | ||
stat, err := f.Stat() | ||
if err != nil { | ||
printer.PrintError(err.Error()) | ||
} | ||
|
||
printer.Print(emoji.File, "File saved to %s (size: %s)", f.Name(), humanize.Bytes(uint64(stat.Size()))) | ||
|
||
err = f.Close() | ||
if err != nil { | ||
printer.PrintError(err.Error()) | ||
} | ||
} | ||
|
||
printer.Print(emoji.Rocket, "Received Packets: %d", rc.Stats.RecvCount()) | ||
printer.Print(emoji.Construction, "Lost Packets: %d", rc.Stats.ErrCount()) | ||
|
||
os.Exit(0) | ||
}() | ||
|
||
printer.Print(emoji.Sparkless, "Record started at %s:%d, press Ctrl+C to stop", flags.host, flags.port) | ||
rc.Start() | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
addFlags(cmdRecord) | ||
|
||
rootCmd.AddCommand(cmdRecord) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package internal | ||
|
||
const ( | ||
DefaultPort = 20777 | ||
DefaultHost = "localhost" | ||
) | ||
|
||
const BufferSize = 1024 + 1024/2 | ||
|
||
const OutFileFormat = "f1-out-%d.bin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package emoji | ||
|
||
type Emoji rune | ||
|
||
const ( | ||
Boom Emoji = '💥' | ||
Fire Emoji = '🔥' | ||
Sparkless Emoji = '✨' | ||
File Emoji = '📁' | ||
Construction Emoji = '🚧' | ||
Rocket Emoji = '🚀' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package printer | ||
|
||
import ( | ||
"fmt" | ||
"github.com/racing-telemetry/f1-dump/internal/text/emoji" | ||
) | ||
|
||
func Print(emoji emoji.Emoji, s string, a ...interface{}) { | ||
fmt.Printf("\r%s %s\n", string(emoji), fmt.Sprintf(s, a...)) | ||
} | ||
|
||
func Error(err error) error { | ||
return fmt.Errorf("\r%s Error: %s", string(emoji.Boom), err.Error()) | ||
} | ||
|
||
func PrintError(format string, a ...interface{}) { | ||
fmt.Println(Error(fmt.Errorf(format, a...))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package udp | ||
|
||
import ( | ||
"github.com/racing-telemetry/f1-dump/internal" | ||
"net" | ||
) | ||
|
||
type Client struct { | ||
conn *net.UDPConn | ||
} | ||
|
||
func Serve(addr *net.UDPAddr) (*Client, error) { | ||
conn, err := net.ListenUDP("udp", addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{conn: conn}, nil | ||
} | ||
|
||
func (c *Client) ReadSocket() ([]byte, error) { | ||
buf := make([]byte, internal.BufferSize) | ||
_, _, err := c.conn.ReadFromUDP(buf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return buf, err | ||
} | ||
|
||
func (c *Client) Close() error { | ||
return c.conn.Close() | ||
} |
Oops, something went wrong.