Skip to content

Commit

Permalink
Reworked set-listener to use netip.AddrPort
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed May 27, 2024
1 parent 258749a commit 9a76bdc
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 38 deletions.
7 changes: 5 additions & 2 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"fmt"
"net"
"net/netip"
"os"
"os/signal"
"reflect"
Expand Down Expand Up @@ -81,8 +82,10 @@ func run(ctx *simulator.Context, udp *net.UDPConn, tcp *net.TCPListener, wait ch
return
}

g := func(dest *net.UDPAddr, event any) {
sendto(bind, dest, event)
g := func(dest netip.AddrPort, event any) {
if dest.IsValid() {
sendto(bind, net.UDPAddrFromAddrPort(dest), event)
}
}

UT0311L04.SetOnEvent(g)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/uhppoted/uhppote-simulator
go 1.22

require (
github.com/uhppoted/uhppote-core v0.8.9-0.20240524165731-f11a293bdd8d
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240524171653-86bb80223c95
github.com/uhppoted/uhppote-core v0.8.9-0.20240527172601-d5317b598bb8
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240527174018-4ff1fd4a0224
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/uhppoted/uhppote-core v0.8.9-0.20240522152700-be733576da28 h1:sDat2im
github.com/uhppoted/uhppote-core v0.8.9-0.20240522152700-be733576da28/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM=
github.com/uhppoted/uhppote-core v0.8.9-0.20240524165731-f11a293bdd8d h1:3i+zWCiQpGN0SXlDHDG2RzBsW1/vzL1sO39gmKbyv88=
github.com/uhppoted/uhppote-core v0.8.9-0.20240524165731-f11a293bdd8d/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM=
github.com/uhppoted/uhppote-core v0.8.9-0.20240527172601-d5317b598bb8 h1:54HfgoJgeDfMDJW2cKF/QDy38dh1qKxv9SMTxVlVOVk=
github.com/uhppoted/uhppote-core v0.8.9-0.20240527172601-d5317b598bb8/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM=
github.com/uhppoted/uhppoted-lib v0.8.5 h1:X5RarX5QMRGJvOwPk8ceowrH8k6Ow3t+TraWmhYO950=
github.com/uhppoted/uhppoted-lib v0.8.5/go.mod h1:euK2cLO934w0GDgvvnWMjYNtZ29K1xSMrMse6THOzDQ=
github.com/uhppoted/uhppoted-lib v0.8.6-0.20230714155133-57e68f2d1360 h1:XaVqmMmjwskTrX6iCdUl2I4ezrvuoA/yhLzf/455E10=
Expand Down Expand Up @@ -114,3 +116,5 @@ github.com/uhppoted/uhppoted-lib v0.8.9-0.20240522155930-ecbe19a67d5b h1:RPpj0UU
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240522155930-ecbe19a67d5b/go.mod h1:BPTNChylhkDutszdOzQ4DFq6JYFLv2yNcGljg9FkWUg=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240524171653-86bb80223c95 h1:XRdwZQUNT0r3zzwjUNOmbsC7Cqc8o0CGXFOztpqMWHQ=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240524171653-86bb80223c95/go.mod h1:X60HhO4iKqIw+J+r0Rh4OkAzpMbhLzo+7QELXKDQowM=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240527174018-4ff1fd4a0224 h1:DYkZ77CR3eVcfTatc9JLSPyVQEcOviJbA2lysPjbVSI=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240527174018-4ff1fd4a0224/go.mod h1:3CvWWsUiXmFh4jTzF+NXeWTMeaHiqkIDLOxoKQ0iJd8=
176 changes: 162 additions & 14 deletions simulator/UT0311L04/UTC0311L04.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net"
"net/netip"
"os"
"path/filepath"
"time"
Expand All @@ -33,7 +34,7 @@ type UT0311L04 struct {
TimeOffset entities.Offset `json:"offset"`
Doors entities.Doors `json:"doors"`
Keypads entities.Keypads `json:"keypads"`
Listener *net.UDPAddr `json:"listener"`
Listener netip.AddrPort `json:"listener"`
RecordSpecialEvents bool `json:"record-special-events"`
PCControl bool `json:"pc-control"`
SystemError uint8 `json:"system-error"`
Expand All @@ -46,10 +47,10 @@ type UT0311L04 struct {
Events entities.EventList `json:"events"`
}

var onEvent = func(dest *net.UDPAddr, event any) {
var onEvent = func(dest netip.AddrPort, event any) {
}

func SetOnEvent(handler func(dest *net.UDPAddr, event any)) {
func SetOnEvent(handler func(dest netip.AddrPort, event any)) {
if handler != nil {
onEvent = handler
}
Expand Down Expand Up @@ -281,20 +282,91 @@ func loadGZ(filepath string) (*UT0311L04, error) {
return nil, err
}

simulator := UT0311L04{
object := struct {
SerialNumber types.SerialNumber `json:"serial-number"`
IpAddress net.IP `json:"address"`
SubnetMask net.IP `json:"subnet"`
Gateway net.IP `json:"gateway"`
MacAddress types.MacAddress `json:"MAC"`
Version types.Version `json:"version"`
Released *ReleaseDate `json:"released"`
TimeOffset entities.Offset `json:"offset"`
Doors entities.Doors `json:"doors"`
Keypads entities.Keypads `json:"keypads"`
Listener json.RawMessage `json:"listener"`
RecordSpecialEvents bool `json:"record-special-events"`
PCControl bool `json:"pc-control"`
SystemError uint8 `json:"system-error"`
SequenceId uint32 `json:"sequence-id"`
SpecialInfo uint8 `json:"special-info"`
InputState uint8 `json:"input-state"`
TimeProfiles entities.TimeProfiles `json:"time-profiles,omitempty"`
TaskList json.RawMessage `json:"tasklist,omitempty"`
Cards entities.CardList `json:"cards"`
Events entities.EventList `json:"events"`
}{
Released: DefaultReleaseDate(),
Doors: entities.MakeDoors(),
Keypads: entities.MakeKeypads(),
TimeProfiles: entities.TimeProfiles{},
}

if err = json.Unmarshal(buffer, &simulator); err != nil {
if err = json.Unmarshal(buffer, &object); err != nil {
return nil, err
}

simulator.file = filepath
simulator.compressed = true
simulator.touched = time.Now()
// ... unmarshal event listener variants
var listener = netip.AddrPort{}
var addrPort netip.AddrPort
var udpAddr net.UDPAddr

if err := json.Unmarshal(object.Listener, &addrPort); err == nil {
listener = addrPort
} else if err := json.Unmarshal(object.Listener, &udpAddr); err == nil {
listener = udpAddr.AddrPort()
}

// ... unmarshal tasklist
tasklist := struct {
Tasks []types.Task `json:"tasks"`
}{
Tasks: []types.Task{},
}

if err := json.Unmarshal(object.TaskList, &tasklist); err != nil {
warnf(object.SerialNumber, "error loading tasklist (%v)", err)
}

// ... initialise simulator
simulator := UT0311L04{
file: filepath,
compressed: true,
touched: time.Now(),

SerialNumber: object.SerialNumber,
IpAddress: object.IpAddress,
SubnetMask: object.SubnetMask,
Gateway: object.Gateway,
MacAddress: object.MacAddress,
Version: object.Version,
Released: object.Released,
TimeOffset: object.TimeOffset,
Doors: object.Doors,
Keypads: object.Keypads,
Listener: listener,
RecordSpecialEvents: object.RecordSpecialEvents,
PCControl: object.PCControl,
SystemError: object.SystemError,
SequenceId: object.SequenceId,
SpecialInfo: object.SpecialInfo,
InputState: object.InputState,
TimeProfiles: object.TimeProfiles,
TaskList: entities.TaskList{
Tasks: tasklist.Tasks,
},
Cards: object.Cards,
Events: object.Events,
}

return &simulator, nil
}
Expand All @@ -305,21 +377,91 @@ func load(filepath string) (*UT0311L04, error) {
return nil, err
}

simulator := UT0311L04{
object := struct {
SerialNumber types.SerialNumber `json:"serial-number"`
IpAddress net.IP `json:"address"`
SubnetMask net.IP `json:"subnet"`
Gateway net.IP `json:"gateway"`
MacAddress types.MacAddress `json:"MAC"`
Version types.Version `json:"version"`
Released *ReleaseDate `json:"released"`
TimeOffset entities.Offset `json:"offset"`
Doors entities.Doors `json:"doors"`
Keypads entities.Keypads `json:"keypads"`
Listener json.RawMessage `json:"listener"`
RecordSpecialEvents bool `json:"record-special-events"`
PCControl bool `json:"pc-control"`
SystemError uint8 `json:"system-error"`
SequenceId uint32 `json:"sequence-id"`
SpecialInfo uint8 `json:"special-info"`
InputState uint8 `json:"input-state"`
TimeProfiles entities.TimeProfiles `json:"time-profiles,omitempty"`
TaskList json.RawMessage `json:"tasklist,omitempty"`
Cards entities.CardList `json:"cards"`
Events entities.EventList `json:"events"`
}{
Released: DefaultReleaseDate(),
Doors: entities.MakeDoors(),
Keypads: entities.MakeKeypads(),
TimeProfiles: entities.TimeProfiles{},
}

err = json.Unmarshal(bytes, &simulator)
if err != nil {
if err = json.Unmarshal(bytes, &object); err != nil {
return nil, err
}

simulator.file = filepath
simulator.compressed = false
simulator.touched = time.Now()
// ... unmarshal event listener variants
var listener = netip.AddrPort{}
var addrPort netip.AddrPort
var udpAddr net.UDPAddr

if err := json.Unmarshal(object.Listener, &addrPort); err == nil {
listener = addrPort
} else if err := json.Unmarshal(object.Listener, &udpAddr); err == nil {
listener = udpAddr.AddrPort()
}

// ... unmarshal tasklist
tasklist := struct {
Tasks []types.Task `json:"tasks"`
}{
Tasks: []types.Task{},
}

if err := json.Unmarshal(object.TaskList, &tasklist); err != nil {
warnf(object.SerialNumber, "error loading tasklist (%v)", err)
}

// ... initialise simulator
simulator := UT0311L04{
file: filepath,
compressed: false,
touched: time.Now(),

SerialNumber: object.SerialNumber,
IpAddress: object.IpAddress,
SubnetMask: object.SubnetMask,
Gateway: object.Gateway,
MacAddress: object.MacAddress,
Version: object.Version,
Released: object.Released,
TimeOffset: object.TimeOffset,
Doors: object.Doors,
Keypads: object.Keypads,
Listener: listener,
RecordSpecialEvents: object.RecordSpecialEvents,
PCControl: object.PCControl,
SystemError: object.SystemError,
SequenceId: object.SequenceId,
SpecialInfo: object.SpecialInfo,
InputState: object.InputState,
TimeProfiles: object.TimeProfiles,
TaskList: entities.TaskList{
Tasks: tasklist.Tasks,
},
Cards: object.Cards,
Events: object.Events,
}

return &simulator, nil
}
Expand Down Expand Up @@ -447,3 +589,9 @@ func (s *UT0311L04) relays() uint8 {

return state
}

func warnf(tag any, format string, args ...any) {
f := fmt.Sprintf("%-10v %v", tag, format)

log.Warnf(f, args...)
}
8 changes: 4 additions & 4 deletions simulator/UT0311L04/UTC0311L04_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package UT0311L04

import (
"net"
"net/netip"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -263,8 +264,7 @@ func TestHandleGetDoorControlState(t *testing.T) {
func TestHandleSetListener(t *testing.T) {
request := messages.SetListenerRequest{
SerialNumber: 12345,
Address: net.IPv4(10, 0, 0, 1),
Port: 43210,
AddrPort: netip.MustParseAddrPort("10.0.0.1:43210"),
}

response := messages.SetListenerResponse{
Expand Down Expand Up @@ -378,7 +378,7 @@ func testHandle(request messages.Request, expected messages.Response, t *testing
from, _ := types.DateFromString("2019-01-01")
to, _ := types.DateFromString("2019-12-31")
timestamp := types.DateTime(time.Date(2019, time.August, 1, 12, 34, 56, 0, time.Local))
listener := net.UDPAddr{IP: net.IPv4(10, 0, 0, 10), Port: 43210}
listener := netip.MustParseAddrPort("10.0.0.10:43210")

doors := entities.MakeDoors()

Expand Down Expand Up @@ -505,7 +505,7 @@ func testHandle(request messages.Request, expected messages.Response, t *testing
Gateway: net.IPv4(10, 0, 0, 1),
MacAddress: types.MacAddress(MAC),
Version: 9876,
Listener: &listener,
Listener: listener,
Cards: cards,
Events: events,
Doors: doors,
Expand Down
13 changes: 7 additions & 6 deletions simulator/UT0311L04/get_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package UT0311L04

import (
"net"
"net/netip"
"reflect"
"testing"

Expand All @@ -13,7 +14,7 @@ import (
func TestGetDeviceWithMatchingAddress(t *testing.T) {
MAC, _ := net.ParseMAC("00:66:19:39:55:2d")
released, _ := types.DateFromString("2020-12-05")
listener := net.UDPAddr{IP: net.IPv4(10, 0, 0, 10), Port: 43210}
listener := netip.MustParseAddrPort("10.0.0.10:43210")

s := UT0311L04{
SerialNumber: 12345,
Expand All @@ -23,7 +24,7 @@ func TestGetDeviceWithMatchingAddress(t *testing.T) {
MacAddress: types.MacAddress(MAC),
Version: 9876,
Released: (*ReleaseDate)(&released),
Listener: &listener,
Listener: listener,
Cards: entities.CardList{},
Events: entities.EventList{},
Doors: entities.MakeDoors(),
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestGetDeviceWithMatchingAddress(t *testing.T) {
func TestGetDeviceWithAddress0(t *testing.T) {
MAC, _ := net.ParseMAC("00:66:19:39:55:2d")
released, _ := types.DateFromString("2020-12-05")
listener := net.UDPAddr{IP: net.IPv4(10, 0, 0, 10), Port: 43210}
listener := netip.MustParseAddrPort("10.0.0.10:43210")

s := UT0311L04{
SerialNumber: 12345,
Expand All @@ -67,7 +68,7 @@ func TestGetDeviceWithAddress0(t *testing.T) {
MacAddress: types.MacAddress(MAC),
Version: 9876,
Released: (*ReleaseDate)(&released),
Listener: &listener,
Listener: listener,
Cards: entities.CardList{},
Events: entities.EventList{},
Doors: entities.MakeDoors(),
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestGetDeviceWithAddress0(t *testing.T) {
func TestGetDeviceWithDifferentAddress(t *testing.T) {
MAC, _ := net.ParseMAC("00:66:19:39:55:2d")
released, _ := types.DateFromString("2020-12-05")
listener := net.UDPAddr{IP: net.IPv4(10, 0, 0, 10), Port: 43210}
listener := netip.MustParseAddrPort("10.0.0.10:43210")

s := UT0311L04{
SerialNumber: 12345,
Expand All @@ -111,7 +112,7 @@ func TestGetDeviceWithDifferentAddress(t *testing.T) {
MacAddress: types.MacAddress(MAC),
Version: 9876,
Released: (*ReleaseDate)(&released),
Listener: &listener,
Listener: listener,
Cards: entities.CardList{},
Events: entities.EventList{},
Doors: entities.MakeDoors(),
Expand Down
7 changes: 4 additions & 3 deletions simulator/UT0311L04/get_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func (s *UT0311L04) getListener(request *messages.GetListenerRequest) (*messages
address := net.IPv4(0, 0, 0, 0)
port := uint16(0)

if s.Listener != nil {
address = s.Listener.IP
port = uint16(s.Listener.Port)
if s.Listener.IsValid() {
addr := s.Listener.Addr().As4()
address = net.IPv4(addr[0], addr[1], addr[2], addr[3])
port = s.Listener.Port()
}

response := messages.GetListenerResponse{
Expand Down
Loading

0 comments on commit 9a76bdc

Please sign in to comment.