Skip to content

Commit

Permalink
Customize data sent
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras committed Nov 4, 2024
1 parent b15c797 commit c926131
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ These will be converted to issues in the future, but some other ideas...
Test that connectivity is NOT made between different machines that should not
talk to each other, for firewall/security reasons.

Customizable data to send.

Allow metric collection (Prometheus, etc).

Realtime streaming updates for the HTML view.
Expand Down
6 changes: 4 additions & 2 deletions cmd/cyn/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var config struct {
SendUDP []string `mapstructure:"send-udp"`
SendTCP []string `mapstructure:"send-tcp"`
SendInterval time.Duration `mapstructure:"send-interval"`
SendData string `mapstructure:"send-data"`
HTTPServer struct {
Address string `mapstructure:"address"`
} `mapstructure:"http"`
Expand Down Expand Up @@ -49,6 +50,7 @@ func init() {
flags.StringSliceP("listen-tcp", "t", nil, "An IP:port address to listen on for TCP. Can be specified multiple times.")
flags.StringSliceP("send-udp", "U", nil, "An IP:port address to send to (UDP). Can be specified multiple times.")
flags.StringSliceP("send-tcp", "T", nil, "An IP:port address to send to (TCP). Can be specified multiple times.")
flags.StringP("send-data", "d", "hi", "The string data to send.")
flags.DurationP("send-interval", "i", time.Second, "How long to wait between attempting to send data")
flags.String("http.address", "", "An address:port to host an HTTP server on for realtime data, such as '127.0.0.1:8080'")
flags.Bool("sinks.stdout.enabled", false, "Whether to enable the stdout metrics sink")
Expand Down Expand Up @@ -124,7 +126,7 @@ var rootCmd = &cobra.Command{
return fmt.Errorf("net.ResolveUDPAddr for %q: %w", sendUDPTo, err)
}

instance.AddUDPSender(sender.NewUDPSender(*addr, config.SendInterval, sink))
instance.AddUDPSender(sender.NewUDPSender(*addr, config.SendInterval, sink, []byte(config.SendData)))
}

// We could probably generalize this a bit better, but it's short enough
Expand All @@ -136,7 +138,7 @@ var rootCmd = &cobra.Command{
return fmt.Errorf("net.ResolveTCPAddr for %q: %w", sendTCPTo, err)
}

instance.AddTCPSender(sender.NewTCPSender(*addr, config.SendInterval, sink))
instance.AddTCPSender(sender.NewTCPSender(*addr, config.SendInterval, sink, []byte(config.SendData)))
}

return instance.Run()
Expand Down
7 changes: 5 additions & 2 deletions pkg/sender/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ type TCPSender struct {
conn *net.TCPConn
sendInterval time.Duration

sendData []byte

sink metrics.Sink
}

func NewTCPSender(addr net.TCPAddr, sendInterval time.Duration, sink metrics.Sink) *TCPSender {
func NewTCPSender(addr net.TCPAddr, sendInterval time.Duration, sink metrics.Sink, sendData []byte) *TCPSender {
return &TCPSender{
broadcastAddr: addr,
sendInterval: sendInterval,
sink: sink,
sendData: sendData,
}
}

Expand Down Expand Up @@ -91,7 +94,7 @@ func (s *TCPSender) Run() error {
s.mu.RUnlock()

for {
err := s.Send([]byte("hi"))
err := s.Send(s.sendData)

if err != nil {
log.Printf("Failed to send to %q: %v", sendTCPTo, err)
Expand Down
7 changes: 5 additions & 2 deletions pkg/sender/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ type UDPSender struct {
fromAddr string
toAddr string

sendData []byte

sink metrics.Sink
}

func NewUDPSender(addr net.UDPAddr, sendInterval time.Duration, sink metrics.Sink) *UDPSender {
func NewUDPSender(addr net.UDPAddr, sendInterval time.Duration, sink metrics.Sink, sendData []byte) *UDPSender {
return &UDPSender{
broadcastAddr: addr,
sendInterval: sendInterval,
sink: sink,
sendData: sendData,
}
}

Expand Down Expand Up @@ -80,7 +83,7 @@ func (s *UDPSender) Run() error {
s.mu.RUnlock()

for {
err := s.Send([]byte("hi"))
err := s.Send(s.sendData)
if err != nil {
log.Printf("Failed to send to %q: %v", sendUDPTo, err)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/features/udp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Feature: send and receive UDP

Scenario: one listen one send (shorthand flags)
Given I run cyn -u 127.0.0.1:14563
And I run cyn -U 127.0.0.1:14563 -i 10ms
And I run cyn -U 127.0.0.1:14563 -i 10ms -d "bdd-test-data"
When I wait a moment
Then the stdout contains "hi"
Then the stdout contains "bdd-test-data"

Scenario: an instance is set to call itself via config file
Given a configuration file that contains:
Expand Down

0 comments on commit c926131

Please sign in to comment.