-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
echo.go
35 lines (32 loc) · 835 Bytes
/
echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package inet256cmd
import (
"github.com/brendoncarroll/stdctx/logctx"
"github.com/spf13/cobra"
"github.com/inet256/inet256/pkg/inet256"
)
func NewEchoCmd(newNode NodeFactory) *cobra.Command {
return &cobra.Command{
Use: "echo",
Short: "echo starts a server which echos all messages",
RunE: func(cmd *cobra.Command, args []string) error {
pk := generateKey()
node, err := newNode(ctx, pk)
if err != nil {
return err
}
defer node.Close()
logctx.Infoln(ctx, node.LocalAddr())
var msg inet256.Message
for {
if err := inet256.Receive(ctx, node, &msg); err != nil {
return err
}
if err := node.Send(ctx, msg.Src, msg.Payload); err != nil {
logctx.Errorln(ctx, err)
continue
}
logctx.Infof(ctx, "echoed %d bytes from %v", len(msg.Payload), msg.Src)
}
},
}
}