You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2021/07/06 12:54:36 UTC

[plc4x] branch develop updated: It turns out the KNX discovery used "SendRequest" for the search. This resulted in only one response being handled and the second response being discarded. Refactored the code to use a loop consuming the default-channel, hereby allowing multiple responses.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 2c746dc  It turns out the KNX discovery used "SendRequest" for the search. This resulted in only one response being handled and the second response being discarded. Refactored the code to use a loop consuming the default-channel, hereby allowing multiple responses.
2c746dc is described below

commit 2c746dcc22a2124a9e0be3c3368ba5b5cc2e70aa
Author: cdutz <ch...@c-ware.de>
AuthorDate: Tue Jul 6 14:54:20 2021 +0200

    It turns out the KNX discovery used "SendRequest" for the search. This resulted in only one response being handled and the second response being discarded. Refactored the code to use a loop consuming the default-channel, hereby allowing multiple responses.
---
 plc4go/internal/plc4go/knxnetip/Discoverer.go | 61 ++++++++++++++-------------
 1 file changed, 32 insertions(+), 29 deletions(-)

diff --git a/plc4go/internal/plc4go/knxnetip/Discoverer.go b/plc4go/internal/plc4go/knxnetip/Discoverer.go
index b2fc75e..2df7520 100644
--- a/plc4go/internal/plc4go/knxnetip/Discoverer.go
+++ b/plc4go/internal/plc4go/knxnetip/Discoverer.go
@@ -29,7 +29,6 @@ import (
 	"github.com/apache/plc4x/plc4go/internal/plc4go/spi/transports/udp"
 	"github.com/apache/plc4x/plc4go/internal/plc4go/spi/utils"
 	"github.com/apache/plc4x/plc4go/pkg/plc4go/model"
-	"github.com/rs/zerolog/log"
 	"net"
 	"net/url"
 	"time"
@@ -119,35 +118,39 @@ func (d *Discoverer) Discover(callback func(event model.PlcDiscoveryEvent)) erro
 			discoveryEndpoint := driverModel.NewHPAIDiscoveryEndpoint(
 				driverModel.HostProtocolCode_IPV4_UDP, localAddr, uint16(localAddress.Port))
 			searchRequestMessage := driverModel.NewSearchRequest(discoveryEndpoint)
-			err = codec.SendRequest(
-				searchRequestMessage,
-				func(message interface{}) bool {
-					searchResponse := driverModel.CastSearchResponse(message)
-					// As we can expect multiple responses, we always tell the codec to keep this selector active
-					return searchResponse != nil
-				},
-				func(message interface{}) error {
-					searchResponse := driverModel.CastSearchResponse(message)
-
-					addr := searchResponse.HpaiControlEndpoint.IpAddress.Addr
-					remoteUrl, err := url.Parse(fmt.Sprintf("udp://%d.%d.%d.%d:%d",
-						uint8(addr[0]), uint8(addr[1]), uint8(addr[2]), uint8(addr[3]), searchResponse.HpaiControlEndpoint.IpPort))
-					if err != nil {
-						return err
+			// Send the search request.
+			err = codec.Send(searchRequestMessage)
+			go func() {
+				// Keep on reading responses till the timeout is done.
+				// TODO: Make this configurable
+				for start := time.Now(); time.Since(start) < time.Second*5; {
+					select {
+					case message := <-codec.GetDefaultIncomingMessageChannel():
+						{
+							searchResponse := driverModel.CastSearchResponse(message)
+							if searchResponse != nil {
+								addr := searchResponse.HpaiControlEndpoint.IpAddress.Addr
+								remoteUrl, err := url.Parse(fmt.Sprintf("udp://%d.%d.%d.%d:%d",
+									uint8(addr[0]), uint8(addr[1]), uint8(addr[2]), uint8(addr[3]), searchResponse.HpaiControlEndpoint.IpPort))
+								if err != nil {
+									continue
+								}
+								deviceName := string(bytes.Trim(utils.Int8ArrayToByteArray(
+									searchResponse.DibDeviceInfo.DeviceFriendlyName), "\x00"))
+								discoveryEvent := model.NewPlcDiscoveryEvent(
+									"knxnet-ip", "udp", *remoteUrl, nil, deviceName)
+								// Pass the event back to the callback
+								callback(discoveryEvent)
+							}
+							continue
+						}
+					case <-time.After(time.Second * 1):
+						{
+							continue
+						}
 					}
-					deviceName := string(bytes.Trim(utils.Int8ArrayToByteArray(
-						searchResponse.DibDeviceInfo.DeviceFriendlyName), "\x00"))
-					discoveryEvent := model.NewPlcDiscoveryEvent(
-						"knxnet-ip", "udp", *remoteUrl, nil, deviceName)
-					// Pass the event back to the callback
-					callback(discoveryEvent)
-					return nil
-				},
-				func(err error) error {
-					log.Debug().Err(err).Msg("got timeout waiting for search-response")
-					return nil
-				},
-				time.Second*1)
+				}
+			}()
 		}
 	}
 	return nil