From 61419c032a7fd6ef851a21b195c46d8d81b24495 Mon Sep 17 00:00:00 2001 From: Chris Keller Date: Sun, 1 Nov 2020 16:41:18 -0700 Subject: [PATCH] Add sending Replay Part of #2 --- cmd/main.go | 5 +++++ encoder.go | 7 +++++++ encoder_test.go | 32 ++++++++++++++++++++++++++++++++ messages.go | 17 +++++++++++++++++ server.go | 7 +++++++ 5 files changed, 68 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index 529638b..9c3b2ba 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -90,6 +90,11 @@ func handleCommand(command string, wsjtxServer wsjtx.Server) { case "close": fmt.Println("Sending Close") err = wsjtxServer.Close(wsjtx.CloseMessage{Id: "WSJT-X"}) + + case "replay": + fmt.Println("Sending Replay") + err = wsjtxServer.Replay(wsjtx.ReplayMessage{Id: "WSJT-X"}) + } if err != nil { fmt.Println(err) diff --git a/encoder.go b/encoder.go index ff4cf47..abd5c38 100644 --- a/encoder.go +++ b/encoder.go @@ -46,6 +46,13 @@ func encodeClose(msg CloseMessage) ([]byte, error) { return e.finish() } +func encodeReplay(msg ReplayMessage) ([]byte, error) { + e := newEncoder() + e.encodeUint32(replayNum) + e.encodeUtf8(msg.Id) + return e.finish() +} + type encoder struct { buf *bytes.Buffer } diff --git a/encoder_test.go b/encoder_test.go index c138d6e..073df59 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -147,3 +147,35 @@ func Test_encodeClose(t *testing.T) { }) } } + +func Test_encodeReplay(t *testing.T) { + type args struct { + msg ReplayMessage + } + wantBin, _ := hex.DecodeString("adbccbda00000002000000070000000657534a542d58") + tests := []struct { + name string + args args + want []byte + wantErr bool + }{ + { + name: "encodeReplay", + args: args{msg: ReplayMessage{"WSJT-X"}}, + want: wantBin, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := encodeReplay(tt.args.msg) + if (err != nil) != tt.wantErr { + t.Errorf("encodeReplay() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("encodeReplay() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/messages.go b/messages.go index 84947cd..b3654bf 100644 --- a/messages.go +++ b/messages.go @@ -177,6 +177,23 @@ type CloseMessage struct { const closeNum = 6 +/* +When a server starts it may be useful for it to determine the +state of preexisting clients. Sending this message to each +client as it is discovered will cause that client (WSJT-X) to +send a "Decode" message for each decode currently in its "Band +activity" window. + +In only. + +https://sourceforge.net/p/wsjt/wsjtx/ci/8f99fcce/tree/Network/NetworkMessage.hpp#l326 +*/ +type ReplayMessage struct { + Id string `json:"id"` +} + +const replayNum = 7 + /* The decode message is sent when a new decode is completed, in this case the 'New' field is true. diff --git a/server.go b/server.go index cdc4398..bd4b97e 100644 --- a/server.go +++ b/server.go @@ -71,6 +71,13 @@ func (s *Server) Close(msg CloseMessage) error { return err } +// Send a message to WSJT-X to replay QSOs in the Band Activity window. +func (s *Server) Replay(msg ReplayMessage) error { + msgBytes, _ := encodeReplay(msg) + _, err := s.conn.WriteTo(msgBytes, s.remoteAddr) + return err +} + func check(err error) { if err != nil { panic(err)