Skip to content

Commit

Permalink
Simplified event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed May 8, 2024
1 parent 808dd62 commit 387e614
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 146 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Added
1. Implemented TCP/IP interface.

### Updated
1. Reworked event handling to use connected UDP socket to send events.

Expand Down
4 changes: 2 additions & 2 deletions NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
- probably just short-circuited byte-by-byte (nibble-by-nibble ?) equals
- or maybe just masks out the most significant nibble ?

2. https://blog.cloudflare.com/everything-you-ever-wanted-to-know-about-udp-sockets-but-were-afraid-to-ask-part-1/

2. https://blog.cloudflare.com/everything-you-ever-wanted-to-know-about-udp-sockets-but-were-afraid-to-ask-part-1
3. https://stackoverflow.com/questions/54360408/docker-container-udp-communication-with-other-hosts
11 changes: 6 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# TODO

- [x] https://github.com/uhppoted/uhppote-simulator/issues/9

- [ ] TCP/IP protocol (cf. https://github.com/uhppoted/uhppote-core/issues/17)
- [x] Rework UDP handling so that `if request.SerialNumber == s.SerialNumber` returns false
- [x] TCP accept socket
- [x] Rework UDP request handling
- [ ] Rework UDP event handling
- [x] Rework UDP event handling
- (?) Remove `Simulator` interface
- [ ] Update docker images, etc
- [ ] CHANGELOG
- [ ] README

- [x] https://github.com/uhppoted/uhppote-simulator/issues/9
- https://stackoverflow.com/questions/54360408/docker-container-udp-communication-with-other-hosts
- https://blog.cloudflare.com/everything-you-ever-wanted-to-know-about-udp-sockets-but-were-afraid-to-ask-part-1/

## TODO

- [ ] Verify fields in listen events/status replies against SDK
Expand Down Expand Up @@ -43,3 +43,4 @@
### Other

1. Integration tests

17 changes: 7 additions & 10 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/uhppoted/uhppote-simulator/log"
"github.com/uhppoted/uhppote-simulator/rest"
"github.com/uhppoted/uhppote-simulator/simulator"
"github.com/uhppoted/uhppote-simulator/simulator/UT0311L04"
)

var debug bool = false
Expand Down Expand Up @@ -80,6 +81,12 @@ 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)
}

UT0311L04.SetOnEvent(g)

go func() {
if err := udpListenAndServe(ctx, udp); err != nil {
errorf("udp", "%v", err)
Expand All @@ -94,16 +101,6 @@ func run(ctx *simulator.Context, udp *net.UDPConn, tcp *net.TCPListener, wait ch
wait <- 0
}()

go func() {
for {
msg := ctx.DeviceList.GetMessage()

if msg.Event {
sendto(bind, msg.Destination, msg.Message)
}
}
}()

go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
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.20240507163420-35508cf9ca2b
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240507163849-65273968c355
github.com/uhppoted/uhppote-core v0.8.9-0.20240508170559-498b633b942d
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240508160756-1c8a037202d9
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ github.com/uhppoted/uhppote-core v0.8.9-0.20240506164953-ac0297e6181a h1:poZuDLo
github.com/uhppoted/uhppote-core v0.8.9-0.20240506164953-ac0297e6181a/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM=
github.com/uhppoted/uhppote-core v0.8.9-0.20240507163420-35508cf9ca2b h1:Icpzi8Vg+k3UsehmJ/RAGScklzaa55cKWGgilsg/KMM=
github.com/uhppoted/uhppote-core v0.8.9-0.20240507163420-35508cf9ca2b/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM=
github.com/uhppoted/uhppote-core v0.8.9-0.20240508154856-915c31b963ec h1:5Ncj4CxC3bmouOn7VuqOIloPhf7v1YofVqZNCOJzhKg=
github.com/uhppoted/uhppote-core v0.8.9-0.20240508154856-915c31b963ec/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM=
github.com/uhppoted/uhppote-core v0.8.9-0.20240508170559-498b633b942d h1:M+6Hxt177luDLWFsuQTqkmk7/kWIAzcC6e+r3EZ4QWY=
github.com/uhppoted/uhppote-core v0.8.9-0.20240508170559-498b633b942d/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 @@ -72,3 +76,5 @@ github.com/uhppoted/uhppoted-lib v0.8.9-0.20240506165508-f3499bacd0ca h1:C2LGzzP
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240506165508-f3499bacd0ca/go.mod h1:ZeXoXSWhMDIP8fGTezEX7bGB/EZM5I9bOqjCf6t+eQ4=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240507163849-65273968c355 h1:1mUioB07UC9p8/qIUAVqdJk/G7LHcGLBryWgJvZBkog=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240507163849-65273968c355/go.mod h1:vL8jQG9himaPioOfp8DI49jvo+Hb9/B2KaEQkmw6ycg=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240508160756-1c8a037202d9 h1:RwyYgHlhB5Prsrq/KSfmhcy2LFCsjk0LDVHzh+Teeds=
github.com/uhppoted/uhppoted-lib v0.8.9-0.20240508160756-1c8a037202d9/go.mod h1:HeGaYNWa+mAj/YGPBkW+DNP6eVPwH2PEhmaqGjL7bSE=
47 changes: 11 additions & 36 deletions simulator/UT0311L04/UTC0311L04.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net"
"os"
"path/filepath"
"reflect"
"time"

"github.com/uhppoted/uhppote-core/messages"
Expand All @@ -23,7 +22,6 @@ type UT0311L04 struct {
file string
compressed bool
touched time.Time
txq chan entities.Message

SerialNumber types.SerialNumber `json:"serial-number"`
IpAddress net.IP `json:"address"`
Expand All @@ -48,6 +46,15 @@ type UT0311L04 struct {
Events entities.EventList `json:"events"`
}

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

func SetOnEvent(handler func(dest *net.UDPAddr, event any)) {
if handler != nil {
onEvent = handler
}
}

func NewUT0311L04(deviceID uint32, dir string, compressed bool) *UT0311L04 {
filename := fmt.Sprintf("%d.json", deviceID)
if compressed {
Expand Down Expand Up @@ -93,10 +100,6 @@ func (s *UT0311L04) FilePath() string {
return s.file
}

func (s *UT0311L04) SetTxQ(txq chan entities.Message) {
s.txq = txq
}

func (s *UT0311L04) Handle(rq messages.Request) (any, error) {
s.touched = time.Now()

Expand Down Expand Up @@ -349,34 +352,6 @@ func (s *UT0311L04) Delete() error {
return nil
}

func (s *UT0311L04) Send(dest *net.UDPAddr, message any) {
if s.txq == nil {
panic(fmt.Sprintf("Device %d: missing TXQ", s.SerialNumber))
}

if s.txq != nil && dest != nil && message != nil && !reflect.ValueOf(message).IsNil() {
s.txq <- entities.Message{
Destination: dest,
Message: message,
Event: false,
}
}
}

func (s *UT0311L04) sendto(dest *net.UDPAddr, message any) {
if s.txq == nil {
panic(fmt.Sprintf("Device %d: missing TXQ", s.SerialNumber))
}

if s.txq != nil && dest != nil && message != nil && !reflect.ValueOf(message).IsNil() {
s.txq <- entities.Message{
Destination: dest,
Message: message,
Event: true,
}
}
}

func saveGZ(filepath string, s *UT0311L04) error {
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
Expand Down Expand Up @@ -449,9 +424,9 @@ func (s *UT0311L04) add(event entities.Event) {
Event: e,
}

s.sendto(s.Listener, &e662)
onEvent(s.Listener, &e662)
} else {
s.sendto(s.Listener, &e)
onEvent(s.Listener, &e)
}
}

Expand Down
4 changes: 0 additions & 4 deletions simulator/UT0311L04/UTC0311L04_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ func testHandle(request messages.Request, expected messages.Response, t *testing
},
})

txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 12345,
IpAddress: net.IPv4(10, 0, 0, 100),
Expand All @@ -511,8 +509,6 @@ func testHandle(request messages.Request, expected messages.Response, t *testing
Cards: cards,
Events: events,
Doors: doors,

txq: txq,
}

if response, err := s.Handle(request); err != nil {
Expand Down
4 changes: 0 additions & 4 deletions simulator/UT0311L04/activate_keypads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

func TestActivateAccessKeypads(t *testing.T) {
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 405419896,
Keypads: entities.Keypads{
Expand All @@ -19,8 +17,6 @@ func TestActivateAccessKeypads(t *testing.T) {
3: entities.KeypadNone,
4: entities.KeypadNone,
},

txq: txq,
}

expected := &messages.ActivateAccessKeypadsResponse{
Expand Down
9 changes: 0 additions & 9 deletions simulator/UT0311L04/get_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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}
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 12345,
Expand All @@ -28,8 +27,6 @@ func TestGetDeviceWithMatchingAddress(t *testing.T) {
Cards: entities.CardList{},
Events: entities.EventList{},
Doors: entities.MakeDoors(),

txq: txq,
}

date, _ := types.DateFromString("2020-12-05")
Expand Down Expand Up @@ -61,7 +58,6 @@ 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}
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 12345,
Expand All @@ -75,8 +71,6 @@ func TestGetDeviceWithAddress0(t *testing.T) {
Cards: entities.CardList{},
Events: entities.EventList{},
Doors: entities.MakeDoors(),

txq: txq,
}

date, _ := types.DateFromString("2020-12-05")
Expand Down Expand Up @@ -108,7 +102,6 @@ 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}
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 12345,
Expand All @@ -122,8 +115,6 @@ func TestGetDeviceWithDifferentAddress(t *testing.T) {
Cards: entities.CardList{},
Events: entities.EventList{},
Doors: entities.MakeDoors(),

txq: txq,
}

request := messages.GetDeviceRequest{
Expand Down
9 changes: 0 additions & 9 deletions simulator/UT0311L04/record_special_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import (
"testing"

"github.com/uhppoted/uhppote-core/messages"
"github.com/uhppoted/uhppote-simulator/entities"
)

func TestRecordSpecialEvents(t *testing.T) {
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 12345,
RecordSpecialEvents: false,

txq: txq,
}

expected := messages.RecordSpecialEventsResponse{
Expand Down Expand Up @@ -44,13 +39,9 @@ func TestRecordSpecialEvents(t *testing.T) {
}

func TestRecordSpecialEventsDisable(t *testing.T) {
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 12345,
RecordSpecialEvents: true,

txq: txq,
}

expected := messages.RecordSpecialEventsResponse{
Expand Down
4 changes: 0 additions & 4 deletions simulator/UT0311L04/restore_default_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
)

func TestRestoreDefaultParameters(t *testing.T) {
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 405419896,
IpAddress: net.IPv4(192, 168, 1, 100),
Expand All @@ -22,8 +20,6 @@ func TestRestoreDefaultParameters(t *testing.T) {
Listener: &net.UDPAddr{IP: net.IPv4(192, 168, 1, 100), Port: 60001},

Doors: entities.MakeDoors(),

txq: txq,
}

s.Doors.SetControlState(1, entities.NormallyOpen)
Expand Down
4 changes: 0 additions & 4 deletions simulator/UT0311L04/set_door_passcodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import (
)

func TestSetDoorPasscodes(t *testing.T) {
txq := make(chan entities.Message, 8)

s := UT0311L04{
SerialNumber: 405419896,
Doors: entities.MakeDoors(),

txq: txq,
}

expected := struct {
Expand Down
Loading

0 comments on commit 387e614

Please sign in to comment.