Skip to content

Commit

Permalink
Reworked event sendto to use connected UDP socket (cf. #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Apr 19, 2024
1 parent 073a237 commit 8362398
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ publish: release
gh release create "$(VERSION)" "./dist/uhppote-simulator_$(VERSION).tar.gz" "./dist/uhppote-simulator_$(VERSION).zip" --draft --prerelease --title "$(VERSION)-beta" --notes-file release-notes.md

debug:
python3 cli.py list-controllers
curl -X POST "http://127.0.0.1:8765/uhppote/simulator/706050403/swipe" -H "accept: application/json" -H "Content-Type: application/json" -d '{"door":1, "card-number":10058400,"direction":1}'

delve: build
# dlv test github.com/uhppoted/uhppote-simulator/simulator/UT0311L04 -- run TestCheckTimeProfileInTimeSegmentWithOffset
Expand Down
3 changes: 3 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
- 1999999 => 0x1E847F (stored as 0x0E847F)
- 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/

15 changes: 1 addition & 14 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
# TODO

- [x] [Prebuilt Docker containers](https://github.com/uhppoted/uhppoted/issues/47)
- [x] Dockerfile
- [x] ghcr.io
- [x] compose.yml
- [x] README
- [x] CHANGELOG
- [x] _latest_
- [x] Update uhppote to use simulator docker

- [x] [Startup fails with compressed controller](https://github.com/uhppoted/uhppote-simulator/issues/12)
- [x] [`restore-default-parameters`](https://github.com/uhppoted/uhppoted/issues/48)
- [x] [Document the HTTP requests](https://github.com/uhppoted/uhppote-simulator/issues/11)

- [ ] https://github.com/uhppoted/uhppote-simulator/issues/9
- Docker weirdness with events
- Stops forwarding UDP event messages
- https://stackoverflow.com/questions/54360408/docker-container-udp-communication-with-other-hosts
- https://serverfault.com/questions/866830/udp-traffic-not-forwarded-from-docker-containers-docker-host
- https://blog.cloudflare.com/everything-you-ever-wanted-to-know-about-udp-sockets-but-were-afraid-to-ask-part-1/

## TODO

Expand Down
48 changes: 45 additions & 3 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func Simulate(ctx *simulator.Context, dbg bool) {
}

func run(ctx *simulator.Context, connection *net.UDPConn, wait chan int) {
bind, err := net.ResolveUDPAddr("udp4", ctx.BindAddress)
if err != nil {
log.Errorf("failed to resolve UDP bind address [%v]", err)
return
}

go func() {
err := listenAndServe(ctx, connection)
if err != nil {
Expand All @@ -61,7 +67,12 @@ func run(ctx *simulator.Context, connection *net.UDPConn, wait chan int) {
go func() {
for {
msg := ctx.DeviceList.GetMessage()
send(connection, msg.Destination, msg.Message)

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

Expand Down Expand Up @@ -132,21 +143,52 @@ func receive(c *net.UDPConn) ([]byte, *net.UDPAddr, error) {
return request[:N], remote, nil
}

func send(c *net.UDPConn, dest *net.UDPAddr, message interface{}) {
func send(c *net.UDPConn, dest *net.UDPAddr, message any) {
msg, err := codec.Marshal(message)
if err != nil {
log.Errorf("%v", err)
return
}

N, err := c.WriteTo(msg, dest)
N, err := c.WriteToUDP(msg, dest)
if err != nil {
log.Errorf("failed to write to UDP socket [%v]", err)
} else if debug {
log.Infof("sent %v bytes to %v\n%s", N, dest, dump(msg[0:N], " ... "))
}
}

func sendto(bind *net.UDPAddr, dest *net.UDPAddr, message any) {
var addr *net.UDPAddr

if bind != nil && bind.IP != nil {
addr = &net.UDPAddr{
IP: bind.IP.To4(),
Port: 0,
Zone: bind.Zone,
}
}

msg, err := codec.Marshal(message)
if err != nil {
log.Errorf("%v", err)
return
}

if c, err := net.DialUDP("udp4", addr, dest); err != nil {
log.Errorf("failed to create UDP event socket [%v]", err)
} else {
defer c.Close()

N, err := c.Write(msg)
if err != nil {
log.Errorf("failed to write to UDP socket [%v]", err)
} else if debug {
log.Infof("sent %v bytes to %v\n%s", N, dest, dump(msg[0:N], " ... "))
}
}
}

func dump(m []byte, prefix string) string {
return regexp.MustCompile("(?m)^(.*)").ReplaceAllString(hex.Dump(m), prefix+"$1")
}
1 change: 1 addition & 0 deletions entities/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import (
type Message struct {
Destination *net.UDPAddr
Message interface{}
Event bool
}
22 changes: 19 additions & 3 deletions simulator/UT0311L04/UTC0311L04.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (s *UT0311L04) Delete() error {
return nil
}

func (s *UT0311L04) send(dest *net.UDPAddr, message interface{}) {
func (s *UT0311L04) send(dest *net.UDPAddr, message any) {
if s.txq == nil {
panic(fmt.Sprintf("Device %d: missing TXQ", s.SerialNumber))
}
Expand All @@ -353,6 +353,21 @@ func (s *UT0311L04) send(dest *net.UDPAddr, message interface{}) {
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,
}
}
}
Expand Down Expand Up @@ -423,14 +438,15 @@ func (s *UT0311L04) add(event entities.Event) {
Direction: event.Direction,
}

// ... firmware 6.62 had a slightly different format
if fmt.Sprintf("%v", s.Version) == "6.62" {
e662 := messages.EventV6_62{
Event: e,
}

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

Expand Down

0 comments on commit 8362398

Please sign in to comment.