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 2022/01/25 15:22:08 UTC

[plc4x] branch develop updated: refactor(plc4go/drivers): Made all driver's Connections implement the spi.PlcConnection interface (so we can use them in the connection pool).

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 20be7b2  refactor(plc4go/drivers): Made all driver's Connections implement the spi.PlcConnection interface (so we can use them in the connection pool).
20be7b2 is described below

commit 20be7b27f3762d2a20d045c4948930dda73de41c
Author: cdutz <ch...@c-ware.de>
AuthorDate: Tue Jan 25 16:22:01 2022 +0100

    refactor(plc4go/drivers): Made all driver's Connections implement the spi.PlcConnection interface (so we can use them in the connection pool).
---
 plc4go/internal/plc4go/ads/Connection.go      | 21 ++++++++++++++++++++-
 plc4go/internal/plc4go/ads/Driver.go          |  2 +-
 plc4go/internal/plc4go/bacnetip/Connection.go | 21 ++++++++++++++++++++-
 plc4go/internal/plc4go/bacnetip/Driver.go     |  2 +-
 plc4go/internal/plc4go/eip/Connection.go      | 21 ++++++++++++++++++++-
 plc4go/internal/plc4go/eip/Driver.go          |  2 +-
 plc4go/internal/plc4go/knxnetip/Connection.go | 20 ++++++++++++++++++++
 plc4go/internal/plc4go/modbus/Connection.go   | 20 ++++++++++++++++++++
 plc4go/internal/plc4go/s7/Connection.go       | 22 +++++++++++++++++++++-
 plc4go/internal/plc4go/s7/Driver.go           |  2 +-
 10 files changed, 125 insertions(+), 8 deletions(-)

diff --git a/plc4go/internal/plc4go/ads/Connection.go b/plc4go/internal/plc4go/ads/Connection.go
index 19f2357..c8c2cc4 100644
--- a/plc4go/internal/plc4go/ads/Connection.go
+++ b/plc4go/internal/plc4go/ads/Connection.go
@@ -37,9 +37,11 @@ type Connection struct {
 	configuration      Configuration
 	reader             *Reader
 	writer             *Writer
+	connectionId       string
+	tracer             *spi.Tracer
 }
 
-func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, fieldHandler spi.PlcFieldHandler) (*Connection, error) {
+func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, fieldHandler spi.PlcFieldHandler, options map[string][]string) (*Connection, error) {
 	reader := *NewReader(
 		messageCodec,
 		configuration.targetAmsNetId,
@@ -66,6 +68,11 @@ func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, f
 		reader: &reader,
 		writer: &writer,
 	}
+	if traceEnabledOption, ok := options["traceEnabled"]; ok {
+		if len(traceEnabledOption) == 1 {
+			connection.tracer = spi.NewTracer(connection.connectionId)
+		}
+	}
 	connection.DefaultConnection = _default.NewDefaultConnection(connection,
 		_default.WithPlcFieldHandler(fieldHandler),
 		_default.WithPlcValueHandler(NewValueHandler()),
@@ -73,6 +80,18 @@ func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, f
 	return connection, nil
 }
 
+func (m *Connection) GetConnectionId() string {
+	return m.connectionId
+}
+
+func (m *Connection) IsTraceEnabled() bool {
+	return m.tracer != nil
+}
+
+func (m *Connection) GetTracer() *spi.Tracer {
+	return m.tracer
+}
+
 func (m *Connection) GetConnection() plc4go.PlcConnection {
 	return m
 }
diff --git a/plc4go/internal/plc4go/ads/Driver.go b/plc4go/internal/plc4go/ads/Driver.go
index 541da6f..b581e16 100644
--- a/plc4go/internal/plc4go/ads/Driver.go
+++ b/plc4go/internal/plc4go/ads/Driver.go
@@ -74,7 +74,7 @@ func (m *Driver) GetConnection(transportUrl url.URL, transports map[string]trans
 	}
 
 	// Create the new connection
-	connection, err := NewConnection(codec, configuration, m.GetPlcFieldHandler())
+	connection, err := NewConnection(codec, configuration, m.GetPlcFieldHandler(), options)
 	if err != nil {
 		ch := make(chan plc4go.PlcConnectionConnectResult)
 		go func() {
diff --git a/plc4go/internal/plc4go/bacnetip/Connection.go b/plc4go/internal/plc4go/bacnetip/Connection.go
index b453327..7af6e47 100644
--- a/plc4go/internal/plc4go/bacnetip/Connection.go
+++ b/plc4go/internal/plc4go/bacnetip/Connection.go
@@ -33,12 +33,19 @@ type Connection struct {
 	_default.DefaultConnection
 	messageCodec spi.MessageCodec
 	subscribers  []*Subscriber
+	connectionId string
+	tracer       *spi.Tracer
 }
 
-func NewConnection(messageCodec spi.MessageCodec, fieldHandler spi.PlcFieldHandler) *Connection {
+func NewConnection(messageCodec spi.MessageCodec, fieldHandler spi.PlcFieldHandler, options map[string][]string) *Connection {
 	connection := &Connection{
 		messageCodec: messageCodec,
 	}
+	if traceEnabledOption, ok := options["traceEnabled"]; ok {
+		if len(traceEnabledOption) == 1 {
+			connection.tracer = spi.NewTracer(connection.connectionId)
+		}
+	}
 	connection.DefaultConnection = _default.NewDefaultConnection(connection,
 		_default.WithPlcFieldHandler(fieldHandler),
 		_default.WithPlcValueHandler(NewValueHandler()),
@@ -46,6 +53,18 @@ func NewConnection(messageCodec spi.MessageCodec, fieldHandler spi.PlcFieldHandl
 	return connection
 }
 
+func (m *Connection) GetConnectionId() string {
+	return m.connectionId
+}
+
+func (m *Connection) IsTraceEnabled() bool {
+	return m.tracer != nil
+}
+
+func (m *Connection) GetTracer() *spi.Tracer {
+	return m.tracer
+}
+
 func (c *Connection) Connect() <-chan plc4go.PlcConnectionConnectResult {
 	log.Trace().Msg("Connecting")
 	ch := make(chan plc4go.PlcConnectionConnectResult)
diff --git a/plc4go/internal/plc4go/bacnetip/Driver.go b/plc4go/internal/plc4go/bacnetip/Driver.go
index 88cc46a..aa5f2fd 100644
--- a/plc4go/internal/plc4go/bacnetip/Driver.go
+++ b/plc4go/internal/plc4go/bacnetip/Driver.go
@@ -71,7 +71,7 @@ func (m *Driver) GetConnection(transportUrl url.URL, transports map[string]trans
 	log.Debug().Msgf("working with codec %#v", codec)
 
 	// Create the new connection
-	connection := NewConnection(codec, m.GetPlcFieldHandler())
+	connection := NewConnection(codec, m.GetPlcFieldHandler(), options)
 	log.Debug().Msg("created connection, connecting now")
 	return connection.Connect()
 }
diff --git a/plc4go/internal/plc4go/eip/Connection.go b/plc4go/internal/plc4go/eip/Connection.go
index 53ca872..8742481 100644
--- a/plc4go/internal/plc4go/eip/Connection.go
+++ b/plc4go/internal/plc4go/eip/Connection.go
@@ -40,15 +40,22 @@ type Connection struct {
 	tm            *spi.RequestTransactionManager
 	sessionHandle uint32
 	senderContext []uint8
+	connectionId  string
+	tracer        *spi.Tracer
 }
 
-func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, driverContext DriverContext, fieldHandler spi.PlcFieldHandler, tm *spi.RequestTransactionManager) *Connection {
+func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, driverContext DriverContext, fieldHandler spi.PlcFieldHandler, tm *spi.RequestTransactionManager, options map[string][]string) *Connection {
 	connection := &Connection{
 		messageCodec:  messageCodec,
 		configuration: configuration,
 		driverContext: driverContext,
 		tm:            tm,
 	}
+	if traceEnabledOption, ok := options["traceEnabled"]; ok {
+		if len(traceEnabledOption) == 1 {
+			connection.tracer = spi.NewTracer(connection.connectionId)
+		}
+	}
 	connection.DefaultConnection = _default.NewDefaultConnection(connection,
 		_default.WithPlcFieldHandler(fieldHandler),
 		_default.WithPlcValueHandler(NewValueHandler()),
@@ -56,6 +63,18 @@ func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, d
 	return connection
 }
 
+func (m *Connection) GetConnectionId() string {
+	return m.connectionId
+}
+
+func (m *Connection) IsTraceEnabled() bool {
+	return m.tracer != nil
+}
+
+func (m *Connection) GetTracer() *spi.Tracer {
+	return m.tracer
+}
+
 func (m *Connection) GetConnection() plc4go.PlcConnection {
 	return m
 }
diff --git a/plc4go/internal/plc4go/eip/Driver.go b/plc4go/internal/plc4go/eip/Driver.go
index 15d930a..6f24cb3 100644
--- a/plc4go/internal/plc4go/eip/Driver.go
+++ b/plc4go/internal/plc4go/eip/Driver.go
@@ -96,7 +96,7 @@ func (m *Driver) GetConnection(transportUrl url.URL, transports map[string]trans
 	driverContext.awaitDisconnectComplete = m.awaitDisconnectComplete
 
 	// Create the new connection
-	connection := NewConnection(codec, configuration, driverContext, m.GetPlcFieldHandler(), &m.tm)
+	connection := NewConnection(codec, configuration, driverContext, m.GetPlcFieldHandler(), &m.tm, options)
 	log.Debug().Msg("created connection, connecting now")
 	return connection.Connect()
 }
diff --git a/plc4go/internal/plc4go/knxnetip/Connection.go b/plc4go/internal/plc4go/knxnetip/Connection.go
index 4996981..83e6847 100644
--- a/plc4go/internal/plc4go/knxnetip/Connection.go
+++ b/plc4go/internal/plc4go/knxnetip/Connection.go
@@ -130,6 +130,9 @@ type Connection struct {
 
 	// indicates if the tunneling requests loop is running
 	handleTunnelingRequests bool
+
+	connectionId string
+	tracer       *spi.Tracer
 }
 
 func (m *Connection) String() string {
@@ -182,6 +185,11 @@ func NewConnection(transportInstance transports.TransportInstance, options map[s
 	}
 	connection.connectionTtl = connection.defaultTtl * 2
 
+	if traceEnabledOption, ok := options["traceEnabled"]; ok {
+		if len(traceEnabledOption) == 1 {
+			connection.tracer = spi.NewTracer(connection.connectionId)
+		}
+	}
 	// If a building key was provided, save that in a dedicated variable
 	if buildingKey, ok := options["buildingKey"]; ok {
 		bc, err := hex.DecodeString(buildingKey[0])
@@ -193,6 +201,18 @@ func NewConnection(transportInstance transports.TransportInstance, options map[s
 	return connection
 }
 
+func (m *Connection) GetConnectionId() string {
+	return m.connectionId
+}
+
+func (m *Connection) IsTraceEnabled() bool {
+	return m.tracer != nil
+}
+
+func (m *Connection) GetTracer() *spi.Tracer {
+	return m.tracer
+}
+
 func (m *Connection) Connect() <-chan plc4go.PlcConnectionConnectResult {
 	result := make(chan plc4go.PlcConnectionConnectResult)
 	sendResult := func(connection plc4go.PlcConnection, err error) {
diff --git a/plc4go/internal/plc4go/modbus/Connection.go b/plc4go/internal/plc4go/modbus/Connection.go
index a92700d..0d26174 100644
--- a/plc4go/internal/plc4go/modbus/Connection.go
+++ b/plc4go/internal/plc4go/modbus/Connection.go
@@ -39,6 +39,9 @@ type Connection struct {
 	messageCodec       spi.MessageCodec
 	options            map[string][]string
 	requestInterceptor interceptors.RequestInterceptor
+
+	connectionId string
+	tracer       *spi.Tracer
 }
 
 func NewConnection(unitIdentifier uint8, messageCodec spi.MessageCodec, options map[string][]string, fieldHandler spi.PlcFieldHandler) *Connection {
@@ -53,6 +56,11 @@ func NewConnection(unitIdentifier uint8, messageCodec spi.MessageCodec, options
 			internalModel.NewDefaultPlcWriteResponse,
 		),
 	}
+	if traceEnabledOption, ok := options["traceEnabled"]; ok {
+		if len(traceEnabledOption) == 1 {
+			connection.tracer = spi.NewTracer(connection.connectionId)
+		}
+	}
 	connection.DefaultConnection = _default.NewDefaultConnection(connection,
 		_default.WithDefaultTtl(time.Second*5),
 		_default.WithPlcFieldHandler(fieldHandler),
@@ -61,6 +69,18 @@ func NewConnection(unitIdentifier uint8, messageCodec spi.MessageCodec, options
 	return connection
 }
 
+func (m *Connection) GetConnectionId() string {
+	return m.connectionId
+}
+
+func (m *Connection) IsTraceEnabled() bool {
+	return m.tracer != nil
+}
+
+func (m *Connection) GetTracer() *spi.Tracer {
+	return m.tracer
+}
+
 func (m *Connection) GetConnection() plc4go.PlcConnection {
 	return m
 }
diff --git a/plc4go/internal/plc4go/s7/Connection.go b/plc4go/internal/plc4go/s7/Connection.go
index 39ceeb1..9ebe67e 100644
--- a/plc4go/internal/plc4go/s7/Connection.go
+++ b/plc4go/internal/plc4go/s7/Connection.go
@@ -59,9 +59,12 @@ type Connection struct {
 	configuration Configuration
 	driverContext DriverContext
 	tm            *spi.RequestTransactionManager
+
+	connectionId string
+	tracer       *spi.Tracer
 }
 
-func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, driverContext DriverContext, fieldHandler spi.PlcFieldHandler, tm *spi.RequestTransactionManager) *Connection {
+func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, driverContext DriverContext, fieldHandler spi.PlcFieldHandler, tm *spi.RequestTransactionManager, options map[string][]string) *Connection {
 	connection := &Connection{
 		tpduGenerator: TpduGenerator{currentTpduId: 10},
 		messageCodec:  messageCodec,
@@ -69,6 +72,11 @@ func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, d
 		driverContext: driverContext,
 		tm:            tm,
 	}
+	if traceEnabledOption, ok := options["traceEnabled"]; ok {
+		if len(traceEnabledOption) == 1 {
+			connection.tracer = spi.NewTracer(connection.connectionId)
+		}
+	}
 	connection.DefaultConnection = _default.NewDefaultConnection(connection,
 		_default.WithPlcFieldHandler(fieldHandler),
 		_default.WithPlcValueHandler(NewValueHandler()),
@@ -76,6 +84,18 @@ func NewConnection(messageCodec spi.MessageCodec, configuration Configuration, d
 	return connection
 }
 
+func (m *Connection) GetConnectionId() string {
+	return m.connectionId
+}
+
+func (m *Connection) IsTraceEnabled() bool {
+	return m.tracer != nil
+}
+
+func (m *Connection) GetTracer() *spi.Tracer {
+	return m.tracer
+}
+
 func (m *Connection) GetConnection() plc4go.PlcConnection {
 	return m
 }
diff --git a/plc4go/internal/plc4go/s7/Driver.go b/plc4go/internal/plc4go/s7/Driver.go
index b6d24c0..c29cb6a 100644
--- a/plc4go/internal/plc4go/s7/Driver.go
+++ b/plc4go/internal/plc4go/s7/Driver.go
@@ -96,7 +96,7 @@ func (m *Driver) GetConnection(transportUrl url.URL, transports map[string]trans
 	driverContext.awaitDisconnectComplete = m.awaitDisconnectComplete
 
 	// Create the new connection
-	connection := NewConnection(codec, configuration, driverContext, m.GetPlcFieldHandler(), &m.tm)
+	connection := NewConnection(codec, configuration, driverContext, m.GetPlcFieldHandler(), &m.tm, options)
 	log.Debug().Msg("created connection, connecting now")
 	return connection.Connect()
 }